File size: 10,550 Bytes
71174bc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
import {
    slugify,
    capitalize,
    lowerize,
    capitalizeEachWord,
    removeTerminalPunctuation,
    isSentence,
    naturalSort,
    createNaturalSortFunc,
} from "./StringUtils"; // Adjust path as needed

describe("slugify", () => {
    test("should convert spaces to hyphens", () => {
        expect(slugify("hello world")).toBe("hello-world");
        expect(slugify("multiple   spaces  here")).toBe("multiple-spaces-here");
    });

    test("should convert to lowercase by default", () => {
        expect(slugify("HELLO WORLD")).toBe("hello-world");
        expect(slugify("MiXeD CaSe")).toBe("mixed-case");
    });

    test("should preserve case when lowerCase is false", () => {
        expect(slugify("HELLO WORLD", false)).toBe("HELLO-WORLD");
        expect(slugify("MiXeD CaSe", false)).toBe("MiXeD-CaSe");
    });

    test("should replace colons and periods with hyphens", () => {
        expect(slugify("hello:world")).toBe("hello-world");
        expect(slugify("hello.world")).toBe("hello-world");
        expect(slugify("v1.0.1")).toBe("v1-0-1");
    });

    test("should remove other special characters", () => {
        expect(slugify("hello@world!")).toBe("helloworld");
        expect(slugify("special #$% characters")).toBe("special-characters");
    });

    test("should handle empty strings", () => {
        expect(slugify("")).toBe("");
    });
});

describe("capitalize", () => {
    test("should capitalize the first letter of a string", () => {
        expect(capitalize("hello")).toBe("Hello");
        expect(capitalize("world")).toBe("World");
    });

    test("should leave the rest of the string unchanged", () => {
        expect(capitalize("hELLO")).toBe("HELLO");
        expect(capitalize("already Capitalized")).toBe("Already Capitalized");
    });

    test("should handle empty strings", () => {
        expect(capitalize("")).toBe("");
    });

    test("should handle single character strings", () => {
        expect(capitalize("a")).toBe("A");
        expect(capitalize("Z")).toBe("Z");
    });

    test("should handle strings with non-letter first characters", () => {
        expect(capitalize("123abc")).toBe("123abc");
        expect(capitalize(" space first")).toBe(" space first");
    });
});

describe("lowerize", () => {
    test("should lowercase the first letter of a string", () => {
        expect(lowerize("Hello")).toBe("hello");
        expect(lowerize("World")).toBe("world");
    });

    test("should leave the rest of the string unchanged", () => {
        expect(lowerize("HELLO")).toBe("hELLO");
        expect(lowerize("Already Capitalized")).toBe("already Capitalized");
    });

    test("should handle empty strings", () => {
        expect(lowerize("")).toBe("");
    });

    test("should handle single character strings", () => {
        expect(lowerize("A")).toBe("a");
        expect(lowerize("z")).toBe("z");
    });

    test("should handle strings with non-letter first characters", () => {
        expect(lowerize("123ABC")).toBe("123ABC");
        expect(lowerize(" Space first")).toBe(" Space first");
    });
});

describe("capitalizeEachWord", () => {
    test("should capitalize each word in a string", () => {
        expect(capitalizeEachWord("hello world")).toBe("Hello World");
        expect(capitalizeEachWord("this is a test")).toBe("This Is A Test");
    });

    test("should handle strings with already capitalized words", () => {
        expect(capitalizeEachWord("Hello World")).toBe("Hello World");
        expect(capitalizeEachWord("Some Words Are Capitalized")).toBe(
            "Some Words Are Capitalized"
        );
    });

    test("should handle strings with mixed case words", () => {
        expect(capitalizeEachWord("hElLo WoRlD")).toBe("HElLo WoRlD");
    });

    test("should handle empty strings", () => {
        expect(capitalizeEachWord("")).toBe("");
    });

    test("should handle single word strings", () => {
        expect(capitalizeEachWord("test")).toBe("Test");
    });
});

describe("removeTerminalPunctuation", () => {
    test("should remove punctuation from the end of a string", () => {
        expect(removeTerminalPunctuation("hello.")).toBe("hello");
        expect(removeTerminalPunctuation("world!")).toBe("world");
        expect(removeTerminalPunctuation("test?")).toBe("test");
        expect(removeTerminalPunctuation("example,")).toBe("example");
    });

    test("should remove multiple punctuation marks from the end", () => {
        expect(removeTerminalPunctuation("hello...")).toBe("hello");
        expect(removeTerminalPunctuation("world!?")).toBe("world");
    });

    test("should not remove letters or numbers from the end", () => {
        expect(removeTerminalPunctuation("hello1")).toBe("hello1");
        expect(removeTerminalPunctuation("world2")).toBe("world2");
    });

    test("should handle empty strings", () => {
        expect(removeTerminalPunctuation("")).toBe("");
    });

    test("should not remove punctuation from the middle of a string", () => {
        expect(removeTerminalPunctuation("hello.world")).toBe("hello.world");
        expect(removeTerminalPunctuation("example,test")).toBe("example,test");
    });

    test("should handle strings with only punctuation", () => {
        expect(removeTerminalPunctuation("...")).toBe("");
        expect(removeTerminalPunctuation("!?")).toBe("");
    });
});

