mirror of
https://github.com/toss/es-toolkit.git
synced 2024-11-24 11:45:26 +03:00
feat(trim, trimStart, trimEnd): Ensure compatibility with trim, trimStart, and trimEnd
This commit is contained in:
parent
5a7d931c38
commit
4a17214496
26
docs/ja/reference/string/trim.md
Normal file
26
docs/ja/reference/string/trim.md
Normal file
@ -0,0 +1,26 @@
|
||||
# trim
|
||||
|
||||
文字列から前後の空白や指定された文字を削除します。
|
||||
|
||||
## インターフェース
|
||||
|
||||
```typescript
|
||||
function trim(str: string, chars?: string | string[]): string;
|
||||
```
|
||||
|
||||
### パラメータ
|
||||
|
||||
- `str` (`string`): 文字をトリムする文字列。
|
||||
- `chars` (`string | string[]`): 文字列から削除する文字。単一の文字または文字の配列で指定します。
|
||||
|
||||
### 戻り値
|
||||
|
||||
(`string`): 指定された文字が削除された後の結果の文字列。
|
||||
|
||||
## 例
|
||||
|
||||
```typescript
|
||||
trim(' hello '); // "hello"
|
||||
trim('--hello--', '-'); // "hello"
|
||||
trim('##hello##', ['#', 'o']); // "hell"
|
||||
```
|
27
docs/ja/reference/string/trimEnd.md
Normal file
27
docs/ja/reference/string/trimEnd.md
Normal file
@ -0,0 +1,27 @@
|
||||
# trimEnd
|
||||
|
||||
文字列末尾の空白または指定された文字を削除します。
|
||||
|
||||
## インターフェース
|
||||
|
||||
```typescript
|
||||
function trimEnd(str: string, chars?: string | string[]): string;
|
||||
```
|
||||
|
||||
### パラメータ
|
||||
|
||||
- `str` (`string`): 末尾の文字が削除される文字列。
|
||||
- `chars` (`string | string[]`): 文字列の末尾から削除する文字。
|
||||
|
||||
### 戻り値
|
||||
|
||||
(`string`): 指定された末尾の文字が削除された後の結果の文字列。
|
||||
|
||||
## 例
|
||||
|
||||
```typescript
|
||||
const trimmedStr1 = trimEnd('hello---', '-'); // returns 'hello'
|
||||
const trimmedStr2 = trimEnd('123000', '0'); // returns '123'
|
||||
const trimmedStr3 = trimEnd('abcabcabc', 'c'); // returns 'abcabcab'
|
||||
const trimmedStr4 = trimEnd('trimmedxxx', 'x'); // returns 'trimmed'
|
||||
```
|
27
docs/ja/reference/string/trimStart.md
Normal file
27
docs/ja/reference/string/trimStart.md
Normal file
@ -0,0 +1,27 @@
|
||||
# trimStart
|
||||
|
||||
文字列の前にある空白または指定された文字を削除します。
|
||||
|
||||
## インターフェース
|
||||
|
||||
```typescript
|
||||
function trimStart(str: string, chars?: string | string[]): string;
|
||||
```
|
||||
|
||||
### パラメータ
|
||||
|
||||
- `str` (`string`): 先頭の文字が削除される文字列。
|
||||
- `chars` (`string | string[]`): 文字列の末尾から削除する文字。
|
||||
|
||||
### 戻り値
|
||||
|
||||
(`string`): 指定された先頭の文字が削除された後の結果の文字列。
|
||||
|
||||
## 例
|
||||
|
||||
```typescript
|
||||
const trimmedStr1 = trimStart('---hello', '-'); // returns 'hello'
|
||||
const trimmedStr2 = trimStart('000123', '0'); // returns '123'
|
||||
const trimmedStr3 = trimStart('abcabcabc', 'a'); // returns 'bcabcabc'
|
||||
const trimmedStr4 = trimStart('xxxtrimmed', 'x'); // returns 'trimmed'
|
||||
```
|
26
docs/ko/reference/string/trim.md
Normal file
26
docs/ko/reference/string/trim.md
Normal file
@ -0,0 +1,26 @@
|
||||
# trim
|
||||
|
||||
문자열의 앞뒤에 있는 공백 또는 지정된 문자를 제거해요.
|
||||
|
||||
## 인터페이스
|
||||
|
||||
```typescript
|
||||
function trim(str: string, chars?: string | string[]): string;
|
||||
```
|
||||
|
||||
### 파라미터
|
||||
|
||||
- `str` (`string`): 양옆에서 문자를 제거할 문자열.
|
||||
- `chars` (`string | string[]`): 문자열에서 제거할 문자예요. 단일 문자 또는 문자 배열일 수 있어요. 기본값은 공백이에요.
|
||||
|
||||
### 반환 값
|
||||
|
||||
(`string`): 지정된 문자가 제거된 후의 결과 문자열입니다요.
|
||||
|
||||
## 예시
|
||||
|
||||
```typescript
|
||||
trim(' hello '); // "hello"
|
||||
trim('--hello--', '-'); // "hello"
|
||||
trim('##hello##', ['#', 'o']); // "hell"
|
||||
```
|
27
docs/ko/reference/string/trimEnd.md
Normal file
27
docs/ko/reference/string/trimEnd.md
Normal file
@ -0,0 +1,27 @@
|
||||
# trimEnd
|
||||
|
||||
문자열 끝의 공백 또는 지정된 문자를 제거해요.
|
||||
|
||||
## 인터페이스
|
||||
|
||||
```typescript
|
||||
function trimEnd(str: string, chars?: string | string[]): string;
|
||||
```
|
||||
|
||||
### 파라미터
|
||||
|
||||
- `str` (`string`): 끝에서 문자를 제거할 문자열.
|
||||
- `chars` (`string | string[]`): 문자열 끝에서 제거할 문자. 기본값은 공백이에요.
|
||||
|
||||
### 반환 값
|
||||
|
||||
(`string`): 끝에 공백이나 문자가 제거된 문자열.
|
||||
|
||||
## 예시
|
||||
|
||||
```typescript
|
||||
const trimmedStr1 = trimEnd('hello---', '-'); // returns 'hello'
|
||||
const trimmedStr2 = trimEnd('123000', '0'); // returns '123'
|
||||
const trimmedStr3 = trimEnd('abcabcabc', 'c'); // returns 'abcabcab'
|
||||
const trimmedStr4 = trimEnd('trimmedxxx', 'x'); // returns 'trimmed'
|
||||
```
|
27
docs/ko/reference/string/trimStart.md
Normal file
27
docs/ko/reference/string/trimStart.md
Normal file
@ -0,0 +1,27 @@
|
||||
# trimStart
|
||||
|
||||
문자열의 시작 부분에 있는 공백 또는 지정된 문자를 제거해요.
|
||||
|
||||
## 인터페이스
|
||||
|
||||
```typescript
|
||||
function trimStart(str: string, chars?: string | string[]): string;
|
||||
```
|
||||
|
||||
### 파라미터
|
||||
|
||||
- `str` (`string`): 시작 부분에서 문자를 제거할 문자열.
|
||||
- `chars` (`string | string[]`): 문자열의 시작 부분에서 제거할 문자. 기본값은 공백이에요.
|
||||
|
||||
### 반환 값
|
||||
|
||||
(`string`): 지정된 시작 문자가 제거된 후의 결과 문자열.
|
||||
|
||||
## 예시
|
||||
|
||||
```typescript
|
||||
const trimmedStr1 = trimStart('---hello', '-'); // returns 'hello'
|
||||
const trimmedStr2 = trimStart('000123', '0'); // returns '123'
|
||||
const trimmedStr3 = trimStart('abcabcabc', 'a'); // returns 'bcabcabc'
|
||||
const trimmedStr4 = trimStart('xxxtrimmed', 'x'); // returns 'trimmed'
|
||||
```
|
26
docs/reference/string/trim.md
Normal file
26
docs/reference/string/trim.md
Normal file
@ -0,0 +1,26 @@
|
||||
# trim
|
||||
|
||||
Removes leading and trailing whitespace or specified characters from a string.
|
||||
|
||||
## Signature
|
||||
|
||||
```typescript
|
||||
function trim(str: string, chars?: string | string[]): string;
|
||||
```
|
||||
|
||||
### Parameters
|
||||
|
||||
- `str` (`string`): The string from which characters will be trimmed.
|
||||
- `chars` (`string | string[]`): The character(s) to remove from the string. Can be a single character or an array of characters.
|
||||
|
||||
### Returns
|
||||
|
||||
(`string`): The resulting string after the specified characters have been removed.
|
||||
|
||||
## Examples
|
||||
|
||||
```typescript
|
||||
trim(' hello '); // "hello"
|
||||
trim('--hello--', '-'); // "hello"
|
||||
trim('##hello##', ['#', 'o']); // "hell"
|
||||
```
|
27
docs/reference/string/trimEnd.md
Normal file
27
docs/reference/string/trimEnd.md
Normal file
@ -0,0 +1,27 @@
|
||||
# trimEnd
|
||||
|
||||
Removes trailing whitespace or specified characters from a string.
|
||||
|
||||
## Signature
|
||||
|
||||
```typescript
|
||||
function trimEnd(str: string, chars?: string | string[]): string;
|
||||
```
|
||||
|
||||
### Parameters
|
||||
|
||||
- `str` (`string`): The string from which trailing characters will be trimmed.
|
||||
- `chars` (`string | string[]`): The character(s) to remove from the end of the string.
|
||||
|
||||
### Returns
|
||||
|
||||
(`string`): The resulting string after the specified trailing character has been removed.
|
||||
|
||||
## Examples
|
||||
|
||||
```typescript
|
||||
const trimmedStr1 = trimEnd('hello---', '-'); // returns 'hello'
|
||||
const trimmedStr2 = trimEnd('123000', '0'); // returns '123'
|
||||
const trimmedStr3 = trimEnd('abcabcabc', 'c'); // returns 'abcabcab'
|
||||
const trimmedStr4 = trimEnd('trimmedxxx', 'x'); // returns 'trimmed'
|
||||
```
|
27
docs/reference/string/trimStart.md
Normal file
27
docs/reference/string/trimStart.md
Normal file
@ -0,0 +1,27 @@
|
||||
# trimStart
|
||||
|
||||
Removes leading whitespace or specified characters from a string.
|
||||
|
||||
## Signature
|
||||
|
||||
```typescript
|
||||
function trimStart(str: string, chars?: string | string[]): string;
|
||||
```
|
||||
|
||||
### Parameters
|
||||
|
||||
- `str` (`string`): The string from which leading characters will be trimmed.
|
||||
- `chars` (`string | string[]`): The character(s) to remove from the end of the string.
|
||||
|
||||
### Returns
|
||||
|
||||
(`string`): The resulting string after the specified leading character has been removed.
|
||||
|
||||
## Examples
|
||||
|
||||
```typescript
|
||||
const trimmedStr1 = trimStart('---hello', '-'); // returns 'hello'
|
||||
const trimmedStr2 = trimStart('000123', '0'); // returns '123'
|
||||
const trimmedStr3 = trimStart('abcabcabc', 'a'); // returns 'bcabcabc'
|
||||
const trimmedStr4 = trimStart('xxxtrimmed', 'x'); // returns 'trimmed'
|
||||
```
|
26
docs/zh_hans/reference/string/trim.md
Normal file
26
docs/zh_hans/reference/string/trim.md
Normal file
@ -0,0 +1,26 @@
|
||||
# trim
|
||||
|
||||
去除字符串开头和结尾的空格或指定字符。
|
||||
|
||||
## 签名
|
||||
|
||||
```typescript
|
||||
function trim(str: string, chars?: string | string[]): string;
|
||||
```
|
||||
|
||||
### 参数
|
||||
|
||||
- `str` (`string`): 要修剪字符的字符串。
|
||||
- `chars` (`string | string[]`): 要从字符串中删除的字符。可以是单个字符或字符数组。
|
||||
|
||||
### 返回值
|
||||
|
||||
(`string`): 删除指定字符后的结果字符串。
|
||||
|
||||
## 示例
|
||||
|
||||
```typescript
|
||||
trim(' hello '); // "hello"
|
||||
trim('--hello--', '-'); // "hello"
|
||||
trim('##hello##', ['#', 'o']); // "hell"
|
||||
```
|
27
docs/zh_hans/reference/string/trimEnd.md
Normal file
27
docs/zh_hans/reference/string/trimEnd.md
Normal file
@ -0,0 +1,27 @@
|
||||
# trimEnd
|
||||
|
||||
删除字符串末尾的空白或指定字符。
|
||||
|
||||
## 签名
|
||||
|
||||
```typescript
|
||||
function trimEnd(str: string, chars?: string | string[]): string;
|
||||
```
|
||||
|
||||
### 参数
|
||||
|
||||
- `str` (`string`): 要从中删除末尾字符的字符串。
|
||||
- `chars` (`string | string[]`): 要从字符串末尾删除的字符。
|
||||
|
||||
### 返回值
|
||||
|
||||
(`string`): 指定末尾字符被删除后的字符串。
|
||||
|
||||
## 示例
|
||||
|
||||
```typescript
|
||||
const trimmedStr1 = trimEnd('hello---', '-'); // returns 'hello'
|
||||
const trimmedStr2 = trimEnd('123000', '0'); // returns '123'
|
||||
const trimmedStr3 = trimEnd('abcabcabc', 'c'); // returns 'abcabcab'
|
||||
const trimmedStr4 = trimEnd('trimmedxxx', 'x'); // returns 'trimmed'
|
||||
```
|
27
docs/zh_hans/reference/string/trimStart.md
Normal file
27
docs/zh_hans/reference/string/trimStart.md
Normal file
@ -0,0 +1,27 @@
|
||||
# trimStart
|
||||
|
||||
移除字符串开头的空白字符或指定字符。
|
||||
|
||||
## 签名
|
||||
|
||||
```typescript
|
||||
function trimStart(str: string, chars?: string | string[]): string;
|
||||
```
|
||||
|
||||
### 参数
|
||||
|
||||
- `str` (`string`): 将要去除开头字符的字符串。
|
||||
- `chars` (`string | string[]`): 要从字符串结尾删除的字符。
|
||||
|
||||
### 返回值
|
||||
|
||||
(`string`): 移除指定开头字符后的结果字符串。
|
||||
|
||||
## 示例
|
||||
|
||||
```typescript
|
||||
const trimmedStr1 = trimStart('---hello', '-'); // returns 'hello'
|
||||
const trimmedStr2 = trimStart('000123', '0'); // returns '123'
|
||||
const trimmedStr3 = trimStart('abcabcabc', 'a'); // returns 'bcabcabc'
|
||||
const trimmedStr4 = trimStart('xxxtrimmed', 'x'); // returns 'trimmed'
|
||||
```
|
30
src/compat/_internal/whitespace.ts
Normal file
30
src/compat/_internal/whitespace.ts
Normal file
@ -0,0 +1,30 @@
|
||||
export const whitespace = [
|
||||
' ',
|
||||
'\t',
|
||||
'\x0b',
|
||||
'\f',
|
||||
'\xa0',
|
||||
'\ufeff',
|
||||
'\n',
|
||||
'\r',
|
||||
'\u2028',
|
||||
'\u2029',
|
||||
'\u1680',
|
||||
'\u180e',
|
||||
'\u2000',
|
||||
'\u2001',
|
||||
'\u2002',
|
||||
'\u2003',
|
||||
'\u2004',
|
||||
'\u2005',
|
||||
'\u2006',
|
||||
'\u2007',
|
||||
'\u2008',
|
||||
'\u2009',
|
||||
'\u200a',
|
||||
'\u202f',
|
||||
'\u205f',
|
||||
'\u3000',
|
||||
]
|
||||
.filter(chr => /\s/.exec(chr))
|
||||
.join('');
|
@ -88,6 +88,9 @@ export { endsWith } from './string/endsWith.ts';
|
||||
export { padStart } from './string/padStart.ts';
|
||||
export { padEnd } from './string/padEnd.ts';
|
||||
export { repeat } from './string/repeat.ts';
|
||||
export { trim } from './string/trim.ts';
|
||||
export { trimStart } from './string/trimStart.ts';
|
||||
export { trimEnd } from './string/trimEnd.ts';
|
||||
|
||||
export { max } from './math/max.ts';
|
||||
export { min } from './math/min.ts';
|
||||
|
72
src/compat/string/trim.spec.ts
Normal file
72
src/compat/string/trim.spec.ts
Normal file
@ -0,0 +1,72 @@
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import { trimEnd } from './trimEnd';
|
||||
import { whitespace } from '../_internal/whitespace';
|
||||
|
||||
describe('trimEnd', () => {
|
||||
const func = trimEnd;
|
||||
|
||||
it(`\`trimEnd\` should remove trailing whitespace`, () => {
|
||||
const string = `${whitespace}a b c${whitespace}`;
|
||||
const expected = `${whitespace}a b c`;
|
||||
|
||||
expect(func(string)).toBe(expected);
|
||||
});
|
||||
|
||||
it(`\`trimEnd\` should coerce \`string\` to a string`, () => {
|
||||
const object = {
|
||||
toString: () => `${whitespace}a b c${whitespace}`,
|
||||
};
|
||||
const expected = `${whitespace}a b c`;
|
||||
|
||||
// @ts-ignore
|
||||
expect(func(object)).toBe(expected);
|
||||
});
|
||||
|
||||
it(`\`trimEnd\` should remove trailing \`chars\``, () => {
|
||||
const string = '-_-a-b-c-_-';
|
||||
const expected = `${'-_-'}a-b-c`;
|
||||
|
||||
expect(func(string, '_-')).toBe(expected);
|
||||
});
|
||||
|
||||
it(`\`trimEnd\` should coerce \`chars\` to a string`, () => {
|
||||
const object = { toString: () => '_-' };
|
||||
const string = '-_-a-b-c-_-';
|
||||
const expected = `${'-_-'}a-b-c`;
|
||||
|
||||
// @ts-ignore
|
||||
expect(func(string, object)).toBe(expected);
|
||||
});
|
||||
|
||||
it(`\`trimEnd\` should return an empty string for empty values and \`chars\``, () => {
|
||||
[null, '_-'].forEach(chars => {
|
||||
// eslint-disable-next-line
|
||||
// @ts-ignore
|
||||
expect(func(null, chars)).toBe('');
|
||||
// eslint-disable-next-line
|
||||
// @ts-ignore
|
||||
expect(func(undefined, chars)).toBe('');
|
||||
// eslint-disable-next-line
|
||||
// @ts-ignore
|
||||
expect(func('', chars)).toBe('');
|
||||
});
|
||||
});
|
||||
|
||||
it(`\`trimEnd\` should work with \`undefined\` or empty string values for \`chars\``, () => {
|
||||
const string = `${whitespace}a b c${whitespace}`;
|
||||
const expected = `${whitespace}a b c`;
|
||||
|
||||
expect(func(string, undefined)).toBe(expected);
|
||||
expect(func(string, '')).toBe(string);
|
||||
});
|
||||
|
||||
it(`\`trimEnd\` should work as an iteratee for methods like \`_.map\``, () => {
|
||||
const string = Object(`${whitespace}a b c${whitespace}`);
|
||||
const trimmed = `${whitespace}a b c`;
|
||||
// eslint-disable-next-line
|
||||
// @ts-ignore
|
||||
const actual = [string, string, string].map(func);
|
||||
|
||||
expect(actual).toEqual([trimmed, trimmed, trimmed]);
|
||||
});
|
||||
});
|
39
src/compat/string/trim.ts
Normal file
39
src/compat/string/trim.ts
Normal file
@ -0,0 +1,39 @@
|
||||
import { trim as trimToolkit } from '../../string/trim.ts';
|
||||
|
||||
/**
|
||||
* Removes leading and trailing whitespace or specified characters from a string.
|
||||
*
|
||||
* @param {string} str - The string from which leading and trailing characters will be trimmed.
|
||||
* @param {string | string[]} chars - The character(s) to remove from the end of the string. Defaults to `" "`.
|
||||
* @returns {string} - The resulting string after the specified leading and trailing characters have been removed.
|
||||
*
|
||||
* @example
|
||||
* trim(" hello "); // "hello"
|
||||
* trim("--hello--", "-"); // "hello"
|
||||
* trim("##hello##", ["#", "o"]); // "hell"
|
||||
*/
|
||||
export function trim(str: string, chars?: string | string[], guard?: unknown): string {
|
||||
if (str == null) {
|
||||
return '';
|
||||
}
|
||||
|
||||
if (guard != null || chars == null) {
|
||||
return str.toString().trim();
|
||||
}
|
||||
|
||||
switch (typeof chars) {
|
||||
case 'string': {
|
||||
return trimToolkit(str, chars.toString().split(''));
|
||||
}
|
||||
case 'object': {
|
||||
if (Array.isArray(chars)) {
|
||||
return trimToolkit(
|
||||
str,
|
||||
chars.map(x => x.toString())
|
||||
);
|
||||
} else {
|
||||
return trimToolkit(str, (chars as any).toString().split(''));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
72
src/compat/string/trimEnd.spec.ts
Normal file
72
src/compat/string/trimEnd.spec.ts
Normal file
@ -0,0 +1,72 @@
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import { trimEnd } from './trimEnd';
|
||||
import { whitespace } from '../_internal/whitespace';
|
||||
|
||||
describe('trimEnd', () => {
|
||||
const func = trimEnd;
|
||||
|
||||
it(`\`trimEnd\` should remove trailing whitespace`, () => {
|
||||
const string = `${whitespace}a b c${whitespace}`;
|
||||
const expected = `${whitespace}a b c`;
|
||||
|
||||
expect(func(string)).toBe(expected);
|
||||
});
|
||||
|
||||
it(`\`trimEnd\` should coerce \`string\` to a string`, () => {
|
||||
const object = {
|
||||
toString: () => `${whitespace}a b c${whitespace}`,
|
||||
};
|
||||
const expected = `${whitespace}a b c`;
|
||||
|
||||
// @ts-ignore
|
||||
expect(func(object)).toBe(expected);
|
||||
});
|
||||
|
||||
it(`\`trimEnd\` should remove trailing \`chars\``, () => {
|
||||
const string = '-_-a-b-c-_-';
|
||||
const expected = `${'-_-'}a-b-c`;
|
||||
|
||||
expect(func(string, '_-')).toBe(expected);
|
||||
});
|
||||
|
||||
it(`\`trimEnd\` should coerce \`chars\` to a string`, () => {
|
||||
const object = { toString: () => '_-' };
|
||||
const string = '-_-a-b-c-_-';
|
||||
const expected = `${'-_-'}a-b-c`;
|
||||
|
||||
// @ts-ignore
|
||||
expect(func(string, object)).toBe(expected);
|
||||
});
|
||||
|
||||
it(`\`trimEnd\` should return an empty string for empty values and \`chars\``, () => {
|
||||
[null, '_-'].forEach(chars => {
|
||||
// eslint-disable-next-line
|
||||
// @ts-ignore
|
||||
expect(func(null, chars)).toBe('');
|
||||
// eslint-disable-next-line
|
||||
// @ts-ignore
|
||||
expect(func(undefined, chars)).toBe('');
|
||||
// eslint-disable-next-line
|
||||
// @ts-ignore
|
||||
expect(func('', chars)).toBe('');
|
||||
});
|
||||
});
|
||||
|
||||
it(`\`trimEnd\` should work with \`undefined\` or empty string values for \`chars\``, () => {
|
||||
const string = `${whitespace}a b c${whitespace}`;
|
||||
const expected = `${whitespace}a b c`;
|
||||
|
||||
expect(func(string, undefined)).toBe(expected);
|
||||
expect(func(string, '')).toBe(string);
|
||||
});
|
||||
|
||||
it(`\`trimEnd\` should work as an iteratee for methods like \`_.map\``, () => {
|
||||
const string = Object(`${whitespace}a b c${whitespace}`);
|
||||
const trimmed = `${whitespace}a b c`;
|
||||
// eslint-disable-next-line
|
||||
// @ts-ignore
|
||||
const actual = [string, string, string].map(func);
|
||||
|
||||
expect(actual).toEqual([trimmed, trimmed, trimmed]);
|
||||
});
|
||||
});
|
40
src/compat/string/trimEnd.ts
Normal file
40
src/compat/string/trimEnd.ts
Normal file
@ -0,0 +1,40 @@
|
||||
import { trimEnd as trimEndToolkit } from '../../string/trimEnd.ts';
|
||||
|
||||
/**
|
||||
* Removes trailing whitespace or specified characters from a string.
|
||||
*
|
||||
* @param {string} str - The string from which trailing characters will be trimmed.
|
||||
* @param {string | string[]} chars - The character(s) to remove from the end of the string. Defaults to `" "`.
|
||||
* @returns {string} - The resulting string after the specified trailing character has been removed.
|
||||
*
|
||||
* @example
|
||||
* const trimmedStr1 = trimEnd('hello---', '-') // returns 'hello'
|
||||
* const trimmedStr2 = trimEnd('123000', '0') // returns '123'
|
||||
* const trimmedStr3 = trimEnd('abcabcabc', 'c') // returns 'abcabcab'
|
||||
* const trimmedStr4 = trimEnd('trimmedxxx', 'x') // returns 'trimmed'
|
||||
*/
|
||||
export function trimEnd(str: string, chars?: string | string[], guard?: unknown): string {
|
||||
if (str == null) {
|
||||
return '';
|
||||
}
|
||||
|
||||
if (guard != null || chars == null) {
|
||||
return str.toString().trimEnd();
|
||||
}
|
||||
|
||||
switch (typeof chars) {
|
||||
case 'string': {
|
||||
return trimEndToolkit(str, chars.toString().split(''));
|
||||
}
|
||||
case 'object': {
|
||||
if (Array.isArray(chars)) {
|
||||
return trimEndToolkit(
|
||||
str,
|
||||
chars.map(x => x.toString())
|
||||
);
|
||||
} else {
|
||||
return trimEndToolkit(str, (chars as any).toString().split(''));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
72
src/compat/string/trimStart.spec.ts
Normal file
72
src/compat/string/trimStart.spec.ts
Normal file
@ -0,0 +1,72 @@
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import { whitespace } from '../_internal/whitespace';
|
||||
import { trimStart } from './trimStart';
|
||||
|
||||
describe('trimStart', () => {
|
||||
const func = trimStart;
|
||||
|
||||
it(`\`trimStart\` should remove leading whitespace`, () => {
|
||||
const string = `${whitespace}a b c${whitespace}`;
|
||||
const expected = `a b c${whitespace}`;
|
||||
|
||||
expect(func(string)).toBe(expected);
|
||||
});
|
||||
|
||||
it(`\`trimStart\` should coerce \`string\` to a string`, () => {
|
||||
const object = {
|
||||
toString: () => `${whitespace}a b c${whitespace}`,
|
||||
};
|
||||
const expected = `a b c${whitespace}`;
|
||||
|
||||
// @ts-ignore
|
||||
expect(func(object)).toBe(expected);
|
||||
});
|
||||
|
||||
it(`\`trimStart\` should remove leading \`chars\``, () => {
|
||||
const string = '-_-a-b-c-_-';
|
||||
const expected = `a-b-c${'-_-'}`;
|
||||
|
||||
expect(func(string, '_-')).toBe(expected);
|
||||
});
|
||||
|
||||
it(`\`trimStart\` should coerce \`chars\` to a string`, () => {
|
||||
const object = { toString: () => '_-' };
|
||||
const string = '-_-a-b-c-_-';
|
||||
const expected = `a-b-c${'-_-'}`;
|
||||
|
||||
// @ts-ignore
|
||||
expect(func(string, object)).toBe(expected);
|
||||
});
|
||||
|
||||
it(`\`trimStart\` should return an empty string for empty values and \`chars\``, () => {
|
||||
[null, '_-'].forEach(chars => {
|
||||
// eslint-disable-next-line
|
||||
// @ts-ignore
|
||||
expect(func(null, chars)).toBe('');
|
||||
// eslint-disable-next-line
|
||||
// @ts-ignore
|
||||
expect(func(undefined, chars)).toBe('');
|
||||
// eslint-disable-next-line
|
||||
// @ts-ignore
|
||||
expect(func('', chars)).toBe('');
|
||||
});
|
||||
});
|
||||
|
||||
it(`\`trimStart\` should work with \`undefined\` or empty string values for \`chars\``, () => {
|
||||
const string = `${whitespace}a b c${whitespace}`;
|
||||
const expected = `a b c${whitespace}`;
|
||||
|
||||
expect(func(string, undefined)).toBe(expected);
|
||||
expect(func(string, '')).toBe(string);
|
||||
});
|
||||
|
||||
it(`\`trimStart\` should work as an iteratee for methods like \`_.map\``, () => {
|
||||
const string = Object(`${whitespace}a b c${whitespace}`);
|
||||
const trimmed = `a b c${whitespace}`;
|
||||
// eslint-disable-next-line
|
||||
// @ts-ignore
|
||||
const actual = [string, string, string].map(func);
|
||||
|
||||
expect(actual).toEqual([trimmed, trimmed, trimmed]);
|
||||
});
|
||||
});
|
40
src/compat/string/trimStart.ts
Normal file
40
src/compat/string/trimStart.ts
Normal file
@ -0,0 +1,40 @@
|
||||
import { trimStart as trimStartToolkit } from '../../string/trimStart.ts';
|
||||
|
||||
/**
|
||||
* Removes leading whitespace or specified characters from a string.
|
||||
*
|
||||
* @param {string} str - The string from which leading characters will be trimmed.
|
||||
* @param {string | string[]} chars - The character(s) to remove from the end of the string. Defaults to `" "`.
|
||||
* @returns {string} - The resulting string after the specified leading character has been removed.
|
||||
*
|
||||
* @example
|
||||
* const trimmedStr1 = ltrim('---hello', '-') // returns 'hello'
|
||||
* const trimmedStr2 = ltrim('000123', '0') // returns '123'
|
||||
* const trimmedStr3 = ltrim('abcabcabc', 'a') // returns 'bcabcabc'
|
||||
* const trimmedStr4 = ltrim('xxxtrimmed', 'x') // returns 'trimmed'
|
||||
*/
|
||||
export function trimStart(str: string, chars?: string | string[], guard?: unknown): string {
|
||||
if (str == null) {
|
||||
return '';
|
||||
}
|
||||
|
||||
if (guard != null || chars == null) {
|
||||
return str.toString().trimStart();
|
||||
}
|
||||
|
||||
switch (typeof chars) {
|
||||
case 'string': {
|
||||
return trimStartToolkit(str, chars.toString().split(''));
|
||||
}
|
||||
case 'object': {
|
||||
if (Array.isArray(chars)) {
|
||||
return trimStartToolkit(
|
||||
str,
|
||||
chars.map(x => x.toString())
|
||||
);
|
||||
} else {
|
||||
return trimStartToolkit(str, (chars as any).toString().split(''));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -7,8 +7,8 @@ export { startCase } from './startCase.ts';
|
||||
export { capitalize } from './capitalize.ts';
|
||||
export { pascalCase } from './pascalCase.ts';
|
||||
export { trim } from './trim.ts';
|
||||
export { ltrim } from './ltrim.ts';
|
||||
export { rtrim } from './rtrim.ts';
|
||||
export { trimStart } from './trimStart.ts';
|
||||
export { trimEnd } from './trimEnd.ts';
|
||||
export { upperFirst } from './upperFirst.ts';
|
||||
export { lowerFirst } from './lowerFirst.ts';
|
||||
export { deburr } from './deburr.ts';
|
||||
|
@ -1,27 +0,0 @@
|
||||
/**
|
||||
* Trims specific characters from the start of a string.
|
||||
*
|
||||
* This function removes all leading occurrences of the specified character from the input string.
|
||||
* Only the characters at the beginning of the string will be removed.
|
||||
*
|
||||
* @param {string} str - The string from which leading characters will be trimmed.
|
||||
* @param {string} toTrim - The character to remove from the start of the string.
|
||||
* @returns {string} - The resulting string after the specified leading character has been removed.
|
||||
*
|
||||
* @example
|
||||
* const trimmedStr1 = ltrim('---hello', '-') // returns 'hello'
|
||||
* const trimmedStr2 = ltrim('000123', '0') // returns '123'
|
||||
* const trimmedStr3 = ltrim('abcabcabc', 'a') // returns 'bcabcabc'
|
||||
* const trimmedStr4 = ltrim('xxxtrimmed', 'x') // returns 'trimmed'
|
||||
*/
|
||||
|
||||
import { TrimParameter } from './trim.ts';
|
||||
|
||||
export const ltrim = (str: string, toTrim: TrimParameter): string => {
|
||||
const chars: string[] = str.split('');
|
||||
let startHere = 0;
|
||||
while ((chars[startHere] === toTrim || toTrim.includes(chars[startHere])) && startHere < chars.length) {
|
||||
startHere++;
|
||||
}
|
||||
return chars.splice(startHere, chars.length).join('');
|
||||
};
|
@ -1,33 +0,0 @@
|
||||
/**
|
||||
* Trims specific characters from the end of a string.
|
||||
*
|
||||
* This function removes all trailing occurrences of the specified character from the input string.
|
||||
* Only the characters at the end of the string will be removed.
|
||||
*
|
||||
* @param {string} str - The string from which trailing characters will be trimmed.
|
||||
* @param {string} toTrim - The character to remove from the end of the string.
|
||||
* @returns {string} - The resulting string after the specified trailing character has been removed.
|
||||
*
|
||||
* @example
|
||||
* const trimmedStr1 = rtrim('hello---', '-') // returns 'hello'
|
||||
* const trimmedStr2 = rtrim('123000', '0') // returns '123'
|
||||
* const trimmedStr3 = rtrim('abcabcabc', 'c') // returns 'abcabcab'
|
||||
* const trimmedStr4 = rtrim('trimmedxxx', 'x') // returns 'trimmed'
|
||||
*/
|
||||
|
||||
import { TrimParameter } from './trim.ts';
|
||||
|
||||
export const rtrim = (str: string, toTrim: TrimParameter): string => {
|
||||
const chars: string[] = str.split('');
|
||||
|
||||
while (chars.length > 0) {
|
||||
const lastChar = chars[chars.length - 1];
|
||||
if (typeof toTrim === 'string' ? lastChar === toTrim : toTrim.includes(lastChar)) {
|
||||
chars.pop();
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return chars.join('');
|
||||
};
|
@ -1,24 +1,22 @@
|
||||
import { trimStart } from './trimStart.ts';
|
||||
import { trimEnd } from './trimEnd.ts';
|
||||
|
||||
/**
|
||||
* Trims specific characters from a string.
|
||||
*
|
||||
* This function removes all occurrences of the specified characters from the input string.
|
||||
* The characters to be removed can be provided as a single character or an array of characters.
|
||||
* Removes leading and trailing whitespace or specified characters from a string.
|
||||
*
|
||||
* @param {string} str - The string from which characters will be trimmed.
|
||||
* @param {TrimParameter} toTrim - The character(s) to remove from the string. Can be a single character or an array of characters.
|
||||
* @param {string | string[]} chars - The character(s) to remove from the string. Can be a single character or an array of characters.
|
||||
* @returns {string} - The resulting string after the specified characters have been removed.
|
||||
*
|
||||
* @example
|
||||
* const trimmedStr1 = trim('hello world', 'l') // returns 'heo word'
|
||||
* const trimmedStr2 = trim('hello world', ['l', 'o']) // returns 'he wrd'
|
||||
* const trimmedStr3 = trim('abcabcabc', 'b') // returns 'acacac'
|
||||
* const trimmedStr4 = trim('123-456-789', ['-', '3']) // returns '12456789'
|
||||
* trim(" hello "); // "hello"
|
||||
* trim("--hello--", "-"); // "hello"
|
||||
* trim("##hello##", ["#", "o"]); // "hell"
|
||||
*/
|
||||
import { ltrim } from './ltrim.ts';
|
||||
import { rtrim } from './rtrim.ts';
|
||||
export function trim(str: string, chars?: string | string[]): string {
|
||||
if (chars == null) {
|
||||
return str.trim();
|
||||
}
|
||||
|
||||
export type TrimParameter = string | string[];
|
||||
|
||||
export const trim = (str: string, toTrim: TrimParameter): string => {
|
||||
return ltrim(rtrim(str, toTrim), toTrim);
|
||||
};
|
||||
return trimStart(trimEnd(str, chars), chars);
|
||||
}
|
||||
|
@ -1,44 +1,44 @@
|
||||
import { describe, it, expect } from 'vitest';
|
||||
import { rtrim } from './rtrim.ts';
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import { trimEnd } from './trimEnd.ts';
|
||||
|
||||
describe('rtrim', () => {
|
||||
describe('trimEnd', () => {
|
||||
it('should remove trailing characters from the string', async () => {
|
||||
expect(rtrim('hello---', '-')).toEqual('hello');
|
||||
expect(trimEnd('hello---', '-')).toEqual('hello');
|
||||
});
|
||||
|
||||
it('should remove trailing characters when multiple characters are provided', async () => {
|
||||
expect(rtrim('123000', '0')).toEqual('123');
|
||||
expect(trimEnd('123000', '0')).toEqual('123');
|
||||
});
|
||||
|
||||
it('should return the string unchanged when there are no trailing characters to remove', async () => {
|
||||
expect(rtrim('hello', 'x')).toEqual('hello');
|
||||
expect(trimEnd('hello', 'x')).toEqual('hello');
|
||||
});
|
||||
|
||||
it('should remove trailing occurrences of a single character', async () => {
|
||||
expect(rtrim('abcabcabc', 'c')).toEqual('abcabcab');
|
||||
expect(trimEnd('abcabcabc', 'c')).toEqual('abcabcab');
|
||||
});
|
||||
|
||||
it('should handle an empty string', async () => {
|
||||
expect(rtrim('', 'x')).toEqual('');
|
||||
expect(trimEnd('', 'x')).toEqual('');
|
||||
});
|
||||
|
||||
it('should remove trailing spaces when specified', async () => {
|
||||
expect(rtrim('hello world ', ' ')).toEqual('hello world');
|
||||
expect(trimEnd('hello world ', ' ')).toEqual('hello world');
|
||||
});
|
||||
|
||||
it('should handle a case where the string is already trimmed', async () => {
|
||||
expect(rtrim('trimmed', 'x')).toEqual('trimmed');
|
||||
expect(trimEnd('trimmed', 'x')).toEqual('trimmed');
|
||||
});
|
||||
|
||||
it('should return an empty string when all characters are removed', async () => {
|
||||
expect(rtrim('xxxxx', 'x')).toEqual('');
|
||||
expect(trimEnd('xxxxx', 'x')).toEqual('');
|
||||
});
|
||||
|
||||
it('should remove numbers from the end of a string', async () => {
|
||||
expect(rtrim('abc123456', '6')).toEqual('abc12345');
|
||||
expect(trimEnd('abc123456', '6')).toEqual('abc12345');
|
||||
});
|
||||
|
||||
it('should handle cases where multiple trailing characters need removal', async () => {
|
||||
expect(rtrim('abc123abc123abc', 'c')).toEqual('abc123abc123ab');
|
||||
expect(trimEnd('abc123abc123abc', 'c')).toEqual('abc123abc123ab');
|
||||
});
|
||||
});
|
36
src/string/trimEnd.ts
Normal file
36
src/string/trimEnd.ts
Normal file
@ -0,0 +1,36 @@
|
||||
/**
|
||||
* Removes trailing whitespace or specified characters from a string.
|
||||
*
|
||||
* @param {string} str - The string from which trailing characters will be trimmed.
|
||||
* @param {string | string[]} chars - The character(s) to remove from the end of the string.
|
||||
* @returns {string} - The resulting string after the specified trailing character has been removed.
|
||||
*
|
||||
* @example
|
||||
* const trimmedStr1 = trimEnd('hello---', '-') // returns 'hello'
|
||||
* const trimmedStr2 = trimEnd('123000', '0') // returns '123'
|
||||
* const trimmedStr3 = trimEnd('abcabcabc', 'c') // returns 'abcabcab'
|
||||
* const trimmedStr4 = trimEnd('trimmedxxx', 'x') // returns 'trimmed'
|
||||
*/
|
||||
export function trimEnd(str: string, chars?: string | string[]): string {
|
||||
if (chars == null) {
|
||||
return str.trimEnd();
|
||||
}
|
||||
|
||||
let endIndex = str.length;
|
||||
|
||||
switch (typeof chars) {
|
||||
case 'string': {
|
||||
while (endIndex > 0 && str[endIndex - 1] === chars) {
|
||||
endIndex--;
|
||||
}
|
||||
break;
|
||||
}
|
||||
case 'object': {
|
||||
while (endIndex > 0 && chars.includes(str[endIndex - 1])) {
|
||||
endIndex--;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return str.substring(0, endIndex);
|
||||
}
|
@ -1,44 +1,44 @@
|
||||
import { describe, it, expect } from 'vitest';
|
||||
import { ltrim } from './ltrim.ts';
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import { trimStart } from './trimStart.ts';
|
||||
|
||||
describe('ltrim', () => {
|
||||
describe('trimStart', () => {
|
||||
it('should remove leading characters from the string', async () => {
|
||||
expect(ltrim('---hello', '-')).toEqual('hello');
|
||||
expect(trimStart('---hello', '-')).toEqual('hello');
|
||||
});
|
||||
|
||||
it('should remove leading zeros from the string', async () => {
|
||||
expect(ltrim('000123', '0')).toEqual('123');
|
||||
expect(trimStart('000123', '0')).toEqual('123');
|
||||
});
|
||||
|
||||
it('should return the string unchanged when there are no leading characters to remove', async () => {
|
||||
expect(ltrim('hello', 'x')).toEqual('hello');
|
||||
expect(trimStart('hello', 'x')).toEqual('hello');
|
||||
});
|
||||
|
||||
it('should remove leading occurrences of a single character', async () => {
|
||||
expect(ltrim('abcabcabc', 'a')).toEqual('bcabcabc');
|
||||
expect(trimStart('abcabcabc', 'a')).toEqual('bcabcabc');
|
||||
});
|
||||
|
||||
it('should handle an empty string', async () => {
|
||||
expect(ltrim('', 'x')).toEqual('');
|
||||
expect(trimStart('', 'x')).toEqual('');
|
||||
});
|
||||
|
||||
it('should remove leading spaces when specified', async () => {
|
||||
expect(ltrim(' hello world', ' ')).toEqual('hello world');
|
||||
expect(trimStart(' hello world', ' ')).toEqual('hello world');
|
||||
});
|
||||
|
||||
it('should handle a case where the string is already trimmed', async () => {
|
||||
expect(ltrim('trimmed', 'x')).toEqual('trimmed');
|
||||
expect(trimStart('trimmed', 'x')).toEqual('trimmed');
|
||||
});
|
||||
|
||||
it('should return an empty string when all characters are removed', async () => {
|
||||
expect(ltrim('xxxxx', 'x')).toEqual('');
|
||||
expect(trimStart('xxxxx', 'x')).toEqual('');
|
||||
});
|
||||
|
||||
it('should remove numbers from the start of a string', async () => {
|
||||
expect(ltrim('123456abc', '1')).toEqual('23456abc');
|
||||
expect(trimStart('123456abc', '1')).toEqual('23456abc');
|
||||
});
|
||||
|
||||
it('should handle cases where multiple leading characters need removal', async () => {
|
||||
expect(ltrim('aaaabbbcccc', 'a')).toEqual('bbbcccc');
|
||||
expect(trimStart('aaaabbbcccc', 'a')).toEqual('bbbcccc');
|
||||
});
|
||||
});
|
35
src/string/trimStart.ts
Normal file
35
src/string/trimStart.ts
Normal file
@ -0,0 +1,35 @@
|
||||
/**
|
||||
* Removes leading whitespace or specified characters from a string.
|
||||
*
|
||||
* @param {string} str - The string from which leading characters will be trimmed.
|
||||
* @param {string | string[]} chars - The character(s) to remove from the end of the string.
|
||||
* @returns {string} - The resulting string after the specified leading character has been removed.
|
||||
*
|
||||
* @example
|
||||
* const trimmedStr1 = trimStart('---hello', '-') // returns 'hello'
|
||||
* const trimmedStr2 = trimStart('000123', '0') // returns '123'
|
||||
* const trimmedStr3 = trimStart('abcabcabc', 'a') // returns 'bcabcabc'
|
||||
* const trimmedStr4 = trimStart('xxxtrimmed', 'x') // returns 'trimmed'
|
||||
*/
|
||||
export function trimStart(str: string, chars?: string | string[]): string {
|
||||
if (chars == null) {
|
||||
return str.trimStart();
|
||||
}
|
||||
let startIndex = 0;
|
||||
|
||||
switch (typeof chars) {
|
||||
case 'string': {
|
||||
while (startIndex < str.length && str[startIndex] === chars) {
|
||||
startIndex++;
|
||||
}
|
||||
break;
|
||||
}
|
||||
case 'object': {
|
||||
while (startIndex < str.length && chars.includes(str[startIndex])) {
|
||||
startIndex++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return str.substring(startIndex);
|
||||
}
|
Loading…
Reference in New Issue
Block a user