From 9e910df7696ab14faf8c793dca64cc7b0dd09218 Mon Sep 17 00:00:00 2001 From: Malo <57839069+MDLC01@users.noreply.github.com> Date: Thu, 23 Jan 2025 16:08:38 +0100 Subject: [PATCH] Rename `compare` to `by` --- crates/typst-library/src/foundations/array.rs | 18 ++++++++---------- tests/suite/foundations/array.typ | 10 +++++----- 2 files changed, 13 insertions(+), 15 deletions(-) diff --git a/crates/typst-library/src/foundations/array.rs b/crates/typst-library/src/foundations/array.rs index fe353a8ad..30481cd7f 100644 --- a/crates/typst-library/src/foundations/array.rs +++ b/crates/typst-library/src/foundations/array.rs @@ -848,8 +848,8 @@ impl Array { /// first element is smaller, positive if the second element is smaller. /// If `{0}` is returned, the order of the elements is not modified. /// - /// When used together with `key`, `compare` will be passed the keys - /// instead of the elements. + /// When used together with `key`, `by` will be passed the keys instead + /// of the elements. /// /// ```example /// #( @@ -859,11 +859,11 @@ impl Array { /// "length", /// ).sorted( /// key: s => s.len(), - /// compare: (x, y) => y - x, + /// by: (x, y) => y - x, /// ) /// ``` #[named] - compare: Option, + by: Option, ) -> SourceResult { let mut result = Ok(()); let mut vec = self.0; @@ -876,14 +876,12 @@ impl Array { }; let x = key_of(x)?; let y = key_of(y)?; - match &compare { + match &by { Some(f) => Ok(match f.call(engine, context, [x, y])? { Value::Int(x) => x.cmp(&0), - x => bail!( - span, - "expected integer from `compare` function, got {}", - x.ty() - ), + x => { + bail!(span, "expected integer from `by` function, got {}", x.ty()) + } }), None => ops::compare(&x, &y).at(span), } diff --git a/tests/suite/foundations/array.typ b/tests/suite/foundations/array.typ index d775646f0..edcbe7e13 100644 --- a/tests/suite/foundations/array.typ +++ b/tests/suite/foundations/array.typ @@ -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(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(("I", "the", "hi", "text").sorted(compare: (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(by: (x, y) => x.len() - y.len()), ("I", "hi", "the", "text")) +#test(("I", "the", "hi", "text").sorted(key: x => x.len(), by: (x, y) => y - x), ("text", "the", "hi", "I")) ---- array-sorted-invalid-compare-function --- -// Error: 2-44 expected integer from `compare` function, got string -#(1, 2, 3).sorted(compare: (_, _) => "hmm") +--- array-sorted-invalid-by-function --- +// Error: 2-39 expected integer from `by` function, got string +#(1, 2, 3).sorted(by: (_, _) => "hmm") --- array-sorted-key-function-positional-1 --- // Error: 12-18 unexpected argument