-
-
Notifications
You must be signed in to change notification settings - Fork 298
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
60 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import { describe, expect, it } from "@jest/globals"; | ||
import { alphaNumericSort } from "../alphaNumericSort.js"; | ||
|
||
describe("alphaNumericSort", () => { | ||
it("should sort the list by creating a new list instead of mutating it", () => { | ||
const list = ["a", "f", "d"]; | ||
const sorted = alphaNumericSort(list); | ||
expect(sorted).toEqual(["a", "d", "f"]); | ||
expect(sorted).not.toBe(list); | ||
expect(list).toEqual(["a", "f", "d"]); | ||
}); | ||
|
||
it("should require an extractor if the provided list is not a list of strings", () => { | ||
// @ts-expect-error | ||
expect(() => alphaNumericSort(["a", 2])).toThrow( | ||
`A \`TextExtractor\` must be provided to \`alphaNumericSort\` for lists that do not contain strings` | ||
); | ||
|
||
expect( | ||
alphaNumericSort(["a", 2], { | ||
extractor: (a) => (typeof a === "number" ? `${a}` : a), | ||
}) | ||
).toEqual([2, "a"]); | ||
}); | ||
|
||
it("should support a list of objects", () => { | ||
const list = [{ name: "Hello" }, { name: "World!" }, { name: "Another!" }]; | ||
expect( | ||
alphaNumericSort(list, { | ||
extractor: (a) => a.name, | ||
}) | ||
).toEqual([{ name: "Another!" }, { name: "Hello" }, { name: "World!" }]); | ||
}); | ||
|
||
it("should allow for a custom compare function", () => { | ||
const list = ["Z", "a", "z", "ä"]; | ||
const compareDE = new Intl.Collator("de").compare; | ||
const compareSV = new Intl.Collator("sv").compare; | ||
|
||
expect(alphaNumericSort(list, { compare: compareDE })).toEqual([ | ||
"a", | ||
"ä", | ||
"z", | ||
"Z", | ||
]); | ||
expect(alphaNumericSort(list, { compare: compareSV })).toEqual([ | ||
"a", | ||
"z", | ||
"Z", | ||
"ä", | ||
]); | ||
|
||
expect(alphaNumericSort(list)).toEqual(["a", "ä", "Z", "z"]); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters