-
Notifications
You must be signed in to change notification settings - Fork 0
/
StringLiteralList.js
260 lines (226 loc) · 5.26 KB
/
StringLiteralList.js
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
const freezeIfImmutable = (source, target) => {
if (source && target && Object.isFrozen(source)) {
/* c8 ignore next 3 @preserve */
return Object.freeze(target);
}
return target;
};
export class SL extends Array {
infered = {
Union: undefined,
Tuple: undefined,
Mutable: undefined,
Unsorted: undefined,
};
enum;
hasEmpty = false;
constructor(...args) {
const entries = [];
const arr = [];
let emptyFound = false;
for (const str of args.flat()) {
if (typeof str === 'string') {
if (str === '') {
emptyFound = true;
}
entries.push([str, str]);
arr.push(str);
}
}
super(...arr);
this.hasEmpty = emptyFound;
this.enum = Object.fromEntries(entries);
if (this.hasEmpty) {
this.enum[''] = '';
}
Object.freeze(this.enum);
}
concat(...args) {
return freezeIfImmutable(
this,
new SL(...super.concat.apply(this, args.flat())),
);
}
concatList(list) {
return this.concat(...list);
}
toSorted() {
return freezeIfImmutable(
this,
new SL(...super.toSorted.apply(this, arguments)),
);
}
toReversed() {
return freezeIfImmutable(
this,
new SL(...super.toReversed.apply(this, arguments)),
);
}
reverse() {
return freezeIfImmutable(
this,
new SL(...super.reverse.apply(this, arguments)),
);
}
toSpliced() {
return freezeIfImmutable(
this,
new SL(...super.toSpliced.apply(this, arguments)),
);
}
slice() {
return freezeIfImmutable(
this,
new SL(...super.slice.apply(this, arguments)),
);
}
without() {
const values = Array.from(arguments).flatMap((el) =>
Array.isArray(el)
? el.filter((s) => typeof s === 'string')
: typeof el === 'string'
? [el]
: [],
);
return freezeIfImmutable(
this,
new SL(...this.filter((e) => !values.includes(e))),
);
}
withTrim() {
return freezeIfImmutable(this, new SL(...super.map((e) => e.trim())));
}
withPrefix(prefix = '') {
return freezeIfImmutable(
this,
new SL(...super.map((e) => `${prefix}${e}`)),
);
}
withSuffix(suffix = '') {
return freezeIfImmutable(
this,
new SL(...super.map((e) => `${e}${suffix}`)),
);
}
// withDerivatedSuffix(chars = '') {
// return freezeIfImmutable(
// this,
// new SL(
// ...super.flatMap((t) => [
// t,
// t.endsWith(chars)
// ? t.slice(0, Math.min(t.length, chars.length) * -1)
// : `${t}${chars}`,
// ]),
// ),
// );
// }
// withDerivatedPrefix(chars = '') {
// return freezeIfImmutable(
// this,
// new SL(
// ...super.flatMap((t) => [
// t,
// t.startsWith(chars)
// ? t.slice(Math.min(chars.length, t.length), t.length)
// : `${chars}${t}`,
// ]),
// ),
// );
// }
withReplace(string, replacement = undefined) {
return freezeIfImmutable(
this,
new SL(...super.map((e) => e.replace(string, replacement))),
);
}
withReplaceAll(string, replacement = undefined) {
return freezeIfImmutable(
this,
new SL(...super.map((e) => e.replaceAll(string, replacement))),
);
}
toLowerCase() {
return freezeIfImmutable(
this,
new SL(...super.map((e) => e.toLowerCase())),
);
}
toUpperCase() {
return freezeIfImmutable(
this,
new SL(...super.map((e) => e.toUpperCase())),
);
}
toCapitalize() {
return this.withReplace(/\b\w/g, (char) => char.toUpperCase());
}
toUnCapitalize() {
return this.withReplace(/\b\w/g, (char) => char.toLowerCase());
}
value(value) {
if (
typeof value === 'string' &&
(this.enum[value] || (this.hasEmpty && value === ''))
) {
return value;
}
throw new Error(`Invalid value ${value}`);
}
// Get the native array
mutable() {
return Array.from(this);
}
toRecordValue(initialValue = undefined, ...records) {
return Object.assign(
{},
...records,
Object.fromEntries(super.map((e) => [e, initialValue])),
);
}
toRecordType(type = 'any', initialValue = undefined, ...records) {
return Object.assign(
{},
...records,
Object.fromEntries(super.map((e) => [e, initialValue])),
);
}
// Methods returning the native array
map() {
const mut = this.mutable();
return mut.map.apply(mut, arguments);
}
filter() {
const mut = this.mutable();
return mut.filter.apply(mut, arguments);
}
reduce() {
const mut = this.mutable();
return mut.reduce.apply(mut, arguments);
}
reduceRight() {
const mut = this.mutable();
return mut.reduceRight.apply(mut, arguments);
}
flat() {
const mut = this.mutable();
return mut.flat.apply(mut, arguments);
}
flatMap() {
const mut = this.mutable();
return mut.flatMap.apply(mut, arguments);
}
with() {
const mut = this.mutable();
return mut.with.apply(mut, arguments);
}
}
export const ARRAY_IN_PLACE_MUTATION = Object.freeze({
push: 'push',
unshift: 'unshift',
shift: 'shift',
copyWithin: 'copyWithin',
pop: 'pop',
fill: 'fill',
splice: 'splice',
});