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

## [2.0.0] - 2025-07-29

### Added
Expand Down
19 changes: 18 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,21 @@ describe("string tests", () => {
])("truncate without suffix parameter", (value, maxLength, expected) => {
expect(truncate(value, maxLength)).toBe(expected);
});

test.each([["hello world", ["hello world"]]])("splitLine with single line", (str, expected) => {
Comment thread
drebrez marked this conversation as resolved.
Outdated
expect(splitLine(str)).toStrictEqual(expected);
});

test.each([
["hello world", ["hello world"]],
["hello world\nhello world\nhello world", ["hello world", "hello world", "hello world"]],
["hello world\rhello world\rhello world", ["hello world", "hello world", "hello world"]],
["hello world\r\nhello world\r\nhello world", ["hello world", "hello world", "hello world"]],
Comment thread
drebrez marked this conversation as resolved.
Outdated
])("splitLine", (str, expected) => {
expect(splitLine(str)).toStrictEqual(expected);
});

test.each([["", [""]]])("splitLine with empty strings", (str, expected) => {
expect(splitLine(str)).toStrictEqual(expected);
});
});
9 changes: 9 additions & 0 deletions src/lib/string.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,12 @@ 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
* @returns the individual lines as an array
*/
export function splitLine(str: string): string[] {
Comment thread
drebrez marked this conversation as resolved.
Outdated
return str.split(/\r\n|\r|\n/);
}
Loading