Skip to content

Commit a008fdd

Browse files
authored
Merge branch 'main' into feature/trim(29)
2 parents 08c192e + 2e765c2 commit a008fdd

File tree

3 files changed

+64
-1
lines changed

3 files changed

+64
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
99

1010
### Added
1111

12+
- `splitLines` string utility function
1213
- `trimStart`, `trimEnd` and `trim` string type utility functions
1314

1415
### Changed

src/lib/string.spec.ts

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { isNullOrEmpty, isNullOrWhitespace, capitalize, uncapitalize, truncate, trimStart, trimEnd, trim } from "./string";
1+
import { isNullOrEmpty, isNullOrWhitespace, capitalize, uncapitalize, truncate, splitLines, trimStart, trimEnd, trim } from "./string";
22

33
describe("string tests", () => {
44
test.each([
@@ -156,4 +156,41 @@ describe("string tests", () => {
156156
])("trim", (haystack, needle, expected) => {
157157
expect(trim(haystack, needle)).toBe(expected);
158158
});
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+
});
159196
});

src/lib/string.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,3 +122,28 @@ export function trim(haystack: string, needle: string): string {
122122
const trimmed = trimStart(haystack, needle);
123123
return trimEnd(trimmed, needle);
124124
}
125+
126+
/**
127+
* Splits the string at line breaks
128+
* @param str the string to split
129+
* @param removeEmptyEntries the option to remove empty entries
130+
* @param trimEntries the option to trim the entries
131+
* @returns the individual lines as an array
132+
*/
133+
export function splitLines(str: string, removeEmptyEntries: boolean = false, trimEntries: boolean = false): string[] {
134+
if (isNullOrEmpty(str)) {
135+
return [];
136+
}
137+
138+
let splitted = str.split(/\r\n|\r|\n/);
139+
140+
if (trimEntries) {
141+
splitted = splitted.map((x) => x.trim());
142+
}
143+
144+
if (removeEmptyEntries) {
145+
splitted = splitted.filter((line) => line.length > 0);
146+
}
147+
148+
return splitted;
149+
}

0 commit comments

Comments
 (0)