forked from neolution-ch/javascript-utils
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstring.spec.ts
More file actions
151 lines (138 loc) · 4.74 KB
/
string.spec.ts
File metadata and controls
151 lines (138 loc) · 4.74 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
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
import { isNullOrEmpty, isNullOrWhitespace, capitalize, uncapitalize, truncate, ltrim, rtrim, trim } from "./string";
describe("string tests", () => {
test.each([
// Other types
[null as unknown as string, true],
[undefined as unknown as string, true],
[0 as unknown as string, true],
[10 as unknown as string, true],
[new Date() as unknown as string, true],
// Actual strings
["", true],
[" ", false],
[" ", false],
["Not empty", false],
])("isNullOrEmpty", (value, expected) => {
expect(isNullOrEmpty(value)).toBe(expected);
});
test.each([
// Other types
[null as unknown as string, true],
[undefined as unknown as string, true],
[0 as unknown as string, true],
[10 as unknown as string, true],
[new Date() as unknown as string, true],
// Actual strings
["", true],
[" ", true],
[" ", true],
["Not empty", false],
// String escape sequences
["\t", true], // Horizontal tab (0x0009)
["\n", true], // New line (0x000A)
["\v", true], // Vertical tab (0x000B)
["\f", true], // Form feed (0x000C)
["\r", true], // Carriage return (0x000D)
// Unicode Characters of Category "Space Separator"
["\u0020", true], // Space
["\u00A0", true], // No-Break Space (nbsp)
["\u1680", true], // Ogham Space Mark
["\u2000", true], // En Quad
["\u2001", true], // Em Quad
["\u2002", true], // En Space
["\u2003", true], // Em Space
["\u2004", true], // Three-Per-Em Space
["\u2005", true], // Four-Per-Em Space
["\u2006", true], // Six-Per-Em Space
["\u2007", true], // Figure Space
["\u2008", true], // Punctuation Space
["\u2009", true], // Thin Space
["\u200A", true], // Hair Space
["\u2028", true], // Line Separator
["\u2029", true], // Paragraph Separator
["\u202F", true], // Narrow No-Break Space (nnbsp)
["\u205F", true], // Medium Mathematical Space
["\u3000", true], // Ideographic Space
])("isNullOrWhitespace", (value, expected) => {
expect(isNullOrWhitespace(value)).toBe(expected);
});
test.each([
[null as unknown as string, null],
[undefined as unknown as string, undefined],
["", ""],
["hello", "Hello"],
["hello world", "Hello world"],
])("capitalize", (value, expected) => {
expect(capitalize(value)).toBe(expected);
});
test.each([
[null as unknown as string, null],
[undefined as unknown as string, undefined],
["", ""],
["A", "a"],
["Hello", "hello"],
["Hello world", "hello world"],
["hello world", "hello world"],
])("uncapitalize", (value, expected) => {
expect(uncapitalize(value)).toBe(expected);
});
test.each([
[null as unknown as string, 10, "", null],
[undefined as unknown as string, 10, "", undefined],
["", 10, "", ""],
["hello", 10, "", "hello"],
["hello", 5, "", "hello"],
["hello world", 5, "", "hello"],
["hello world", 8, "", "hello wo"],
["hello world", 11, "", "hello world"],
["hello world", 0, "", ""],
["hello", 3, ">>", "hel>>"],
["test", 2, "", "te"],
["hello world", 5, "...", "hello..."],
["hello world", 8, "...", "hello wo..."],
["hello world", 0, "...", "..."],
])("truncate", (value, maxLength, suffix, expected) => {
expect(truncate(value, maxLength, suffix)).toBe(expected);
});
test.each([
[null as unknown as string, 10, null],
[undefined as unknown as string, 10, undefined],
["", 10, ""],
["hello", 10, "hello"],
["hello", 5, "hello"],
["hello world", 5, "hello"],
["hello world", 8, "hello wo"],
["hello world", 11, "hello world"],
["hello world", 0, ""],
["test", 2, "te"],
["a very long string", 6, "a very"],
["short", 10, "short"],
])("truncate without suffix parameter", (value, maxLength, expected) => {
expect(truncate(value, maxLength)).toBe(expected);
});
test.each([
["", " ", ""],
["", "", ""],
["hello world", "", "hello world"],
[" hello world", " ", "hello world"],
])("left trim", (haystack, needle, expected) => {
expect(ltrim(haystack, needle)).toBe(expected);
});
test.each([
["", " ", ""],
["", "", ""],
["hello world", "hello world", ""],
["hello world", "", "hello world"],
["hello world ", " ", "hello world"],
])("right trim", (haystack, needle, expected) => {
expect(rtrim(haystack, needle)).toBe(expected);
});
test.each([
["", " ", ""],
["", "", ""],
["hello world", "", "hello world"],
[" hello world ", " ", "hello world"],
])("trim", (haystack, needle, expected) => {
expect(trim(haystack, needle)).toBe(expected);
});
});