summaryrefslogtreecommitdiff
path: root/crates/typst-library/src/foundations/array.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/typst-library/src/foundations/array.rs')
-rw-r--r--crates/typst-library/src/foundations/array.rs24
1 files changed, 18 insertions, 6 deletions
diff --git a/crates/typst-library/src/foundations/array.rs b/crates/typst-library/src/foundations/array.rs
index e81b9e64..b647473a 100644
--- a/crates/typst-library/src/foundations/array.rs
+++ b/crates/typst-library/src/foundations/array.rs
@@ -172,17 +172,29 @@ impl Array {
}
/// Returns the first item in the array. May be used on the left-hand side
- /// of an assignment. Fails with an error if the array is empty.
+ /// an assignment. Returns the default value if the array is empty
+ /// or fails with an error is no default value was specified.
#[func]
- pub fn first(&self) -> StrResult<Value> {
- self.0.first().cloned().ok_or_else(array_is_empty)
+ pub fn first(
+ &self,
+ /// A default value to return if the array is empty.
+ #[named]
+ default: Option<Value>,
+ ) -> StrResult<Value> {
+ self.0.first().cloned().or(default).ok_or_else(array_is_empty)
}
/// Returns the last item in the array. May be used on the left-hand side of
- /// an assignment. Fails with an error if the array is empty.
+ /// an assignment. Returns the default value if the array is empty
+ /// or fails with an error is no default value was specified.
#[func]
- pub fn last(&self) -> StrResult<Value> {
- self.0.last().cloned().ok_or_else(array_is_empty)
+ pub fn last(
+ &self,
+ /// A default value to return if the array is empty.
+ #[named]
+ default: Option<Value>,
+ ) -> StrResult<Value> {
+ self.0.last().cloned().or(default).ok_or_else(array_is_empty)
}
/// Returns the item at the specified index in the array. May be used on the