Skip to content

Commit 2e765c2

Browse files
authored
Add string utility functions: splitLines (#76)
Add feature splitLines for issue #29
1 parent 1eb6e73 commit 2e765c2

3 files changed

Lines changed: 67 additions & 1 deletion

File tree

CHANGELOG.md

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

88
## [Unreleased]
99

10+
### Added
11+
12+
- `splitLines` string utility function
13+
1014
### Changed
1115

1216
- Moved `isValidSwissIbanNumber` and `isValidSwissSocialInsuranceNumber` to swissStandards

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 } from "./string";
1+
import { isNullOrEmpty, isNullOrWhitespace, capitalize, uncapitalize, truncate, splitLines } from "./string";
22

33
describe("string tests", () => {
44
test.each([
@@ -120,4 +120,41 @@ describe("string tests", () => {
120120
])("truncate without suffix parameter", (value, maxLength, expected) => {
121121
expect(truncate(value, maxLength)).toBe(expected);
122122
});
123+
124+
test.each([
125+
["", false, false, []],
126+
[null as unknown as string, false, false, []],
127+
[undefined as unknown as string, false, false, []],
128+
[" aaaa \n\nbbbb \n \ncccc", false, false, [" aaaa ", "", "bbbb ", " ", "cccc"]],
129+
[" aaaa \r\rbbbb \r \rcccc", false, false, [" aaaa ", "", "bbbb ", " ", "cccc"]],
130+
[" aaaa \r\rbbbb \n \r\ncccc", false, false, [" aaaa ", "", "bbbb ", " ", "cccc"]],
131+
[" aaaa \r\n\r\nbbbb \r\n \r\ncccc", false, false, [" aaaa ", "", "bbbb ", " ", "cccc"]],
132+
133+
[" aaaa \n\nbbbb \n \ncccc", true, false, [" aaaa ", "bbbb ", " ", "cccc"]],
134+
[" aaaa \r\rbbbb \r \rcccc", true, false, [" aaaa ", "bbbb ", " ", "cccc"]],
135+
[" aaaa \r\rbbbb \n \r\ncccc", true, false, [" aaaa ", "bbbb ", " ", "cccc"]],
136+
[" aaaa \r\n\r\nbbbb \r\n \r\ncccc", true, false, [" aaaa ", "bbbb ", " ", "cccc"]],
137+
138+
[" aaaa \n\nbbbb \n \ncccc", false, true, ["aaaa", "", "bbbb", "", "cccc"]],
139+
[" aaaa \r\rbbbb \r \rcccc", false, true, ["aaaa", "", "bbbb", "", "cccc"]],
140+
[" aaaa \r\rbbbb \n \r\ncccc", false, true, ["aaaa", "", "bbbb", "", "cccc"]],
141+
[" aaaa \r\n\r\nbbbb \r\n \r\ncccc", false, true, ["aaaa", "", "bbbb", "", "cccc"]],
142+
143+
[" aaaa \n\nbbbb \n \ncccc", true, true, ["aaaa", "bbbb", "cccc"]],
144+
[" aaaa \r\rbbbb \r \rcccc", true, true, ["aaaa", "bbbb", "cccc"]],
145+
[" aaaa \r\rbbbb \n \r\ncccc", true, true, ["aaaa", "bbbb", "cccc"]],
146+
])("splitLines with parameter removeEmptyEntries and trimEntries", (str, removeEmptyEntries, trimEntries, expected) => {
147+
expect(splitLines(str, removeEmptyEntries, trimEntries)).toEqual(expected);
148+
});
149+
150+
test.each([
151+
["", []],
152+
[null as unknown as string, []],
153+
[undefined as unknown as string, []],
154+
[" aaaa \n\nbbbb \n \ncccc", [" aaaa ", "", "bbbb ", " ", "cccc"]],
155+
[" aaaa \r\rbbbb \r \rcccc", [" aaaa ", "", "bbbb ", " ", "cccc"]],
156+
[" aaaa \r\n\nbbbb \r\n \r\ncccc", [" aaaa ", "", "bbbb ", " ", "cccc"]],
157+
])("splitLines with just the string as a parameter", (str, expected) => {
158+
expect(splitLines(str)).toEqual(expected);
159+
});
123160
});

src/lib/string.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,3 +64,28 @@ export function truncate(value: string | undefined, maxLength: number, suffix =
6464

6565
return `${value.slice(0, maxLength)}${suffix}`;
6666
}
67+
68+
/**
69+
* Splits the string at line breaks
70+
* @param str the string to split
71+
* @param removeEmptyEntries the option to remove empty entries
72+
* @param trimEntries the option to trim the entries
73+
* @returns the individual lines as an array
74+
*/
75+
export function splitLines(str: string, removeEmptyEntries: boolean = false, trimEntries: boolean = false): string[] {
76+
if (isNullOrEmpty(str)) {
77+
return [];
78+
}
79+
80+
let splitted = str.split(/\r\n|\r|\n/);
81+
82+
if (trimEntries) {
83+
splitted = splitted.map((x) => x.trim());
84+
}
85+
86+
if (removeEmptyEntries) {
87+
splitted = splitted.filter((line) => line.length > 0);
88+
}
89+
90+
return splitted;
91+
}

0 commit comments

Comments
 (0)