mirror of
https://github.com/typst/typst
synced 2025-08-13 22:57:56 +08:00
Rename compare
to by
This commit is contained in:
parent
40302e472a
commit
9e910df769
@ -848,8 +848,8 @@ impl Array {
|
|||||||
/// first element is smaller, positive if the second element is smaller.
|
/// first element is smaller, positive if the second element is smaller.
|
||||||
/// If `{0}` is returned, the order of the elements is not modified.
|
/// If `{0}` is returned, the order of the elements is not modified.
|
||||||
///
|
///
|
||||||
/// When used together with `key`, `compare` will be passed the keys
|
/// When used together with `key`, `by` will be passed the keys instead
|
||||||
/// instead of the elements.
|
/// of the elements.
|
||||||
///
|
///
|
||||||
/// ```example
|
/// ```example
|
||||||
/// #(
|
/// #(
|
||||||
@ -859,11 +859,11 @@ impl Array {
|
|||||||
/// "length",
|
/// "length",
|
||||||
/// ).sorted(
|
/// ).sorted(
|
||||||
/// key: s => s.len(),
|
/// key: s => s.len(),
|
||||||
/// compare: (x, y) => y - x,
|
/// by: (x, y) => y - x,
|
||||||
/// )
|
/// )
|
||||||
/// ```
|
/// ```
|
||||||
#[named]
|
#[named]
|
||||||
compare: Option<Func>,
|
by: Option<Func>,
|
||||||
) -> SourceResult<Array> {
|
) -> SourceResult<Array> {
|
||||||
let mut result = Ok(());
|
let mut result = Ok(());
|
||||||
let mut vec = self.0;
|
let mut vec = self.0;
|
||||||
@ -876,14 +876,12 @@ impl Array {
|
|||||||
};
|
};
|
||||||
let x = key_of(x)?;
|
let x = key_of(x)?;
|
||||||
let y = key_of(y)?;
|
let y = key_of(y)?;
|
||||||
match &compare {
|
match &by {
|
||||||
Some(f) => Ok(match f.call(engine, context, [x, y])? {
|
Some(f) => Ok(match f.call(engine, context, [x, y])? {
|
||||||
Value::Int(x) => x.cmp(&0),
|
Value::Int(x) => x.cmp(&0),
|
||||||
x => bail!(
|
x => {
|
||||||
span,
|
bail!(span, "expected integer from `by` function, got {}", x.ty())
|
||||||
"expected integer from `compare` function, got {}",
|
}
|
||||||
x.ty()
|
|
||||||
),
|
|
||||||
}),
|
}),
|
||||||
None => ops::compare(&x, &y).at(span),
|
None => ops::compare(&x, &y).at(span),
|
||||||
}
|
}
|
||||||
|
@ -355,12 +355,12 @@
|
|||||||
#test((2, 1, 3, 10, 5, 8, 6, -7, 2).sorted(), (-7, 1, 2, 2, 3, 5, 6, 8, 10))
|
#test((2, 1, 3, 10, 5, 8, 6, -7, 2).sorted(), (-7, 1, 2, 2, 3, 5, 6, 8, 10))
|
||||||
#test((2, 1, 3, -10, -5, 8, 6, -7, 2).sorted(key: x => x), (-10, -7, -5, 1, 2, 2, 3, 6, 8))
|
#test((2, 1, 3, -10, -5, 8, 6, -7, 2).sorted(key: x => x), (-10, -7, -5, 1, 2, 2, 3, 6, 8))
|
||||||
#test((2, 1, 3, -10, -5, 8, 6, -7, 2).sorted(key: x => x * x), (1, 2, 2, 3, -5, 6, -7, 8, -10))
|
#test((2, 1, 3, -10, -5, 8, 6, -7, 2).sorted(key: x => x * x), (1, 2, 2, 3, -5, 6, -7, 8, -10))
|
||||||
#test(("I", "the", "hi", "text").sorted(compare: (x, y) => x.len() - y.len()), ("I", "hi", "the", "text"))
|
#test(("I", "the", "hi", "text").sorted(by: (x, y) => x.len() - y.len()), ("I", "hi", "the", "text"))
|
||||||
#test(("I", "the", "hi", "text").sorted(key: x => x.len(), compare: (x, y) => y - x), ("text", "the", "hi", "I"))
|
#test(("I", "the", "hi", "text").sorted(key: x => x.len(), by: (x, y) => y - x), ("text", "the", "hi", "I"))
|
||||||
|
|
||||||
--- array-sorted-invalid-compare-function ---
|
--- array-sorted-invalid-by-function ---
|
||||||
// Error: 2-44 expected integer from `compare` function, got string
|
// Error: 2-39 expected integer from `by` function, got string
|
||||||
#(1, 2, 3).sorted(compare: (_, _) => "hmm")
|
#(1, 2, 3).sorted(by: (_, _) => "hmm")
|
||||||
|
|
||||||
--- array-sorted-key-function-positional-1 ---
|
--- array-sorted-key-function-positional-1 ---
|
||||||
// Error: 12-18 unexpected argument
|
// Error: 12-18 unexpected argument
|
||||||
|
Loading…
x
Reference in New Issue
Block a user