Fix use of numeric option for collator (#25917)

* fix(string): use numeric option for collator

* test: add natural sort comparison tests
This commit is contained in:
Kevin Lakotko
2025-06-30 12:00:45 -04:00
committed by GitHub
parent 89d9dd2893
commit 8cc762d839
2 changed files with 54 additions and 2 deletions

View File

@ -0,0 +1,51 @@
import { assert, describe, it } from "vitest";
import { stringCompare } from "../../../src/common/string/compare";
describe("stringCompare", () => {
// Node only ships with English support for `Intl`, so we cannot test for other language collators.
it("Ensure natural order reutrned when numeric value is included", () => {
assert.strictEqual(stringCompare("Helper 2", "Helper 10"), -1);
});
it("Ensure prefixed numeric value is sorted naturally", () => {
assert.strictEqual(stringCompare("2 Helper", "10 Helper"), -1);
});
it("Ensure order has reversed alphabet is sorted", () => {
const reverseAlphabet = [
"z",
"y",
"x",
"w",
"v",
"u",
"t",
"d",
"c",
"b",
"a",
];
assert.deepStrictEqual(
[...reverseAlphabet].sort(stringCompare),
[...reverseAlphabet].reverse()
);
});
it("Ensure natural order when using numbers", () => {
const testArray = [
"Helper 1",
"Helper 10",
"Helper 2",
"Helper 3",
"Helper 4",
];
assert.deepStrictEqual([...testArray].sort(stringCompare), [
"Helper 1",
"Helper 2",
"Helper 3",
"Helper 4",
"Helper 10",
]);
});
});