data-structure-typed 1.12.21 → 1.15.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +278 -179
- package/dist/data-structures/binary-tree/binary-tree.d.ts +46 -1
- package/dist/data-structures/binary-tree/binary-tree.js +67 -0
- package/dist/data-structures/binary-tree/segment-tree.d.ts +29 -0
- package/dist/data-structures/binary-tree/segment-tree.js +45 -0
- package/dist/data-structures/graph/abstract-graph.d.ts +40 -5
- package/dist/data-structures/graph/abstract-graph.js +47 -7
- package/dist/data-structures/graph/directed-graph.d.ts +8 -0
- package/dist/data-structures/graph/directed-graph.js +14 -2
- package/dist/data-structures/graph/undirected-graph.d.ts +10 -0
- package/dist/data-structures/graph/undirected-graph.js +23 -1
- package/dist/data-structures/hash/coordinate-map.d.ts +7 -1
- package/dist/data-structures/hash/coordinate-map.js +16 -0
- package/dist/data-structures/hash/coordinate-set.d.ts +7 -1
- package/dist/data-structures/hash/coordinate-set.js +16 -0
- package/dist/data-structures/heap/heap.d.ts +16 -0
- package/dist/data-structures/heap/heap.js +38 -0
- package/dist/data-structures/linked-list/doubly-linked-list.d.ts +30 -7
- package/dist/data-structures/linked-list/doubly-linked-list.js +71 -4
- package/dist/data-structures/linked-list/singly-linked-list.d.ts +262 -328
- package/dist/data-structures/linked-list/singly-linked-list.js +258 -273
- package/dist/data-structures/priority-queue/priority-queue.d.ts +7 -1
- package/dist/data-structures/priority-queue/priority-queue.js +18 -2
- package/dist/data-structures/queue/deque.d.ts +18 -7
- package/dist/data-structures/queue/deque.js +50 -3
- package/dist/data-structures/types/abstract-graph.d.ts +2 -2
- package/dist/utils/types/utils.d.ts +0 -49
- package/dist/utils/types/utils.js +14 -52
- package/dist/utils/utils.d.ts +1 -97
- package/dist/utils/utils.js +197 -546
- package/package.json +4 -3
- package/src/data-structures/binary-tree/aa-tree.ts +1 -1
- package/src/data-structures/binary-tree/binary-tree.ts +84 -14
- package/src/data-structures/binary-tree/segment-tree.ts +45 -13
- package/src/data-structures/graph/abstract-graph.ts +58 -15
- package/src/data-structures/graph/directed-graph.ts +14 -5
- package/src/data-structures/graph/undirected-graph.ts +23 -6
- package/src/data-structures/hash/coordinate-map.ts +13 -1
- package/src/data-structures/hash/coordinate-set.ts +13 -1
- package/src/data-structures/heap/heap.ts +31 -0
- package/src/data-structures/linked-list/doubly-linked-list.ts +68 -11
- package/src/data-structures/linked-list/singly-linked-list.ts +312 -334
- package/src/data-structures/priority-queue/priority-queue.ts +15 -2
- package/src/data-structures/queue/deque.ts +38 -8
- package/src/data-structures/types/abstract-graph.ts +3 -3
- package/src/utils/types/utils.ts +165 -167
- package/src/utils/utils.ts +209 -480
- package/tests/unit/data-structures/graph/directed-graph.test.ts +431 -8
package/src/utils/utils.ts
CHANGED
|
@@ -1,15 +1,5 @@
|
|
|
1
|
-
import _ from 'lodash';
|
|
2
|
-
import type {AnyFunction, JSONObject, JSONSerializable} from './types';
|
|
3
|
-
|
|
4
|
-
export function randomText(length: number) {
|
|
5
|
-
let result = '';
|
|
6
|
-
const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
|
|
7
|
-
const charactersLength = characters.length;
|
|
8
|
-
for (let i = 0; i < length; i++) {
|
|
9
|
-
result += characters.charAt(Math.floor(Math.random() * charactersLength));
|
|
10
|
-
}
|
|
11
|
-
return result;
|
|
12
|
-
}
|
|
1
|
+
// import _ from 'lodash';
|
|
2
|
+
// import type {AnyFunction, CaseType, JSONObject, JSONSerializable} from './types';
|
|
13
3
|
|
|
14
4
|
export const uuidV4 = function () {
|
|
15
5
|
return 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx'.replace(/[x]/g, function (c) {
|
|
@@ -18,446 +8,214 @@ export const uuidV4 = function () {
|
|
|
18
8
|
});
|
|
19
9
|
};
|
|
20
10
|
|
|
21
|
-
export
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
export
|
|
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
|
-
get time60(): number {
|
|
183
|
-
return this._time60 / this._nXSpeed;
|
|
184
|
-
}
|
|
185
|
-
|
|
186
|
-
private _cusTime = 1000;
|
|
187
|
-
|
|
188
|
-
get cusTime(): number {
|
|
189
|
-
return this._cusTime / this._nXSpeed;
|
|
190
|
-
}
|
|
191
|
-
|
|
192
|
-
set cusTime(v: number) {
|
|
193
|
-
this._cusTime = v;
|
|
194
|
-
}
|
|
195
|
-
}
|
|
196
|
-
|
|
197
|
-
export const wait = async (ms: number, resolveValue?: any) => {
|
|
198
|
-
return new Promise((resolve, reject) => {
|
|
199
|
-
setTimeout(() => {
|
|
200
|
-
const finalResolveValue = resolveValue || true;
|
|
201
|
-
resolve(finalResolveValue);
|
|
202
|
-
}, ms);
|
|
203
|
-
});
|
|
204
|
-
};
|
|
205
|
-
|
|
206
|
-
export function extractValue<Item>(data: { key: string, value: Item }[]) {
|
|
207
|
-
let result: Item[] = [];
|
|
208
|
-
if (data && data.length > 0) {
|
|
209
|
-
result = data.map(item => item.value);
|
|
210
|
-
}
|
|
211
|
-
return result;
|
|
212
|
-
}
|
|
213
|
-
|
|
214
|
-
export function keyValueToArray<Item>(data: { [key: string]: Item }) {
|
|
215
|
-
const itemArray: Array<Item> = [];
|
|
216
|
-
const keys = Object.keys(data);
|
|
217
|
-
for (const i of keys) {
|
|
218
|
-
itemArray.push({...data[i], _id: i});
|
|
219
|
-
}
|
|
220
|
-
return itemArray;
|
|
221
|
-
}
|
|
222
|
-
|
|
223
|
-
export function minuted(time: number) {
|
|
224
|
-
const minutes = Math.floor(time / 60000).toString();
|
|
225
|
-
const seconds = Math.floor((time % 60000) / 1000).toString().padStart(2, '0');
|
|
226
|
-
return `${minutes}:${seconds}`;
|
|
227
|
-
}
|
|
228
|
-
|
|
229
|
-
export function randomDate(start?: Date, end?: Date, specificProbabilityStart?: Date, specificProbability?: number) {
|
|
230
|
-
if (!start) start = new Date('1970-1-1');
|
|
231
|
-
if (!end) end = new Date();
|
|
232
|
-
|
|
233
|
-
if (specificProbabilityStart) {
|
|
234
|
-
if (!specificProbability) specificProbability = 0.5;
|
|
235
|
-
if (Math.random() <= specificProbability) {
|
|
236
|
-
return new Date(specificProbabilityStart.getTime() + Math.random() * (end.getTime() - specificProbabilityStart.getTime()));
|
|
237
|
-
}
|
|
238
|
-
}
|
|
239
|
-
|
|
240
|
-
return new Date(start.getTime() + Math.random() * (end.getTime() - start.getTime()));
|
|
241
|
-
}
|
|
242
|
-
|
|
243
|
-
export const capitalizeWords = (str: string) => str.replace(/(?:^|\s)\S/g, (a: string) => a.toUpperCase());
|
|
244
|
-
|
|
245
|
-
export const capitalizeFirstLetter = (str: string) => str.charAt(0).toUpperCase() + str.slice(1);
|
|
246
|
-
|
|
247
|
-
export const comparerArray = <T>(otherArray: T[], limitKeys?: string[]) => {
|
|
248
|
-
return function (current: T) {
|
|
249
|
-
return otherArray.filter(function (other: T) {
|
|
250
|
-
if (!limitKeys) {
|
|
251
|
-
return _.isEqual(current, other);
|
|
252
|
-
} else {
|
|
253
|
-
// TODO
|
|
254
|
-
}
|
|
255
|
-
}).length == 0;
|
|
256
|
-
};
|
|
257
|
-
};
|
|
258
|
-
|
|
259
|
-
export const onlyInA = <T>(a: T[], b: T[]) => a.filter(comparerArray(b));
|
|
260
|
-
|
|
261
|
-
export const onlyInB = <T>(a: T[], b: T[]) => b.filter(comparerArray(a));
|
|
262
|
-
|
|
263
|
-
export const diffAB = <T>(a: T[], b: T[]) => onlyInA(a, b).concat(onlyInB(a, b));
|
|
264
|
-
|
|
265
|
-
export class StringUtil {
|
|
266
|
-
// camelCase
|
|
267
|
-
static toCamelCase(str: string) {
|
|
268
|
-
return _.camelCase(str);
|
|
269
|
-
}
|
|
270
|
-
|
|
271
|
-
// snake_case
|
|
272
|
-
static toSnakeCase(str: string) {
|
|
273
|
-
return _.snakeCase(str);
|
|
274
|
-
}
|
|
275
|
-
|
|
276
|
-
// PascalCase
|
|
277
|
-
static toPascalCase(str: string) {
|
|
278
|
-
return _.startCase(_.camelCase(str)).replace(/ /g, '');
|
|
279
|
-
}
|
|
280
|
-
|
|
281
|
-
// CONSTANT_CASE
|
|
282
|
-
static toConstantCase(str: string) {
|
|
283
|
-
return _.upperCase(str).replace(/ /g, '_');
|
|
284
|
-
}
|
|
285
|
-
|
|
286
|
-
// kebab-case
|
|
287
|
-
static toKebabCase(str: string) {
|
|
288
|
-
return _.kebabCase(str);
|
|
289
|
-
}
|
|
290
|
-
|
|
291
|
-
// lowercase
|
|
292
|
-
static toLowerCase(str: string) {
|
|
293
|
-
return _.lowerCase(str).replace(/ /g, '');
|
|
294
|
-
}
|
|
295
|
-
|
|
296
|
-
// Title Case
|
|
297
|
-
static toTitleCase(str: string) {
|
|
298
|
-
return _.startCase(_.camelCase(str));
|
|
299
|
-
}
|
|
300
|
-
|
|
301
|
-
// Sentence case
|
|
302
|
-
static toSentenceCase(str: string) {
|
|
303
|
-
return _.upperFirst(_.lowerCase(str));
|
|
304
|
-
}
|
|
305
|
-
|
|
306
|
-
// path/case
|
|
307
|
-
static toPathCase(str: string) {
|
|
308
|
-
return _.lowerCase(str).replace(/ /g, '/');
|
|
309
|
-
}
|
|
310
|
-
|
|
311
|
-
// dot.case
|
|
312
|
-
static toDotCase(str: string) {
|
|
313
|
-
return _.lowerCase(str).replace(/ /g, '.');
|
|
314
|
-
}
|
|
315
|
-
}
|
|
316
|
-
|
|
317
|
-
export type CaseType =
|
|
318
|
-
'camel'
|
|
319
|
-
| 'snake'
|
|
320
|
-
| 'pascal'
|
|
321
|
-
| 'constant'
|
|
322
|
-
| 'kebab'
|
|
323
|
-
| 'lower'
|
|
324
|
-
| 'title'
|
|
325
|
-
| 'sentence'
|
|
326
|
-
| 'path'
|
|
327
|
-
| 'dot';
|
|
328
|
-
export const deepKeysConvert = (obj: any, toType?: CaseType): any => {
|
|
329
|
-
const _toType = toType || 'snake';
|
|
330
|
-
if (Array.isArray(obj)) {
|
|
331
|
-
return obj.map(v => deepKeysConvert(v, _toType));
|
|
332
|
-
} else if (obj !== null && obj.constructor === Object) {
|
|
333
|
-
return Object.keys(obj).reduce(
|
|
334
|
-
(result, key) => {
|
|
335
|
-
let newKey = '';
|
|
336
|
-
switch (_toType) {
|
|
337
|
-
case 'camel':
|
|
338
|
-
newKey = StringUtil.toCamelCase(key);
|
|
339
|
-
break;
|
|
340
|
-
case 'snake':
|
|
341
|
-
newKey = StringUtil.toSnakeCase(key);
|
|
342
|
-
break;
|
|
343
|
-
case 'pascal':
|
|
344
|
-
newKey = StringUtil.toPascalCase(key);
|
|
345
|
-
break;
|
|
346
|
-
case 'constant':
|
|
347
|
-
newKey = StringUtil.toConstantCase(key);
|
|
348
|
-
break;
|
|
349
|
-
case 'kebab':
|
|
350
|
-
newKey = StringUtil.toKebabCase(key);
|
|
351
|
-
break;
|
|
352
|
-
case 'lower':
|
|
353
|
-
newKey = StringUtil.toLowerCase(key);
|
|
354
|
-
break;
|
|
355
|
-
case 'title':
|
|
356
|
-
newKey = StringUtil.toTitleCase(key);
|
|
357
|
-
break;
|
|
358
|
-
case 'sentence':
|
|
359
|
-
newKey = StringUtil.toSentenceCase(key);
|
|
360
|
-
break;
|
|
361
|
-
case 'path':
|
|
362
|
-
newKey = StringUtil.toPathCase(key);
|
|
363
|
-
break;
|
|
364
|
-
case 'dot':
|
|
365
|
-
newKey = StringUtil.toDotCase(key);
|
|
366
|
-
break;
|
|
367
|
-
default:
|
|
368
|
-
newKey = StringUtil.toDotCase(key);
|
|
369
|
-
break;
|
|
370
|
-
}
|
|
371
|
-
return {
|
|
372
|
-
...result,
|
|
373
|
-
[newKey]: deepKeysConvert(obj[key], _toType),
|
|
374
|
-
};
|
|
375
|
-
},
|
|
376
|
-
{},
|
|
377
|
-
);
|
|
378
|
-
}
|
|
379
|
-
return obj;
|
|
380
|
-
};
|
|
381
|
-
|
|
382
|
-
export const deepRemoveByKey = (obj: any, keysToBeRemoved: string[]) => {
|
|
383
|
-
const result = _.transform(obj, function (result: JSONSerializable, value: any, key: string) {
|
|
384
|
-
if (_.isObject(value)) {
|
|
385
|
-
value = deepRemoveByKey(value, keysToBeRemoved);
|
|
386
|
-
}
|
|
387
|
-
if (!keysToBeRemoved.includes(key)) {
|
|
388
|
-
_.isArray(obj) ? result.push(value) : result[key] = value;
|
|
389
|
-
}
|
|
390
|
-
});
|
|
391
|
-
return result as typeof obj;
|
|
392
|
-
};
|
|
393
|
-
|
|
394
|
-
export const deepRenameKeys = (obj: JSONSerializable, keysMap: { [key in string]: string }) => {
|
|
395
|
-
return _.transform(obj, function (result: JSONSerializable, value: any, key: string | number) {
|
|
396
|
-
const currentKey = keysMap[key] || key;
|
|
397
|
-
result[currentKey] = _.isObject(value) ? deepRenameKeys(value, keysMap) : value;
|
|
398
|
-
});
|
|
399
|
-
};
|
|
400
|
-
|
|
401
|
-
export const deepReplaceValues = (obj: JSONSerializable, keyReducerMap: { [key in string]: (item: JSONSerializable) => any }) => {
|
|
402
|
-
const newObject = _.clone(obj) as JSONSerializable;
|
|
403
|
-
_.each(obj, (val: any, key: string) => {
|
|
404
|
-
for (const item in keyReducerMap) {
|
|
405
|
-
if (key === item) {
|
|
406
|
-
newObject[key] = keyReducerMap[item](newObject);
|
|
407
|
-
} else if (typeof (val) === 'object' || val instanceof Array) {
|
|
408
|
-
newObject[key] = deepReplaceValues(val, keyReducerMap);
|
|
409
|
-
}
|
|
410
|
-
}
|
|
411
|
-
});
|
|
412
|
-
return newObject;
|
|
413
|
-
};
|
|
11
|
+
// export const isObject = (object: string | JSONObject | boolean | AnyFunction | number) => object != null && typeof object === 'object';
|
|
12
|
+
|
|
13
|
+
// export const deepObjectStrictEqual = (object1: JSONSerializable, object2: JSONSerializable) => {
|
|
14
|
+
// const keys1 = Object.keys(object1);
|
|
15
|
+
// const keys2 = Object.keys(object2);
|
|
16
|
+
// if (keys1.length !== keys2.length) {
|
|
17
|
+
// return false;
|
|
18
|
+
// }
|
|
19
|
+
// for (const key of keys1) {
|
|
20
|
+
// const val1 = object1[key];
|
|
21
|
+
// const val2 = object2[key];
|
|
22
|
+
// const areObjects = isObject(val1) && isObject(val2);
|
|
23
|
+
// if (
|
|
24
|
+
// areObjects && !deepObjectStrictEqual(val1, val2) ||
|
|
25
|
+
// !areObjects && val1 !== val2
|
|
26
|
+
// ) {
|
|
27
|
+
// return false;
|
|
28
|
+
// }
|
|
29
|
+
// }
|
|
30
|
+
// return true;
|
|
31
|
+
// };
|
|
32
|
+
|
|
33
|
+
// export class StringUtil {
|
|
34
|
+
// // camelCase
|
|
35
|
+
// static toCamelCase(str: string) {
|
|
36
|
+
// return _.camelCase(str);
|
|
37
|
+
// }
|
|
38
|
+
//
|
|
39
|
+
// // snake_case
|
|
40
|
+
// static toSnakeCase(str: string) {
|
|
41
|
+
// return _.snakeCase(str);
|
|
42
|
+
// }
|
|
43
|
+
//
|
|
44
|
+
// // PascalCase
|
|
45
|
+
// static toPascalCase(str: string) {
|
|
46
|
+
// return _.startCase(_.camelCase(str)).replace(/ /g, '');
|
|
47
|
+
// }
|
|
48
|
+
//
|
|
49
|
+
// // CONSTANT_CASE
|
|
50
|
+
// static toConstantCase(str: string) {
|
|
51
|
+
// return _.upperCase(str).replace(/ /g, '_');
|
|
52
|
+
// }
|
|
53
|
+
//
|
|
54
|
+
// // kebab-case
|
|
55
|
+
// static toKebabCase(str: string) {
|
|
56
|
+
// return _.kebabCase(str);
|
|
57
|
+
// }
|
|
58
|
+
//
|
|
59
|
+
// // lowercase
|
|
60
|
+
// static toLowerCase(str: string) {
|
|
61
|
+
// return _.lowerCase(str).replace(/ /g, '');
|
|
62
|
+
// }
|
|
63
|
+
//
|
|
64
|
+
// // Title Case
|
|
65
|
+
// static toTitleCase(str: string) {
|
|
66
|
+
// return _.startCase(_.camelCase(str));
|
|
67
|
+
// }
|
|
68
|
+
//
|
|
69
|
+
// // Sentence case
|
|
70
|
+
// static toSentenceCase(str: string) {
|
|
71
|
+
// return _.upperFirst(_.lowerCase(str));
|
|
72
|
+
// }
|
|
73
|
+
//
|
|
74
|
+
// // path/case
|
|
75
|
+
// static toPathCase(str: string) {
|
|
76
|
+
// return _.lowerCase(str).replace(/ /g, '/');
|
|
77
|
+
// }
|
|
78
|
+
//
|
|
79
|
+
// // dot.case
|
|
80
|
+
// static toDotCase(str: string) {
|
|
81
|
+
// return _.lowerCase(str).replace(/ /g, '.');
|
|
82
|
+
// }
|
|
83
|
+
// }
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
// export const deepKeysConvert = (obj: any, toType?: CaseType): any => {
|
|
87
|
+
// const _toType = toType || 'snake';
|
|
88
|
+
// if (Array.isArray(obj)) {
|
|
89
|
+
// return obj.map(v => deepKeysConvert(v, _toType));
|
|
90
|
+
// } else if (obj !== null && obj.constructor === Object) {
|
|
91
|
+
// return Object.keys(obj).reduce(
|
|
92
|
+
// (result, key) => {
|
|
93
|
+
// let newKey = '';
|
|
94
|
+
// switch (_toType) {
|
|
95
|
+
// case 'camel':
|
|
96
|
+
// newKey = StringUtil.toCamelCase(key);
|
|
97
|
+
// break;
|
|
98
|
+
// case 'snake':
|
|
99
|
+
// newKey = StringUtil.toSnakeCase(key);
|
|
100
|
+
// break;
|
|
101
|
+
// case 'pascal':
|
|
102
|
+
// newKey = StringUtil.toPascalCase(key);
|
|
103
|
+
// break;
|
|
104
|
+
// case 'constant':
|
|
105
|
+
// newKey = StringUtil.toConstantCase(key);
|
|
106
|
+
// break;
|
|
107
|
+
// case 'kebab':
|
|
108
|
+
// newKey = StringUtil.toKebabCase(key);
|
|
109
|
+
// break;
|
|
110
|
+
// case 'lower':
|
|
111
|
+
// newKey = StringUtil.toLowerCase(key);
|
|
112
|
+
// break;
|
|
113
|
+
// case 'title':
|
|
114
|
+
// newKey = StringUtil.toTitleCase(key);
|
|
115
|
+
// break;
|
|
116
|
+
// case 'sentence':
|
|
117
|
+
// newKey = StringUtil.toSentenceCase(key);
|
|
118
|
+
// break;
|
|
119
|
+
// case 'path':
|
|
120
|
+
// newKey = StringUtil.toPathCase(key);
|
|
121
|
+
// break;
|
|
122
|
+
// case 'dot':
|
|
123
|
+
// newKey = StringUtil.toDotCase(key);
|
|
124
|
+
// break;
|
|
125
|
+
// default:
|
|
126
|
+
// newKey = StringUtil.toDotCase(key);
|
|
127
|
+
// break;
|
|
128
|
+
// }
|
|
129
|
+
// return {
|
|
130
|
+
// ...result,
|
|
131
|
+
// [newKey]: deepKeysConvert(obj[key], _toType),
|
|
132
|
+
// };
|
|
133
|
+
// },
|
|
134
|
+
// {},
|
|
135
|
+
// );
|
|
136
|
+
// }
|
|
137
|
+
// return obj;
|
|
138
|
+
// };
|
|
139
|
+
|
|
140
|
+
// export const deepRemoveByKey = (obj: any, keysToBeRemoved: string[]) => {
|
|
141
|
+
// const result = _.transform(obj, function (result: JSONSerializable, value: any, key: string) {
|
|
142
|
+
// if (_.isObject(value)) {
|
|
143
|
+
// value = deepRemoveByKey(value, keysToBeRemoved);
|
|
144
|
+
// }
|
|
145
|
+
// if (!keysToBeRemoved.includes(key)) {
|
|
146
|
+
// _.isArray(obj) ? result.push(value) : result[key] = value;
|
|
147
|
+
// }
|
|
148
|
+
// });
|
|
149
|
+
// return result as typeof obj;
|
|
150
|
+
// };
|
|
151
|
+
|
|
152
|
+
// export const deepRenameKeys = (obj: JSONSerializable, keysMap: { [key in string]: string }) => {
|
|
153
|
+
// return _.transform(obj, function (result: JSONSerializable, value: any, key: string | number) {
|
|
154
|
+
// const currentKey = keysMap[key] || key;
|
|
155
|
+
// result[currentKey] = _.isObject(value) ? deepRenameKeys(value, keysMap) : value;
|
|
156
|
+
// });
|
|
157
|
+
// };
|
|
158
|
+
|
|
159
|
+
// export const deepReplaceValues = (obj: JSONSerializable, keyReducerMap: { [key in string]: (item: JSONSerializable) => any }) => {
|
|
160
|
+
// const newObject = _.clone(obj) as JSONSerializable;
|
|
161
|
+
// _.each(obj, (val: any, key: string) => {
|
|
162
|
+
// for (const item in keyReducerMap) {
|
|
163
|
+
// if (key === item) {
|
|
164
|
+
// newObject[key] = keyReducerMap[item](newObject);
|
|
165
|
+
// } else if (typeof (val) === 'object' || val instanceof Array) {
|
|
166
|
+
// newObject[key] = deepReplaceValues(val, keyReducerMap);
|
|
167
|
+
// }
|
|
168
|
+
// }
|
|
169
|
+
// });
|
|
170
|
+
// return newObject;
|
|
171
|
+
// };
|
|
414
172
|
|
|
415
173
|
// TODO determine depth and pass root node as a param through callback
|
|
416
|
-
export const deepAdd = (obj: JSONSerializable, keyReducerMap: { [key in string]: (item: JSONSerializable) => any }, isItemRootParent?: boolean) => {
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
};
|
|
433
|
-
|
|
434
|
-
const styleString = (color: string) => `color: ${color}; font-weight: bold`;
|
|
435
|
-
|
|
436
|
-
const styleHeader = (header: string) => `%c[${header}]`;
|
|
437
|
-
|
|
438
|
-
export const bunnyConsole = {
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
};
|
|
449
|
-
|
|
450
|
-
export const timeStart = () => {
|
|
451
|
-
|
|
452
|
-
};
|
|
453
|
-
|
|
454
|
-
export const timeEnd = (startTime: number, headerLog?: string, consoleConditionFn?: (timeSpent: number) => boolean) => {
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
};
|
|
174
|
+
// export const deepAdd = (obj: JSONSerializable, keyReducerMap: { [key in string]: (item: JSONSerializable) => any }, isItemRootParent?: boolean) => {
|
|
175
|
+
// const newObject = _.clone(obj) as JSONObject | [];
|
|
176
|
+
// if (_.isObject(newObject) && !_.isArray(newObject)) {
|
|
177
|
+
// for (const item in keyReducerMap) {
|
|
178
|
+
// newObject[item] = keyReducerMap[item](newObject);
|
|
179
|
+
// }
|
|
180
|
+
// }
|
|
181
|
+
// _.each(obj, (val: any, key: string | number) => {
|
|
182
|
+
// if (_.isObject(val)) {
|
|
183
|
+
// for (const item in keyReducerMap) {
|
|
184
|
+
// // @ts-ignore
|
|
185
|
+
// newObject[key] = deepAdd(val, keyReducerMap, isItemRootParent);
|
|
186
|
+
// }
|
|
187
|
+
// }
|
|
188
|
+
// });
|
|
189
|
+
// return newObject;
|
|
190
|
+
// };
|
|
191
|
+
|
|
192
|
+
// const styleString = (color: string) => `color: ${color}; font-weight: bold`;
|
|
193
|
+
|
|
194
|
+
// const styleHeader = (header: string) => `%c[${header}]`;
|
|
195
|
+
|
|
196
|
+
// export const bunnyConsole = {
|
|
197
|
+
// log: (headerLog = 'bunny', ...args: any[]) => {
|
|
198
|
+
// return console.log(styleHeader(headerLog), styleString('black'), ...args);
|
|
199
|
+
// },
|
|
200
|
+
// warn: (headerLog = 'bunny', ...args: any[]) => {
|
|
201
|
+
// return console.warn(styleHeader(headerLog), styleString('orange'), ...args);
|
|
202
|
+
// },
|
|
203
|
+
// error: (headerLog = 'bunny', ...args: any[]) => {
|
|
204
|
+
// return console.error(styleHeader(headerLog), styleString('red'), ...args);
|
|
205
|
+
// }
|
|
206
|
+
// };
|
|
207
|
+
|
|
208
|
+
// export const timeStart = () => {
|
|
209
|
+
// return performance ? performance.now() : new Date().getTime();
|
|
210
|
+
// };
|
|
211
|
+
|
|
212
|
+
// export const timeEnd = (startTime: number, headerLog?: string, consoleConditionFn?: (timeSpent: number) => boolean) => {
|
|
213
|
+
// const timeSpent = (performance ? performance.now() : new Date().getTime()) - startTime;
|
|
214
|
+
// const isPassCondition = consoleConditionFn ? consoleConditionFn(timeSpent) : true;
|
|
215
|
+
// if (isPassCondition) {
|
|
216
|
+
// bunnyConsole.log(headerLog ? headerLog : 'time spent', timeSpent.toFixed(2));
|
|
217
|
+
// }
|
|
218
|
+
// };
|
|
461
219
|
|
|
462
220
|
export const arrayRemove = function <T>(array: T[], predicate: (item: T, index: number, array: T[]) => boolean): T[] {
|
|
463
221
|
let i = -1, len = array ? array.length : 0;
|
|
@@ -475,35 +233,6 @@ export const arrayRemove = function <T>(array: T[], predicate: (item: T, index:
|
|
|
475
233
|
return result;
|
|
476
234
|
};
|
|
477
235
|
|
|
478
|
-
export function memo() {
|
|
479
|
-
const cache: { [k: string]: any } = {};
|
|
480
|
-
// eslint-disable-next-line @typescript-eslint/ban-types
|
|
481
|
-
return function (target: Object, propertyKey: string, descriptor: PropertyDescriptor) {
|
|
482
|
-
const originalMethod = descriptor.value;
|
|
483
|
-
descriptor.value = function (...args: any[]) {
|
|
484
|
-
const cacheKey = `__cacheKey__${args.toString()}`;
|
|
485
|
-
// eslint-disable-next-line no-prototype-builtins
|
|
486
|
-
if (!cache.hasOwnProperty(cacheKey)) {
|
|
487
|
-
cache[cacheKey] = originalMethod.apply(this, args);
|
|
488
|
-
}
|
|
489
|
-
return cache[cacheKey];
|
|
490
|
-
}
|
|
491
|
-
}
|
|
492
|
-
}
|
|
493
|
-
|
|
494
|
-
export function zip<T = number, T1 = number>(array1: T[], array2: T1[], options?: { isToObj: boolean }) {
|
|
495
|
-
const zipped: [T, T1][] = [];
|
|
496
|
-
const zippedObjCoords: { x: T, y: T1 }[] = [];
|
|
497
|
-
const {isToObj} = options ? options : {isToObj: false};
|
|
498
|
-
for (let i = 0; i < array1.length; i++) {
|
|
499
|
-
if (isToObj) {
|
|
500
|
-
zippedObjCoords.push({x: array1[i], y: array2[i]})
|
|
501
|
-
} else {
|
|
502
|
-
zipped.push([array1[i], array2[i]]);
|
|
503
|
-
}
|
|
504
|
-
}
|
|
505
|
-
return isToObj ? zippedObjCoords : zipped;
|
|
506
|
-
}
|
|
507
236
|
|
|
508
237
|
/**
|
|
509
238
|
* data-structure-typed
|
|
@@ -512,7 +241,7 @@ export function zip<T = number, T1 = number>(array1: T[], array2: T1[], options?
|
|
|
512
241
|
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
513
242
|
* @license MIT License
|
|
514
243
|
*/
|
|
515
|
-
import {Thunk, ToThunkFn, TrlAsyncFn, TrlFn} from './types';
|
|
244
|
+
import type {Thunk, ToThunkFn, TrlAsyncFn, TrlFn} from './types';
|
|
516
245
|
|
|
517
246
|
export const THUNK_SYMBOL = Symbol('thunk')
|
|
518
247
|
|