-
Notifications
You must be signed in to change notification settings - Fork 1
/
strReplaceMany.test.ts
150 lines (123 loc) · 7.6 KB
/
strReplaceMany.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import { describe, it, expect } from 'vitest';
import strReplaceMany from '../../src/strReplaceMany.js';
describe('str/strReplaceMany', () => {
it('works with string parameters', () => {
expect(strReplaceMany('o', '', 'Foobar')).toEqual('Fbar');
expect(strReplaceMany('bar', '', 'Foobar')).toEqual('Foo');
expect(strReplaceMany('F', '', 'Foobar')).toEqual('oobar');
expect(strReplaceMany('f', '', 'Foobar')).toEqual('Foobar');
expect(strReplaceMany('Bar', '', 'Foobar')).toEqual('Foobar');
expect(strReplaceMany('bAr', '', 'Foobar')).toEqual('Foobar');
expect(strReplaceMany('f', '', 'Foobar', false)).toEqual('oobar');
expect(strReplaceMany('Bar', '', 'Foobar', false)).toEqual('Foo');
expect(strReplaceMany('bAr', '', 'Foobar', false)).toEqual('Foo');
expect(strReplaceMany('o', '--', 'Foobar')).toEqual('F----bar');
expect(strReplaceMany('bar', '--', 'Foobar')).toEqual('Foo--');
expect(strReplaceMany('F', '--', 'Foobar')).toEqual('--oobar');
expect(strReplaceMany('f', '--', 'Foobar')).toEqual('Foobar');
expect(strReplaceMany('Bar', '--', 'Foobar')).toEqual('Foobar');
expect(strReplaceMany('bAr', '--', 'Foobar')).toEqual('Foobar');
expect(strReplaceMany('f', '--', 'Foobar', false)).toEqual('--oobar');
expect(strReplaceMany('Bar', '--', 'Foobar', false)).toEqual('Foo--');
expect(strReplaceMany('bAr', '--', 'Foobar', false)).toEqual('Foo--');
});
it('works with the `search` parameter being an array', () => {
expect(strReplaceMany(['o', 'a'], '', 'Foobar')).toEqual('Fbr');
expect(strReplaceMany(['f', 'b'], '', 'Foobar')).toEqual('Fooar');
expect(strReplaceMany(['f', 'b'], '', 'Foobar', false)).toEqual('ooar');
expect(strReplaceMany(['f', '|'], '', 'Foo|bar')).toEqual('Foobar');
expect(strReplaceMany(['o', 'a'], '*', 'Foobar')).toEqual('F**b*r');
expect(strReplaceMany(['f', 'b'], '*', 'Foobar')).toEqual('Foo*ar');
expect(strReplaceMany(['f', 'b'], '*', 'Foobar', false)).toEqual('*oo*ar');
expect(strReplaceMany(['f', '|'], '*', 'Foo|bar')).toEqual('Foo*bar');
expect(strReplaceMany([''], '*', 'hey')).toEqual('hey');
expect(strReplaceMany([' '], '👏', 'Black Lives Matter')).toEqual('Black👏Lives👏Matter');
});
it('works with the `replace` parameter being an array', () => {
expect(strReplaceMany('o', ['x', '-'], 'Foobar')).toEqual('Fxxbar');
expect(strReplaceMany('oo', ['x', '-'], 'Foobar')).toEqual('Fxbar');
expect(strReplaceMany('f', ['x', '-'], 'Foobar')).toEqual('Foobar');
expect(strReplaceMany('f', ['x', '-'], 'Foobar', false)).toEqual('xoobar');
expect(strReplaceMany('|', ['', '-'], 'Foo|bar')).toEqual('Foobar');
expect(strReplaceMany(['o', 'a'], ['x', '-'], 'Foobar')).toEqual('Fxxb-r');
expect(strReplaceMany(['f', 'b'], ['x', '-'], 'Foobar')).toEqual('Foo-ar');
expect(strReplaceMany(['f', 'b'], ['x', '-'], 'Foobar', false)).toEqual('xoo-ar');
expect(strReplaceMany(['f', '|'], ['x', '-'], 'Foo|bar')).toEqual('Foo-bar');
expect(strReplaceMany(['B', ' '], ['#B', ''], 'Black Lives Matter')).toEqual('#BlackLivesMatter');
});
it('works with the `subject` parameter being an array', () => {
expect(strReplaceMany('o', '', ['Foobar'])).toEqual(['Fbar']);
expect(strReplaceMany('oo', '', ['Foobar'])).toEqual(['Fbar']);
expect(strReplaceMany('o', '', ['Foobar', 'Loonar'])).toEqual(['Fbar', 'Lnar']);
expect(strReplaceMany('f', '', ['Foobar'])).toEqual(['Foobar']);
expect(strReplaceMany('f', '', ['Foobar'], false)).toEqual(['oobar']);
expect(strReplaceMany('|', '', ['Foo|bar'])).toEqual(['Foobar']);
expect(strReplaceMany('o', '', ['Foobar', 'Loonar'])).toEqual(['Fbar', 'Lnar']);
expect(strReplaceMany('o', ['x', '-'], ['Foobar'])).toEqual(['Fxxbar']);
expect(strReplaceMany('oo', ['x', '-'], ['Foobar'])).toEqual(['Fxbar']);
expect(strReplaceMany('o', ['x', '-'], ['Foobar', 'Loonar'])).toEqual(['Fxxbar', 'Lxxnar']);
expect(strReplaceMany('f', ['x', '-'], ['Foobar', 'Loonar'])).toEqual(['Foobar', 'Loonar']);
expect(strReplaceMany('f', ['x', '-'], ['Foobar', 'Loonar'], false)).toEqual(['xoobar', 'Loonar']);
expect(strReplaceMany('|', ['', '-'], ['Foo|bar', 'Loo|nar'])).toEqual(['Foobar', 'Loonar']);
expect(strReplaceMany('o', [''], ['Foobar', 'Loonar'])).toEqual(['Fbar', 'Lnar']);
expect(strReplaceMany(['o'], ['x', '-'], ['Foobar', 'Loonar'])).toEqual(['Fxxbar', 'Lxxnar']);
expect(strReplaceMany(['oo'], ['x', '-'], ['Foobar', 'Loonar'])).toEqual(['Fxbar', 'Lxnar']);
expect(strReplaceMany(['o', 'a'], ['x', '-'], ['Foobar', 'Loonar'])).toEqual(['Fxxb-r', 'Lxxn-r']);
expect(strReplaceMany(['f', 'l'], ['x', '-'], ['Foobar', 'Loonar'])).toEqual(['Foobar', 'Loonar']);
expect(strReplaceMany(['f', 'l'], ['x', '-'], ['Foobar', 'Loonar'], false)).toEqual(['xoobar', '-oonar']);
expect(strReplaceMany(['|', '-'], ['', 'p'], ['Foo|bar', 'Loo-nar'])).toEqual(['Foobar', 'Loopnar']);
expect(
//
strReplaceMany(
//
['Black', '!', 'Too'],
['All', '?', 'Exactly!'],
['Black Lives Matter!', 'Too']
)
).toEqual(['All Lives Matter?', 'Exactly!']);
});
it('returns an array when the `subject` parameter is an array', () => {
expect(strReplaceMany('o', '', 'Foobar')).toEqual('Fbar');
expect(strReplaceMany('o', '', ['Foobar'])).toEqual(['Fbar']);
});
it('returns `subject` if the `search` or `subject` parameters are empty', () => {
expect(strReplaceMany('', '*', 'hey')).toEqual('hey');
expect(strReplaceMany('', '*', 'hey', false)).toEqual('hey');
expect(strReplaceMany([], '*', 'hey')).toEqual('hey');
expect(strReplaceMany([''], '*', 'hey')).toEqual('hey');
expect(strReplaceMany([''], '*', 'hey', false)).toEqual('hey');
expect(strReplaceMany(['', 'a'], '*', 'hey')).toEqual('hey');
expect(strReplaceMany('hey', '*', '')).toEqual('');
expect(strReplaceMany('hey', '*', '', false)).toEqual('');
expect(strReplaceMany('hey', '*', [])).toEqual([]);
expect(strReplaceMany('hey', '*', [''])).toEqual(['']);
expect(strReplaceMany('hey', '*', [''], false)).toEqual(['']);
expect(strReplaceMany('hey', '*', ['', 'a'])).toEqual(['', 'a']);
});
it('can keeps a record of the amount of matches', () => {
const replacedCount = { value: 0 };
strReplaceMany('', '*', 'heey', true, replacedCount);
expect(replacedCount.value).toEqual(0);
strReplaceMany('e', '*', '', true, replacedCount);
expect(replacedCount.value).toEqual(0);
strReplaceMany('e', '*', 'heey', true, replacedCount);
expect(replacedCount.value).toEqual(2);
strReplaceMany('e', '*', 'heeey', true, replacedCount);
expect(replacedCount.value).toEqual(3);
strReplaceMany('e', '*', 'heEy', true, replacedCount);
expect(replacedCount.value).toEqual(1);
// array params
expect(strReplaceMany(['B', ' '], ['#B', ''], 'Black Lives Matter', true, replacedCount)).toEqual(
'#BlackLivesMatter'
);
expect(replacedCount.value).toEqual(3);
strReplaceMany(
['Black', '!', 'Too'],
['All', '?', 'Exactly!'],
['Black Lives Matter!', 'Too'],
true,
replacedCount
);
expect(replacedCount.value).toEqual(3);
});
});