This PR moves the entire project to Typescript. Due to the ~1000 ignores this will lead to a messy codebase for a while, but the churn is worth it – all of those ignore comments are places that were never type-safe previously. closes #1282
141 lines
2.3 KiB
TypeScript
141 lines
2.3 KiB
TypeScript
import naturalSort from "./naturalSort";
|
|
|
|
describe("#naturalSort", () => {
|
|
it("should sort a list of objects by the given key", () => {
|
|
const items = [
|
|
{
|
|
name: "Joan",
|
|
},
|
|
{
|
|
name: "Pedro",
|
|
},
|
|
{
|
|
name: "Mark",
|
|
},
|
|
];
|
|
expect(naturalSort(items, "name")).toEqual([
|
|
{
|
|
name: "Joan",
|
|
},
|
|
{
|
|
name: "Mark",
|
|
},
|
|
{
|
|
name: "Pedro",
|
|
},
|
|
]);
|
|
});
|
|
|
|
it("should accept a function as the object key", () => {
|
|
const items = [
|
|
{
|
|
name: "Joan",
|
|
},
|
|
{
|
|
name: "Pedro",
|
|
},
|
|
{
|
|
name: "Mark",
|
|
},
|
|
];
|
|
// @ts-expect-error ts-migrate(2345) FIXME: Argument of type '(item: any) => any' is not assig... Remove this comment to see the full error message
|
|
expect(naturalSort(items, (item) => item.name)).toEqual([
|
|
{
|
|
name: "Joan",
|
|
},
|
|
{
|
|
name: "Mark",
|
|
},
|
|
{
|
|
name: "Pedro",
|
|
},
|
|
]);
|
|
});
|
|
|
|
it("should accept natural-sort options", () => {
|
|
const items = [
|
|
{
|
|
name: "Joan",
|
|
},
|
|
{
|
|
name: "joan",
|
|
},
|
|
{
|
|
name: "Pedro",
|
|
},
|
|
{
|
|
name: "Mark",
|
|
},
|
|
];
|
|
expect(
|
|
naturalSort(items, "name", {
|
|
direction: "desc",
|
|
caseSensitive: true,
|
|
})
|
|
).toEqual([
|
|
{
|
|
name: "joan",
|
|
},
|
|
{
|
|
name: "Pedro",
|
|
},
|
|
{
|
|
name: "Mark",
|
|
},
|
|
{
|
|
name: "Joan",
|
|
},
|
|
]);
|
|
});
|
|
|
|
it("should ignore non basic latin letters", () => {
|
|
const items = [
|
|
{
|
|
name: "Abel",
|
|
},
|
|
{
|
|
name: "Martín",
|
|
},
|
|
{
|
|
name: "Ávila",
|
|
},
|
|
];
|
|
expect(naturalSort(items, "name")).toEqual([
|
|
{
|
|
name: "Abel",
|
|
},
|
|
{
|
|
name: "Ávila",
|
|
},
|
|
{
|
|
name: "Martín",
|
|
},
|
|
]);
|
|
});
|
|
|
|
it("should ignore emojis", () => {
|
|
const items = [
|
|
{
|
|
title: "🍔 Document 2",
|
|
},
|
|
{
|
|
title: "🐻 Document 3",
|
|
},
|
|
{
|
|
title: "🙂 Document 1",
|
|
},
|
|
];
|
|
expect(naturalSort(items, "title")).toEqual([
|
|
{
|
|
title: "🙂 Document 1",
|
|
},
|
|
{
|
|
title: "🍔 Document 2",
|
|
},
|
|
{
|
|
title: "🐻 Document 3",
|
|
},
|
|
]);
|
|
});
|
|
});
|