mirror of
https://github.com/typst/typst
synced 2025-05-13 20:46:23 +08:00
Add default parameter for array.{first, last} (#5970)
This commit is contained in:
parent
1082181a6f
commit
a64af130dc
@ -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
|
||||
|
@ -179,6 +179,10 @@
|
||||
#test((2,).last(), 2)
|
||||
#test((1, 2, 3).first(), 1)
|
||||
#test((1, 2, 3).last(), 3)
|
||||
#test((1, 2).first(default: 99), 1)
|
||||
#test(().first(default: 99), 99)
|
||||
#test((1, 2).last(default: 99), 2)
|
||||
#test(().last(default: 99), 99)
|
||||
|
||||
--- array-first-empty ---
|
||||
// Error: 2-12 array is empty
|
||||
|
Loading…
x
Reference in New Issue
Block a user