Skip to content
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Added

- `splitLine` string utility function

### Changed

- Moved `isValidSwissIbanNumber` and `isValidSwissSocialInsuranceNumber` to swissStandards
Expand Down
31 changes: 30 additions & 1 deletion src/lib/string.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { isNullOrEmpty, isNullOrWhitespace, capitalize, uncapitalize, truncate } from "./string";
import { isNullOrEmpty, isNullOrWhitespace, capitalize, uncapitalize, truncate, splitLine } from "./string";

describe("string tests", () => {
test.each([
Expand Down Expand Up @@ -120,4 +120,33 @@ describe("string tests", () => {
])("truncate without suffix parameter", (value, maxLength, expected) => {
expect(truncate(value, maxLength)).toBe(expected);
});

test.each([
["", []],
["hello world", ["hello world"]],
["hello world\n", ["hello world", ""]],
["hello world\n\n", ["hello world", "", ""]],
["hello world\r\r", ["hello world", "", ""]],
["hello world\r\n\r\n", ["hello world", "", ""]],
])("splitLine without the parameter to remove the empty entries", (str, expected) => {
expect(splitLine(str)).toEqual(expected);
});

test.each([
["", []],
[null as unknown as string, []],
[undefined as unknown as string, []],
["hello world", ["hello world"]],
["hello world\n", ["hello world"]],
["hello world\n\n", ["hello world"]],
["hello world\r\r", ["hello world"]],
["hello world\r\n\r\n", ["hello world"]],
["hello world 1\nhello world 2\nhello world 3", ["hello world 1", "hello world 2", "hello world 3"]],
["hello world 1\nhello world 2\rhello world 3", ["hello world 1", "hello world 2", "hello world 3"]],
["hello world 1\rhello world 2\rhello world 3", ["hello world 1", "hello world 2", "hello world 3"]],
["hello world 1\r\nhello world 2\r\nhello world 3", ["hello world 1", "hello world 2", "hello world 3"]],
["hello world 1\r\nhello world 2\nhello world 3\rhello world 4", ["hello world 1", "hello world 2", "hello world 3", "hello world 4"]],
])("splitLine with the parameter to remove empty entries", (str, expected) => {
Comment thread
drebrez marked this conversation as resolved.
Outdated
expect(splitLine(str, true)).toEqual(expected);
});
});
15 changes: 15 additions & 0 deletions src/lib/string.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,18 @@ export function truncate(value: string | undefined, maxLength: number, suffix =

return `${value.slice(0, maxLength)}${suffix}`;
}

/**
* Splits the string at line breaks
* @param str the string to split
* @param removeEmptyEntries the option to remove empty entries
* @returns the individual lines as an array
*/
export function splitLine(str: string, removeEmptyEntries: boolean = false): string[] {
Comment thread
drebrez marked this conversation as resolved.
Outdated
if (isNullOrEmpty(str)) {
return [];
}

const splitted = str.split(/\r\n|\r|\n/);
return removeEmptyEntries ? splitted.filter((line) => !isNullOrWhitespace(line)) : splitted;
Comment thread
drebrez marked this conversation as resolved.
Outdated
}
Loading