Skip to content

Commit 7adb320

Browse files
committed
Fix string utility functions ltrim, rtrim null check
1 parent 6fa20ae commit 7adb320

File tree

2 files changed

+13
-51
lines changed

2 files changed

+13
-51
lines changed

src/lib/string.spec.ts

Lines changed: 10 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -122,65 +122,33 @@ describe("string tests", () => {
122122
});
123123

124124
test.each([
125-
[null as unknown as string, " ", null],
126-
[undefined as unknown as string, " ", undefined],
125+
["", " ", ""],
126+
["", "", ""],
127+
["hello world", "", "hello world"],
127128
[" hello world", " ", "hello world"],
128-
[" hello world", " ", "hello world"],
129-
[" hello world", " ", "hello world"],
130129
])("left trim", (haystack, needle, expected) => {
131130
expect(ltrim(haystack, needle)).toBe(expected);
132131
});
133132

134133
test.each([
135-
[null as unknown as string, " ", null],
136-
[undefined as unknown as string, " ", undefined],
137-
[" hello world", "", " hello world"],
138-
[" hello world", "", " hello world"],
139-
[" hello world", "", " hello world"],
140-
])("left trim without needle", (haystack, needle, expected) => {
141-
expect(ltrim(haystack, needle)).toBe(expected);
142-
});
143-
144-
test.each([
145-
[null as unknown as string, " ", null],
146-
[undefined as unknown as string, " ", undefined],
134+
["", " ", ""],
135+
["", "", ""],
136+
["hello world", "hello world", ""],
137+
["hello world", "", "hello world"],
147138
["hello world ", " ", "hello world"],
148-
["hello world ", " ", "hello world"],
149-
["hello world ", " ", "hello world"],
150139
])("right trim", (haystack, needle, expected) => {
151140
expect(rtrim(haystack, needle)).toBe(expected);
152141
});
153142

154143
test.each([
155-
[null as unknown as string, " ", null],
156-
[undefined as unknown as string, " ", undefined],
157-
["hello world ", "", "hello world "],
158-
["hello world ", "", "hello world "],
159-
["hello world ", "", "hello world "],
160-
])("right trim without needle", (haystack, needle, expected) => {
161-
expect(rtrim(haystack, needle)).toBe(expected);
162-
});
163-
164-
test.each([
165-
[null as unknown as string, " ", null],
166-
[undefined as unknown as string, " ", undefined],
144+
["", " ", ""],
145+
["", "", ""],
146+
["hello world", "", "hello world"],
167147
[" hello world ", " ", "hello world"],
168-
[" hello world ", " ", "hello world"],
169-
[" hello world ", " ", "hello world"],
170148
])("trim", (haystack, needle, expected) => {
171149
expect(trim(haystack, needle)).toBe(expected);
172150
});
173151

174-
test.each([
175-
[null as unknown as string, " ", null],
176-
[undefined as unknown as string, " ", undefined],
177-
[" hello world ", "", " hello world "],
178-
[" hello world ", "", " hello world "],
179-
[" hello world ", "", " hello world "],
180-
])("trim without needle", (haystack, needle, expected) => {
181-
expect(trim(haystack, needle)).toBe(expected);
182-
});
183-
184152
test.each([["hello world", ["hello world"]]])("splitLine with single line", (str, expected) => {
185153
expect(splitLine(str)).toStrictEqual(expected);
186154
});

src/lib/string.ts

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,6 @@ export function truncate(value: string | undefined, maxLength: number, suffix =
7272
* @returns the string trimmed from the left side
7373
*/
7474
export function ltrim(haystack: string, needle: string): string {
75-
if (!haystack || !needle) return haystack;
76-
7775
const needleLength = needle.length;
7876
if (needleLength === 0 || haystack.length === 0) {
7977
return haystack;
@@ -94,18 +92,14 @@ export function ltrim(haystack: string, needle: string): string {
9492
* @returns the string trimmed from the right side
9593
*/
9694
export function rtrim(haystack: string, needle: string): string {
97-
if (!haystack || !needle) {
98-
return haystack;
99-
}
100-
10195
const needleLength = needle.length,
102-
haystackLen = haystack.length;
96+
haystackLength = haystack.length;
10397

104-
if (needleLength === 0 || haystackLen === 0) {
98+
if (needleLength === 0 || haystackLength === 0) {
10599
return haystack;
106100
}
107101

108-
let offset = haystackLen,
102+
let offset = haystackLength,
109103
idx = -1;
110104

111105
while (true) {

0 commit comments

Comments
 (0)