describe("isSentence", () => {
    test("should return true for proper sentences", () => {
        expect(isSentence("This is a sentence.")).toBe(true);
        expect(isSentence("Another sentence!")).toBe(true);
        expect(isSentence("Is this a question?")).toBe(true);
        expect(isSentence("This ends with a colon:")).toBe(true);
        expect(isSentence('This has a quote at the end."')).toBe(true);
        expect(isSentence("This has a parenthesis at the end.)")).toBe(true);
    });

    test("should return false for strings not starting with a capital letter", () => {
        expect(isSentence("this is not a sentence.")).toBe(false);
        expect(isSentence("lowercase beginning!")).toBe(false);
    });

    test("should return false for strings not ending with proper punctuation", () => {
        expect(isSentence("This does not end with punctuation")).toBe(false);
        expect(isSentence("This ends with a comma,")).toBe(false);
    });

    test("should handle HTML content by stripping tags", () => {
        expect(isSentence("<p>This is a sentence.</p>")).toBe(true);
        expect(isSentence("<strong>Hello</strong> world.")).toBe(true);
    });

    test("should handle whitespace by trimming", () => {
        expect(isSentence("  This has spaces.  ")).toBe(true);
        expect(isSentence("\tTabbed sentence!\n")).toBe(true);
    });

    test("should return true for undefined", () => {
        expect(isSentence(undefined as unknown as string)).toBe(true);
    });

    test("should return true for empty strings", () => {
        expect(isSentence("")).toBe(true);
    });
});

describe("naturalSort", () => {
    test("should sort strings with mixed alphanumeric content naturally", () => {
        const unsorted = ["file10", "file1", "file2"];
        const sorted = [...unsorted].sort(naturalSort);
        expect(sorted).toEqual(["file1", "file2", "file10"]);
    });

    test("should sort numbers numerically, not lexicographically", () => {
        const unsorted = ["10", "1", "2", "100"];
        const sorted = [...unsorted].sort(naturalSort);
        expect(sorted).toEqual(["1", "2", "10", "100"]);
    });

    test("should handle objects with a key accessor", () => {
        const items = [
            { name: "Item 10" },
            { name: "Item 1" },
            { name: "Item 2" },
        ];
        const sorted = [...items].sort((a, b) =>
            naturalSort(a, b, (item) => item.name)
        );
        expect(sorted).toEqual([
            { name: "Item 1" },
            { name: "Item 2" },
            { name: "Item 10" },
        ]);
    });

    test("should handle different types in chunks", () => {
        const unsorted = ["file", "10file", "file10"];
        const sorted = [...unsorted].sort(naturalSort);
        // Numbers should come before strings in the same position
        expect(sorted).toEqual(["10file", "file", "file10"]);
    });

    test("should compare strings lexicographically when no numbers are present", () => {
        const unsorted = ["banana", "apple", "cherry"];
        const sorted = [...unsorted].sort(naturalSort);
        expect(sorted).toEqual(["apple", "banana", "cherry"]);
    });

    test("should handle empty strings", () => {
        const unsorted = ["banana", "", "apple"];
        const sorted = [...unsorted].sort(naturalSort);
        expect(sorted).toEqual(["", "apple", "banana"]);
    });

    test("should prefer shorter strings when prefix is the same", () => {
        const unsorted = ["test123", "test"];
        const sorted = [...unsorted].sort(naturalSort);
        expect(sorted).toEqual(["test", "test123"]);
    });
});

describe("createNaturalSortFunc", () => {
    test("should create a sort function that can be used with Array.sort", () => {
        const sortFunc = createNaturalSortFunc();
        const unsorted = ["file10", "file1", "file2"];
        const sorted = [...unsorted].sort(sortFunc);
        expect(sorted).toEqual(["file1", "file2", "file10"]);
    });

    test("should create a sort function that uses the provided key accessor", () => {
        const items = [
            { name: "Item 10" },
            { name: "Item 1" },
            { name: "Item 2" },
        ];
        const sortFunc = createNaturalSortFunc<{ name: string }>(
            (item) => item.name
        );
        const sorted = [...items].sort(sortFunc);
        expect(sorted).toEqual([
            { name: "Item 1" },
            { name: "Item 2" },
            { name: "Item 10" },
        ]);
    });

    test("should handle complex nested objects", () => {
        const items = [
            { data: { version: "v1.10.1" } },
            { data: { version: "v1.2.1" } },
            { data: { version: "v1.1.1" } },
        ];
        const sortFunc = createNaturalSortFunc<{ data: { version: string } }>(
            (item) => item.data.version
        );
        const sorted = [...items].sort(sortFunc);
        expect(sorted).toEqual([
            { data: { version: "v1.1.1" } },
            { data: { version: "v1.2.1" } },
            { data: { version: "v1.10.1" } },
        ]);
    });
});