Skip to content

Commit 874b0be

Browse files
committed
Add check for parameters with a value of null or undefined
1 parent 2bf2199 commit 874b0be

2 files changed

Lines changed: 16 additions & 14 deletions

File tree

src/lib/string.spec.ts

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -122,27 +122,28 @@ describe("string tests", () => {
122122
});
123123

124124
test.each([
125-
["", " ", ""],
126-
["", "", ""],
127-
["hello world", "", "hello world"],
125+
[null as unknown as string, " ", null as unknown as string],
126+
[undefined as unknown as string, " ", undefined as unknown as string],
127+
["hello world", "hello world", ""],
128+
["hello world", " ", "hello world"],
128129
[" hello world", " ", "hello world"],
129130
])("left trim", (haystack, needle, expected) => {
130131
expect(ltrim(haystack, needle)).toBe(expected);
131132
});
132133

133134
test.each([
134-
["", " ", ""],
135-
["", "", ""],
135+
[null as unknown as string, " ", null as unknown as string],
136+
[undefined as unknown as string, " ", undefined as unknown as string],
136137
["hello world", "hello world", ""],
137-
["hello world", "", "hello world"],
138138
["hello world ", " ", "hello world"],
139+
["hello world", " ", "hello world"],
139140
])("right trim", (haystack, needle, expected) => {
140141
expect(rtrim(haystack, needle)).toBe(expected);
141142
});
142143

143144
test.each([
144-
["", " ", ""],
145-
["", "", ""],
145+
[null as unknown as string, " ", null as unknown as string],
146+
[undefined as unknown as string, " ", undefined as unknown as string],
146147
["hello world", "", "hello world"],
147148
[" hello world ", " ", "hello world"],
148149
])("trim", (haystack, needle, expected) => {

src/lib/string.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -72,11 +72,12 @@ 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-
const needleLength = needle.length;
76-
if (needleLength === 0 || haystack.length === 0) {
75+
if (isNullOrEmpty(haystack) || isNullOrEmpty(needle)) {
7776
return haystack;
7877
}
7978

79+
const needleLength = needle.length;
80+
8081
let offset = 0;
8182

8283
while (haystack.indexOf(needle, offset) === offset) {
@@ -92,13 +93,13 @@ export function ltrim(haystack: string, needle: string): string {
9293
* @returns the string trimmed from the right side
9394
*/
9495
export function rtrim(haystack: string, needle: string): string {
95-
const needleLength = needle.length,
96-
haystackLength = haystack.length;
97-
98-
if (needleLength === 0 || haystackLength === 0) {
96+
if (isNullOrEmpty(haystack) || isNullOrEmpty(needle)) {
9997
return haystack;
10098
}
10199

100+
const needleLength = needle.length,
101+
haystackLength = haystack.length;
102+
102103
let offset = haystackLength,
103104
idx = -1;
104105

0 commit comments

Comments
 (0)