mirror of
https://github.com/home-assistant/frontend.git
synced 2025-07-21 23:28:58 +00:00
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:
51
test/common/string/sort.test.ts
Normal file
51
test/common/string/sort.test.ts
Normal 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",
|
||||
]);
|
||||
});
|
||||
});
|
Reference in New Issue
Block a user