-
Notifications
You must be signed in to change notification settings - Fork 1
/
strTrim.test.ts
107 lines (91 loc) · 6.13 KB
/
strTrim.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import { describe, it, expect } from 'vitest';
import strTrim, { STR_TRIM_BOTH, STR_TRIM_LEFT, STR_TRIM_RIGHT } from '../../src/strTrim.js';
describe('str/strTrim', () => {
it('can trim the left side (start)', () => {
expect(_trimLeft('Hello World')).toEqual('Hello World');
expect(_trimLeft(' foo ')).toEqual('foo ');
expect(_trimLeft(' ___foo__ ', '_')).toEqual(' ___foo__ ');
expect(_trimLeft('___foo__ ', '_')).toEqual('foo__ ');
expect(_trimLeft('___foo__ ', '__')).toEqual('foo__ ');
expect(_trimLeft('___foo__ ', '___')).toEqual('foo__ ');
expect(_trimLeft('___foo__ ', '____')).toEqual('___foo__ ');
expect(_trimLeft(' ___foo__ ', [' ', '_'])).toEqual('foo__ ');
expect(_trimLeft(' ___foo__ ', ['_', ' '])).toEqual('foo__ ');
expect(_trimLeft(' ___foo__', '_')).toEqual(' ___foo__');
expect(_trimLeft(' ___foo__', '__')).toEqual(' ___foo__');
expect(_trimLeft(' ___foo__', '___')).toEqual(' ___foo__');
expect(_trimLeft(' ___foo__', '____')).toEqual(' ___foo__');
expect(_trimLeft('\t\tThese are a few words :) ... ')).toEqual('These are a few words :) ... ');
expect(_trimLeft('\t\tThese are a few words :) ... ', ' ')).toEqual('\t\tThese are a few words :) ... ');
expect(_trimLeft('\t\tThese are a few words :) ... ', '\t\t')).toEqual('These are a few words :) ... ');
expect(_trimLeft('\t\tThese are a few words :) ... ', [' '])).toEqual('\t\tThese are a few words :) ... ');
expect(_trimLeft('\t\tThese are a few words :) ... ', ['\t\t'])).toEqual('These are a few words :) ... ');
expect(_trimLeft('\t\tThese are a few words :) ... ', [' ', '\t'])).toEqual('These are a few words :) ... ');
expect(_trimLeft('\t\tThese are a few words :) ... ', [' ', '\t', '123'])).toEqual(
'These are a few words :) ... '
);
expect(_trimLeft('\x09Example string\x0A')).toEqual('Example string\x0A');
});
it('can trim the right side (end)', () => {
expect(_trimRight('Hello World')).toEqual('Hello World');
expect(_trimRight(' foo ')).toEqual(' foo');
expect(_trimRight(' ___foo__ ', '_')).toEqual(' ___foo__ ');
expect(_trimRight(' ___foo__', '_')).toEqual(' ___foo');
expect(_trimRight(' ___foo__', '__')).toEqual(' ___foo');
expect(_trimRight(' ___foo__', '___')).toEqual(' ___foo__');
expect(_trimRight(' ___foo__', '____')).toEqual(' ___foo__');
expect(_trimRight(' ___foo__ ', [' ', '_'])).toEqual(' ___foo');
expect(_trimRight(' ___foo__ ', ['_', ' '])).toEqual(' ___foo');
expect(_trimRight('___foo__ ', '_')).toEqual('___foo__ ');
expect(_trimRight('___foo__ ', '__')).toEqual('___foo__ ');
expect(_trimRight('___foo__ ', '___')).toEqual('___foo__ ');
expect(_trimRight('___foo__ ', '____')).toEqual('___foo__ ');
expect(_trimRight(' These are a few words :) ...\t\t')).toEqual(' These are a few words :) ...');
expect(_trimRight(' These are a few words :) ...\t\t', ' ')).toEqual(' These are a few words :) ...\t\t');
expect(_trimRight(' These are a few words :) ...\t\t', '\t\t')).toEqual(' These are a few words :) ...');
expect(_trimRight(' These are a few words :) ...\t\t', [' '])).toEqual(' These are a few words :) ...\t\t');
expect(_trimRight(' These are a few words :) ...\t\t', ['\t\t'])).toEqual(' These are a few words :) ...');
expect(_trimRight(' These are a few words :) ...\t\t', [' ', '\t'])).toEqual(' These are a few words :) ...');
expect(_trimRight(' These are a few words :) ...\t\t', [' ', '\t', '123'])).toEqual(
' These are a few words :) ...'
);
expect(_trimRight('\x09Example string\x0A')).toEqual('\x09Example string');
});
it('can trim both sides', () => {
expect(_trimBoth('Hello World')).toEqual('Hello World');
expect(_trimBoth(' foo ')).toEqual('foo');
expect(_trimBoth(' ___foo__ ', '_')).toEqual(' ___foo__ ');
expect(_trimBoth(' ___foo__ ', '_')).toEqual(' ___foo__ ');
expect(_trimBoth(' ___foo__ ', '__')).toEqual(' ___foo__ ');
expect(_trimBoth(' ___foo__ ', '___')).toEqual(' ___foo__ ');
expect(_trimBoth(' ___foo__ ', '____')).toEqual(' ___foo__ ');
expect(_trimBoth(' ___foo__ ', [' ', '_'])).toEqual('foo');
expect(_trimBoth(' ___foo__ ', ['_', ' '])).toEqual('foo');
expect(_trimBoth('___foo__', '_')).toEqual('foo');
expect(_trimBoth('___foo__', '__')).toEqual('foo');
expect(_trimBoth('___foo__', '___')).toEqual('foo__');
expect(_trimBoth('___foo__', '____')).toEqual('___foo__');
expect(_trimBoth('\t\tThese are a few words :) ... ')).toEqual('These are a few words :) ...');
expect(_trimBoth('\t\tThese are a few words :) ... ', ' ')).toEqual('\t\tThese are a few words :) ...');
expect(_trimBoth('\t\tThese are a few words :) ... ', '\t\t')).toEqual('These are a few words :) ... ');
expect(_trimBoth('\t\tThese are a few words :) ... ', [' '])).toEqual('\t\tThese are a few words :) ...');
expect(_trimBoth('\t\tThese are a few words :) ... ', ['\t\t'])).toEqual('These are a few words :) ... ');
expect(_trimBoth('\t\tThese are a few words :) ... ', [' ', '\t'])).toEqual('These are a few words :) ...');
expect(_trimBoth('\t\tThese are a few words :) ... ', [' ', '\t', '123'])).toEqual(
'These are a few words :) ...'
);
expect(_trimBoth('\x09Example string\x0A')).toEqual('Example string');
});
});
// eslint-disable-next-line @typescript-eslint/no-explicit-any
function _trimLeft(value: string, characters: any = undefined): string {
return strTrim(value, characters, STR_TRIM_LEFT);
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
function _trimRight(value: string, characters: any = undefined): string {
return strTrim(value, characters, STR_TRIM_RIGHT);
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
function _trimBoth(value: string, characters: any = undefined): string {
return strTrim(value, characters, STR_TRIM_BOTH);
}