mirror of
https://github.com/Kir-Antipov/mc-publish.git
synced 2024-11-22 08:20:58 -05:00
Implemented default comparers for strings
This commit is contained in:
parent
d1b1ed9a16
commit
89d56de5e4
2 changed files with 68 additions and 0 deletions
13
src/utils/comparison/string-comparer.ts
Normal file
13
src/utils/comparison/string-comparer.ts
Normal file
|
@ -0,0 +1,13 @@
|
||||||
|
import { createBaseComparer, createDefaultComparer } from "./comparer";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A string comparer that performs a case-sensitive ordinal string comparison.
|
||||||
|
*/
|
||||||
|
export const ORDINAL_COMPARER = createDefaultComparer<string>();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A string comparer that ignores case differences.
|
||||||
|
*/
|
||||||
|
export const IGNORE_CASE_COMPARER = createBaseComparer<string>().thenBy(
|
||||||
|
(left, right) => left?.localeCompare(right, undefined, { sensitivity: "accent" }) ?? 0
|
||||||
|
);
|
55
tests/unit/utils/comparison/string-comparer.spec.ts
Normal file
55
tests/unit/utils/comparison/string-comparer.spec.ts
Normal file
|
@ -0,0 +1,55 @@
|
||||||
|
import { ORDINAL_COMPARER, IGNORE_CASE_COMPARER } from "@/utils/comparison/string-comparer";
|
||||||
|
|
||||||
|
describe("ORDINAL_COMPARER", () => {
|
||||||
|
test("compares two strings using case-sensitive ordinal comparison", () => {
|
||||||
|
expect(ORDINAL_COMPARER("test", "test")).toBe(0);
|
||||||
|
expect(ORDINAL_COMPARER("Test", "test")).toBeLessThan(0);
|
||||||
|
expect(ORDINAL_COMPARER("test", "Test")).toBeGreaterThan(0);
|
||||||
|
expect(ORDINAL_COMPARER("test", "testing")).toBeLessThan(0);
|
||||||
|
expect(ORDINAL_COMPARER("testing", "test")).toBeGreaterThan(0);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("treats undefined as smaller than any other value", () => {
|
||||||
|
expect(ORDINAL_COMPARER(undefined, "test")).toBeLessThan(0);
|
||||||
|
expect(ORDINAL_COMPARER(undefined, null)).toBeLessThan(0);
|
||||||
|
expect(ORDINAL_COMPARER(undefined, undefined)).toBe(0);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("treats null as smaller than any other value except undefined", () => {
|
||||||
|
expect(ORDINAL_COMPARER(null, "test")).toBeLessThan(0);
|
||||||
|
expect(ORDINAL_COMPARER(null, undefined)).toBeGreaterThan(0);
|
||||||
|
expect(ORDINAL_COMPARER(null, null)).toBe(0);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("IGNORE_CASE_COMPARER", () => {
|
||||||
|
test("compares two strings using case-insensitive ordinal comparison", () => {
|
||||||
|
expect(IGNORE_CASE_COMPARER("A", "a")).toBe(0);
|
||||||
|
expect(IGNORE_CASE_COMPARER("a", "B")).toBeLessThan(0);
|
||||||
|
expect(IGNORE_CASE_COMPARER("A", "b")).toBeLessThan(0);
|
||||||
|
expect(IGNORE_CASE_COMPARER("A", "B")).toBeLessThan(0);
|
||||||
|
expect(IGNORE_CASE_COMPARER("B", "a")).toBeGreaterThan(0);
|
||||||
|
expect(IGNORE_CASE_COMPARER("b", "A")).toBeGreaterThan(0);
|
||||||
|
expect(IGNORE_CASE_COMPARER("B", "A")).toBeGreaterThan(0);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("ignores case differences when comparing strings", () => {
|
||||||
|
expect(IGNORE_CASE_COMPARER("test", "test")).toBe(0);
|
||||||
|
expect(IGNORE_CASE_COMPARER("Test", "test")).toBe(0);
|
||||||
|
expect(IGNORE_CASE_COMPARER("TEST", "test")).toBe(0);
|
||||||
|
expect(IGNORE_CASE_COMPARER("test", "TEST")).toBe(0);
|
||||||
|
expect(IGNORE_CASE_COMPARER("Test", "TEST")).toBe(0);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("treats undefined as smaller than any other value", () => {
|
||||||
|
expect(IGNORE_CASE_COMPARER(undefined, "test")).toBeLessThan(0);
|
||||||
|
expect(IGNORE_CASE_COMPARER(undefined, null)).toBeLessThan(0);
|
||||||
|
expect(IGNORE_CASE_COMPARER(undefined, undefined)).toBe(0);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("treats null as smaller than any other value except undefined", () => {
|
||||||
|
expect(IGNORE_CASE_COMPARER(null, "test")).toBeLessThan(0);
|
||||||
|
expect(IGNORE_CASE_COMPARER(null, undefined)).toBeGreaterThan(0);
|
||||||
|
expect(IGNORE_CASE_COMPARER(null, null)).toBe(0);
|
||||||
|
});
|
||||||
|
});
|
Loading…
Reference in a new issue