|
1 | | -import { isNullOrEmpty, isNullOrWhitespace, capitalize, uncapitalize, truncate } from "./string"; |
| 1 | +import { isNullOrEmpty, isNullOrWhitespace, capitalize, uncapitalize, truncate, splitLines, trimStart, trimEnd, trim } from "./string"; |
2 | 2 |
|
3 | 3 | describe("string tests", () => { |
4 | 4 | test.each([ |
@@ -120,4 +120,77 @@ describe("string tests", () => { |
120 | 120 | ])("truncate without suffix parameter", (value, maxLength, expected) => { |
121 | 121 | expect(truncate(value, maxLength)).toBe(expected); |
122 | 122 | }); |
| 123 | + |
| 124 | + test.each([ |
| 125 | + [null as unknown as string, " ", null as unknown as string], |
| 126 | + [undefined as unknown as string, " ", undefined as unknown as string], |
| 127 | + ["hello world", "hello world", ""], |
| 128 | + ["hello world", " ", "hello world"], |
| 129 | + [" hello world", " ", "hello world"], |
| 130 | + ["hello world hello world", "hello world", " hello world"], |
| 131 | + ["hello worldhello world", "hello world", ""], |
| 132 | + ])("left trim", (haystack, needle, expected) => { |
| 133 | + expect(trimStart(haystack, needle)).toBe(expected); |
| 134 | + }); |
| 135 | + |
| 136 | + test.each([ |
| 137 | + [null as unknown as string, " ", null as unknown as string], |
| 138 | + [undefined as unknown as string, " ", undefined as unknown as string], |
| 139 | + ["hello world", "hello world", ""], |
| 140 | + ["hello world ", " ", "hello world"], |
| 141 | + ["hello world", " ", "hello world"], |
| 142 | + ["hello world hello world", "hello world", "hello world "], |
| 143 | + ["hello worldhello world", "hello world", ""], |
| 144 | + ])("right trim", (haystack, needle, expected) => { |
| 145 | + expect(trimEnd(haystack, needle)).toBe(expected); |
| 146 | + }); |
| 147 | + |
| 148 | + test.each([ |
| 149 | + [null as unknown as string, " ", null as unknown as string], |
| 150 | + [undefined as unknown as string, " ", undefined as unknown as string], |
| 151 | + ["hello world", "", "hello world"], |
| 152 | + [" hello world ", " ", "hello world"], |
| 153 | + ["hello world ", " ", "hello world"], |
| 154 | + [" hello world", " ", "hello world"], |
| 155 | + ["hello worldhello world", "hello world", ""], |
| 156 | + ])("trim", (haystack, needle, expected) => { |
| 157 | + expect(trim(haystack, needle)).toBe(expected); |
| 158 | + }); |
| 159 | + |
| 160 | + test.each([ |
| 161 | + ["", false, false, []], |
| 162 | + [null as unknown as string, false, false, []], |
| 163 | + [undefined as unknown as string, false, false, []], |
| 164 | + [" aaaa \n\nbbbb \n \ncccc", false, false, [" aaaa ", "", "bbbb ", " ", "cccc"]], |
| 165 | + [" aaaa \r\rbbbb \r \rcccc", false, false, [" aaaa ", "", "bbbb ", " ", "cccc"]], |
| 166 | + [" aaaa \r\rbbbb \n \r\ncccc", false, false, [" aaaa ", "", "bbbb ", " ", "cccc"]], |
| 167 | + [" aaaa \r\n\r\nbbbb \r\n \r\ncccc", false, false, [" aaaa ", "", "bbbb ", " ", "cccc"]], |
| 168 | + |
| 169 | + [" aaaa \n\nbbbb \n \ncccc", true, false, [" aaaa ", "bbbb ", " ", "cccc"]], |
| 170 | + [" aaaa \r\rbbbb \r \rcccc", true, false, [" aaaa ", "bbbb ", " ", "cccc"]], |
| 171 | + [" aaaa \r\rbbbb \n \r\ncccc", true, false, [" aaaa ", "bbbb ", " ", "cccc"]], |
| 172 | + [" aaaa \r\n\r\nbbbb \r\n \r\ncccc", true, false, [" aaaa ", "bbbb ", " ", "cccc"]], |
| 173 | + |
| 174 | + [" aaaa \n\nbbbb \n \ncccc", false, true, ["aaaa", "", "bbbb", "", "cccc"]], |
| 175 | + [" aaaa \r\rbbbb \r \rcccc", false, true, ["aaaa", "", "bbbb", "", "cccc"]], |
| 176 | + [" aaaa \r\rbbbb \n \r\ncccc", false, true, ["aaaa", "", "bbbb", "", "cccc"]], |
| 177 | + [" aaaa \r\n\r\nbbbb \r\n \r\ncccc", false, true, ["aaaa", "", "bbbb", "", "cccc"]], |
| 178 | + |
| 179 | + [" aaaa \n\nbbbb \n \ncccc", true, true, ["aaaa", "bbbb", "cccc"]], |
| 180 | + [" aaaa \r\rbbbb \r \rcccc", true, true, ["aaaa", "bbbb", "cccc"]], |
| 181 | + [" aaaa \r\rbbbb \n \r\ncccc", true, true, ["aaaa", "bbbb", "cccc"]], |
| 182 | + ])("splitLines with parameter removeEmptyEntries and trimEntries", (str, removeEmptyEntries, trimEntries, expected) => { |
| 183 | + expect(splitLines(str, removeEmptyEntries, trimEntries)).toEqual(expected); |
| 184 | + }); |
| 185 | + |
| 186 | + test.each([ |
| 187 | + ["", []], |
| 188 | + [null as unknown as string, []], |
| 189 | + [undefined as unknown as string, []], |
| 190 | + [" aaaa \n\nbbbb \n \ncccc", [" aaaa ", "", "bbbb ", " ", "cccc"]], |
| 191 | + [" aaaa \r\rbbbb \r \rcccc", [" aaaa ", "", "bbbb ", " ", "cccc"]], |
| 192 | + [" aaaa \r\n\nbbbb \r\n \r\ncccc", [" aaaa ", "", "bbbb ", " ", "cccc"]], |
| 193 | + ])("splitLines with just the string as a parameter", (str, expected) => { |
| 194 | + expect(splitLines(str)).toEqual(expected); |
| 195 | + }); |
123 | 196 | }); |
0 commit comments