summaryrefslogtreecommitdiff
path: root/crates
diff options
context:
space:
mode:
authorbluebear94 <uruwi@protonmail.com>2023-07-29 17:11:21 -0400
committerGitHub <noreply@github.com>2023-07-29 23:11:21 +0200
commit66df130ca4c9217d137cba0778d33c8ff9349d33 (patch)
tree6f897e2dcd3bcbee0c311ab0d41d1d000b79ef72 /crates
parent5bd97e218b698b2ad14fad0470b3b800469ee748 (diff)
Add start parameter to array enumerate (#1818)
Diffstat (limited to 'crates')
-rw-r--r--crates/typst/src/eval/array.rs12
-rw-r--r--crates/typst/src/eval/methods.rs5
2 files changed, 14 insertions, 3 deletions
diff --git a/crates/typst/src/eval/array.rs b/crates/typst/src/eval/array.rs
index 5d5fdcdb..86c41ff6 100644
--- a/crates/typst/src/eval/array.rs
+++ b/crates/typst/src/eval/array.rs
@@ -392,10 +392,18 @@ impl Array {
}
/// Enumerate all items in the array.
- pub fn enumerate(&self) -> Self {
+ pub fn enumerate(&self, start: i64) -> StrResult<Self> {
self.iter()
.enumerate()
- .map(|(i, value)| array![i, value.clone()].into_value())
+ .map(|(i, value)| {
+ Ok(array![
+ start
+ .checked_add_unsigned(i as u64)
+ .ok_or_else(|| "array index is too large".to_string())?,
+ value.clone()
+ ]
+ .into_value())
+ })
.collect()
}
diff --git a/crates/typst/src/eval/methods.rs b/crates/typst/src/eval/methods.rs
index bf47b7aa..6a846f52 100644
--- a/crates/typst/src/eval/methods.rs
+++ b/crates/typst/src/eval/methods.rs
@@ -146,7 +146,10 @@ pub fn call(
}
"sorted" => array.sorted(vm, span, args.named("key")?)?.into_value(),
"zip" => array.zip(args.expect("other")?).into_value(),
- "enumerate" => array.enumerate().into_value(),
+ "enumerate" => array
+ .enumerate(args.named("start")?.unwrap_or(0))
+ .at(span)?
+ .into_value(),
"dedup" => array.dedup(vm, args.named("key")?)?.into_value(),
_ => return missing(),
},