Skip to content

Commit

Permalink
fix(compat/trim): trim should support character arrays (#821)
Browse files Browse the repository at this point in the history
* fix(compat/pad): pad should support character arrays

* Update src/compat/string/trim.ts

* Update src/compat/string/trim.ts

* fix

---------

Co-authored-by: Sojin Park <[email protected]>
  • Loading branch information
D-Sketon and raon0211 authored Nov 13, 2024
1 parent 0aa9dc2 commit ba84918
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 3 deletions.
10 changes: 10 additions & 0 deletions src/compat/string/trim.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,14 @@ describe('trim', () => {

expect(actual).toEqual([trimmed, trimmed, trimmed]);
});

it(`\`trim\` should support character arrays`, () => {
const string = 'hello world';
const expected = 'o wo';

expect(func(string, ['rld', 'hel'])).toBe(expected);
expect(func(string, ['rl', 'd', 'he', 'l'])).toBe(expected);
expect(func(string, ['he', 'd', 'lr'])).toBe(expected);
expect(func(string, ['d', 'l', 'r', 'e', 'h'])).toBe(expected);
});
});
2 changes: 1 addition & 1 deletion src/compat/string/trim.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export function trim(str: string, chars?: string | string[], guard?: unknown): s
if (Array.isArray(chars)) {
return trimToolkit(
str,
chars.map(x => x.toString())
chars.flatMap(x => x.toString().split(''))
);
} else {
return trimToolkit(str, (chars as any).toString().split(''));
Expand Down
10 changes: 10 additions & 0 deletions src/compat/string/trimEnd.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,14 @@ describe('trimEnd', () => {

expect(actual).toEqual([trimmed, trimmed, trimmed]);
});

it(`\`trimEnd\` should support character arrays`, () => {
const string = 'hello world';
const expected = 'hello wo';

expect(func(string, ['rld'])).toBe(expected);
expect(func(string, ['rl', 'd'])).toBe(expected);
expect(func(string, ['d', 'lr'])).toBe(expected);
expect(func(string, ['d', 'l', 'r'])).toBe(expected);
});
});
2 changes: 1 addition & 1 deletion src/compat/string/trimEnd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export function trimEnd(str: string, chars?: string | string[], guard?: unknown)
if (Array.isArray(chars)) {
return trimEndToolkit(
str,
chars.map(x => x.toString())
chars.flatMap(x => x.toString().split(''))
);
} else {
return trimEndToolkit(str, (chars as any).toString().split(''));
Expand Down
10 changes: 10 additions & 0 deletions src/compat/string/trimStart.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,14 @@ describe('trimStart', () => {

expect(actual).toEqual([trimmed, trimmed, trimmed]);
});

it(`\`trimStart\` should support character arrays`, () => {
const string = 'hello world';
const expected = 'o world';

expect(func(string, ['hel'])).toBe(expected);
expect(func(string, ['he', 'l'])).toBe(expected);
expect(func(string, ['eh', 'l'])).toBe(expected);
expect(func(string, ['l', 'e', 'h'])).toBe(expected);
});
});
2 changes: 1 addition & 1 deletion src/compat/string/trimStart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export function trimStart(str: string, chars?: string | string[], guard?: unknow
if (Array.isArray(chars)) {
return trimStartToolkit(
str,
chars.map(x => x.toString())
chars.flatMap(x => x.toString().split(''))
);
} else {
return trimStartToolkit(str, (chars as any).toString().split(''));
Expand Down

0 comments on commit ba84918

Please sign in to comment.