Skip to content

Commit 7da65c5

Browse files
committed
Changed the name of the function ltrim to trimStart, the function rtrim to trimEnd and improved the code
1 parent 5cd4df8 commit 7da65c5

File tree

3 files changed

+14
-16
lines changed

3 files changed

+14
-16
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
99

1010
### Added
1111

12-
- `ltrim`, `rtrim` and `trim` string type utility functions
12+
- `trimStart`, `trimEnd` and `trim` string type utility functions
1313

1414
### Changed
1515

src/lib/string.spec.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { isNullOrEmpty, isNullOrWhitespace, capitalize, uncapitalize, truncate, ltrim, rtrim, trim } from "./string";
1+
import { isNullOrEmpty, isNullOrWhitespace, capitalize, uncapitalize, truncate, trimStart, trimEnd, trim } from "./string";
22

33
describe("string tests", () => {
44
test.each([
@@ -128,7 +128,7 @@ describe("string tests", () => {
128128
["hello world", " ", "hello world"],
129129
[" hello world", " ", "hello world"],
130130
])("left trim", (haystack, needle, expected) => {
131-
expect(ltrim(haystack, needle)).toBe(expected);
131+
expect(trimStart(haystack, needle)).toBe(expected);
132132
});
133133

134134
test.each([
@@ -138,14 +138,17 @@ describe("string tests", () => {
138138
["hello world ", " ", "hello world"],
139139
["hello world", " ", "hello world"],
140140
])("right trim", (haystack, needle, expected) => {
141-
expect(rtrim(haystack, needle)).toBe(expected);
141+
expect(trimEnd(haystack, needle)).toBe(expected);
142142
});
143143

144144
test.each([
145145
[null as unknown as string, " ", null as unknown as string],
146146
[undefined as unknown as string, " ", undefined as unknown as string],
147147
["hello world", "", "hello world"],
148148
[" hello world ", " ", "hello world"],
149+
["hello world ", " ", "hello world"],
150+
[" hello world", " ", "hello world"],
151+
["hello worldhello world", "hello world", ""],
149152
])("trim", (haystack, needle, expected) => {
150153
expect(trim(haystack, needle)).toBe(expected);
151154
});

src/lib/string.ts

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -71,17 +71,15 @@ export function truncate(value: string | undefined, maxLength: number, suffix =
7171
* @param needle the thing to trim
7272
* @returns the string trimmed from the left side
7373
*/
74-
export function ltrim(haystack: string, needle: string): string {
74+
export function trimStart(haystack: string, needle: string): string {
7575
if (isNullOrEmpty(haystack) || isNullOrEmpty(needle)) {
7676
return haystack;
7777
}
7878

79-
const needleLength = needle.length;
80-
8179
let offset = 0;
8280

8381
while (haystack.indexOf(needle, offset) === offset) {
84-
offset = offset + needleLength;
82+
offset = offset + needle.length;
8583
}
8684
return haystack.slice(offset);
8785
}
@@ -92,20 +90,17 @@ export function ltrim(haystack: string, needle: string): string {
9290
* @param needle the thing to trim
9391
* @returns the string trimmed from the right side
9492
*/
95-
export function rtrim(haystack: string, needle: string): string {
93+
export function trimEnd(haystack: string, needle: string): string {
9694
if (isNullOrEmpty(haystack) || isNullOrEmpty(needle)) {
9795
return haystack;
9896
}
9997

100-
const needleLength = needle.length,
101-
haystackLength = haystack.length;
102-
103-
let offset = haystackLength,
98+
let offset = haystack.length,
10499
idx = -1;
105100

106101
while (true) {
107102
idx = haystack.lastIndexOf(needle, offset - 1);
108-
if (idx === -1 || idx + needleLength !== offset) {
103+
if (idx === -1 || idx + needle.length !== offset) {
109104
break;
110105
}
111106
if (idx === 0) {
@@ -124,6 +119,6 @@ export function rtrim(haystack: string, needle: string): string {
124119
* @returns the string trimmed from the right and left side
125120
*/
126121
export function trim(haystack: string, needle: string): string {
127-
const trimmed = ltrim(haystack, needle);
128-
return rtrim(trimmed, needle);
122+
const trimmed = trimStart(haystack, needle);
123+
return trimEnd(trimmed, needle);
129124
}

0 commit comments

Comments
 (0)