-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patha_to_z_ul_test.ts
More file actions
59 lines (49 loc) · 1.78 KB
/
a_to_z_ul_test.ts
File metadata and controls
59 lines (49 loc) · 1.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import { assertEquals, assert } from "@std/assert";
// Pure logic extracted from AToZUL for unit testing without a DOM.
function groupByFirstLetter(
items: string[],
): Record<string, string[]> {
const sections: Record<string, string[]> = {};
for (const item of items) {
const letter = (item.trim()[0] ?? "").toUpperCase();
if (!sections[letter]) sections[letter] = [];
sections[letter].push(item);
}
return sections;
}
function alphabetSections(
sections: Record<string, string[]>,
): string[] {
const alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
return alphabet.split("").filter((letter) => sections[letter] !== undefined);
}
const sampleItems = [
"Banana", "Apple", "Cherry", "Avocado", "Blueberry", "Apricot",
];
Deno.test("groupByFirstLetter groups correctly", () => {
const result = groupByFirstLetter(sampleItems);
assertEquals(result["A"], ["Apple", "Avocado", "Apricot"]);
assertEquals(result["B"], ["Banana", "Blueberry"]);
assertEquals(result["C"], ["Cherry"]);
});
Deno.test("groupByFirstLetter is case-insensitive on first letter", () => {
const result = groupByFirstLetter(["alice", "Bob", "CAROL"]);
assert("A" in result);
assert("B" in result);
assert("C" in result);
});
Deno.test("alphabetSections returns present letters in order", () => {
const sections = groupByFirstLetter(sampleItems);
const letters = alphabetSections(sections);
assertEquals(letters, ["A", "B", "C"]);
});
Deno.test("alphabetSections returns empty for no items", () => {
assertEquals(alphabetSections({}), []);
});
Deno.test("groupByFirstLetter handles empty list", () => {
assertEquals(groupByFirstLetter([]), {});
});
Deno.test("groupByFirstLetter handles single item", () => {
const result = groupByFirstLetter(["Zebra"]);
assertEquals(result["Z"], ["Zebra"]);
});