@giveback007/util-lib 0.25.4 → 1.0.0
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 +7 -7
- package/dist/@types.d.ts +40 -42
- package/dist/@types.js +2 -2
- package/dist/array.d.ts +66 -66
- package/dist/array.js +128 -128
- package/dist/array.js.map +1 -1
- package/dist/clone.d.ts +2 -2
- package/dist/clone.js +44 -45
- package/dist/clone.js.map +1 -1
- package/dist/equality.d.ts +1 -1
- package/dist/equality.js +81 -82
- package/dist/equality.js.map +1 -1
- package/dist/general.d.ts +44 -37
- package/dist/general.js +172 -139
- package/dist/general.js.map +1 -1
- package/dist/index.d.ts +11 -11
- package/dist/index.js +27 -23
- package/dist/index.js.map +1 -1
- package/dist/iterate.d.ts +44 -44
- package/dist/iterate.js +43 -44
- package/dist/iterate.js.map +1 -1
- package/dist/number.d.ts +33 -32
- package/dist/number.js +57 -55
- package/dist/number.js.map +1 -1
- package/dist/object.d.ts +45 -49
- package/dist/object.js +82 -92
- package/dist/object.js.map +1 -1
- package/dist/string.d.ts +3 -4
- package/dist/string.js +8 -18
- package/dist/string.js.map +1 -1
- package/dist/test.d.ts +40 -41
- package/dist/test.js +65 -67
- package/dist/test.js.map +1 -1
- package/dist/time.d.ts +87 -93
- package/dist/time.js +230 -174
- package/dist/time.js.map +1 -1
- package/package.json +33 -45
- package/src/@types/types.d.ts +34 -0
- package/src/@types.ts +67 -67
- package/src/array.ts +175 -175
- package/src/clone.ts +34 -35
- package/src/equality.ts +80 -80
- package/src/general.ts +192 -152
- package/src/index.ts +11 -11
- package/src/iterate.ts +86 -86
- package/src/number.ts +64 -62
- package/src/object.ts +109 -123
- package/src/string.ts +6 -20
- package/src/test.ts +71 -74
- package/src/time.ts +268 -219
- package/dist/node/file-systems.d.ts +0 -1
- package/dist/node/file-systems.js +0 -16
- package/dist/node/file-systems.js.map +0 -1
- package/dist/node/index.d.ts +0 -1
- package/dist/node/index.js +0 -14
- package/dist/node/index.js.map +0 -1
- package/src/node/file-systems.ts +0 -16
- package/src/node/index.ts +0 -1
package/src/clone.ts
CHANGED
|
@@ -1,35 +1,34 @@
|
|
|
1
|
-
/* tslint:disable */
|
|
2
|
-
// original source from:
|
|
3
|
-
// https://github.com/davidmarkclements/rfdc
|
|
4
|
-
|
|
5
|
-
/** Performances deep clone of a given object */
|
|
6
|
-
export function clone<T>(o: T): T
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
if (
|
|
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
|
-
}
|
|
1
|
+
/* tslint:disable */
|
|
2
|
+
// original source from:
|
|
3
|
+
// https://github.com/davidmarkclements/rfdc
|
|
4
|
+
|
|
5
|
+
/** Performances deep clone of a given object */
|
|
6
|
+
export function clone<T>(o: T): T {
|
|
7
|
+
var c, x, l, i, k: any, a: any, q: any;
|
|
8
|
+
if (typeof o !== 'object' || o === null) return o;
|
|
9
|
+
if (o instanceof Date) return new Date(o) as any;
|
|
10
|
+
|
|
11
|
+
x = Object.keys(o); l = x.length;
|
|
12
|
+
|
|
13
|
+
if (Array.isArray(o)) {
|
|
14
|
+
a = new Array(l)
|
|
15
|
+
for (i = 0; i < l; i++) {
|
|
16
|
+
k = x[i]; c = o[k];
|
|
17
|
+
if (typeof c !== 'object' || c === null) a[k] = c;
|
|
18
|
+
else if (c instanceof Date) a[k] = new Date(c);
|
|
19
|
+
else a[k] = clone(c);
|
|
20
|
+
}
|
|
21
|
+
return a;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
q = {};
|
|
25
|
+
|
|
26
|
+
for (i = 0; i < l; i++) {
|
|
27
|
+
k = x[i]; c = (o as any)[k];
|
|
28
|
+
if (Object.hasOwnProperty.call(o, k) === false) continue;
|
|
29
|
+
if (typeof c !== 'object' || c === null) q[k] = c;
|
|
30
|
+
else if (c instanceof Date) q[k] = new Date(c);
|
|
31
|
+
else q[k] = clone(c);
|
|
32
|
+
}
|
|
33
|
+
return q;
|
|
34
|
+
}
|
package/src/equality.ts
CHANGED
|
@@ -1,80 +1,80 @@
|
|
|
1
|
-
/* tslint:disable */
|
|
2
|
-
// original source from:
|
|
3
|
-
// https://github.com/epoberezkin/fast-deep-equal
|
|
4
|
-
|
|
5
|
-
export function equal(a: any, b: any)
|
|
6
|
-
{
|
|
7
|
-
if (a === b) return true;
|
|
8
|
-
|
|
9
|
-
if (a && b && typeof a === 'object' && typeof b === 'object') {
|
|
10
|
-
if (a.constructor !== b.constructor) return false;
|
|
11
|
-
|
|
12
|
-
var length, i, keys;
|
|
13
|
-
if (Array.isArray(a)) {
|
|
14
|
-
length = a.length;
|
|
15
|
-
if (length !== b.length) return false;
|
|
16
|
-
for (i = length; i-- !== 0;)
|
|
17
|
-
if (!equal(a[i], b[i])) return false;
|
|
18
|
-
return true;
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
if ((a instanceof Map) && (b instanceof Map)) {
|
|
22
|
-
if (a.size !== b.size) return false;
|
|
23
|
-
for (i of (a as any).entries())
|
|
24
|
-
if (!b.has(i[0])) return false;
|
|
25
|
-
for (i of (a as any).entries())
|
|
26
|
-
if (!equal(i[1], b.get(i[0]))) return false;
|
|
27
|
-
return true;
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
if ((a instanceof Set) && (b instanceof Set)) {
|
|
31
|
-
if (a.size !== b.size) return false;
|
|
32
|
-
for (i of (a as any).entries())
|
|
33
|
-
if (!b.has(i[0])) return false;
|
|
34
|
-
return true;
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
if (ArrayBuffer.isView(a) && ArrayBuffer.isView(b)) {
|
|
38
|
-
length = (a as any).length;
|
|
39
|
-
if (length != (b as any).length) return false;
|
|
40
|
-
for (i = length; i-- !== 0;)
|
|
41
|
-
if ((a as any)[i] !== (b as any)[i]) return false;
|
|
42
|
-
return true;
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
if (a.constructor === RegExp)
|
|
47
|
-
return a.source === b.source && a.flags === b.flags;
|
|
48
|
-
if (a.valueOf !== Object.prototype.valueOf)
|
|
49
|
-
return a.valueOf() === b.valueOf();
|
|
50
|
-
if (a.toString !== Object.prototype.toString)
|
|
51
|
-
return a.toString() === b.toString();
|
|
52
|
-
|
|
53
|
-
keys = Object.keys(a);
|
|
54
|
-
length = keys.length;
|
|
55
|
-
if (length !== Object.keys(b).length) return false;
|
|
56
|
-
|
|
57
|
-
for (i = length; i-- !== 0;)
|
|
58
|
-
if (!Object.prototype.hasOwnProperty.call(b, keys[i]))
|
|
59
|
-
return false;
|
|
60
|
-
|
|
61
|
-
for (i = length; i-- !== 0;) {
|
|
62
|
-
var key = keys[i];
|
|
63
|
-
|
|
64
|
-
if (key === '_owner' && a.$$typeof) {
|
|
65
|
-
// React-specific: avoid traversing React elements'
|
|
66
|
-
// _owner. _owner contains circular references
|
|
67
|
-
// and is not needed when comparing the actual elements
|
|
68
|
-
// (and not their owners)
|
|
69
|
-
continue;
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
if (!equal(a[key], b[key])) return false;
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
return true;
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
// true if both NaN, false otherwise
|
|
79
|
-
return a !== a && b !== b;
|
|
80
|
-
};
|
|
1
|
+
/* tslint:disable */
|
|
2
|
+
// original source from:
|
|
3
|
+
// https://github.com/epoberezkin/fast-deep-equal
|
|
4
|
+
|
|
5
|
+
export function equal(a: any, b: any)
|
|
6
|
+
{
|
|
7
|
+
if (a === b) return true;
|
|
8
|
+
|
|
9
|
+
if (a && b && typeof a === 'object' && typeof b === 'object') {
|
|
10
|
+
if (a.constructor !== b.constructor) return false;
|
|
11
|
+
|
|
12
|
+
var length, i, keys;
|
|
13
|
+
if (Array.isArray(a)) {
|
|
14
|
+
length = a.length;
|
|
15
|
+
if (length !== b.length) return false;
|
|
16
|
+
for (i = length; i-- !== 0;)
|
|
17
|
+
if (!equal(a[i], b[i])) return false;
|
|
18
|
+
return true;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
if ((a instanceof Map) && (b instanceof Map)) {
|
|
22
|
+
if (a.size !== b.size) return false;
|
|
23
|
+
for (i of (a as any).entries())
|
|
24
|
+
if (!b.has(i[0])) return false;
|
|
25
|
+
for (i of (a as any).entries())
|
|
26
|
+
if (!equal(i[1], b.get(i[0]))) return false;
|
|
27
|
+
return true;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
if ((a instanceof Set) && (b instanceof Set)) {
|
|
31
|
+
if (a.size !== b.size) return false;
|
|
32
|
+
for (i of (a as any).entries())
|
|
33
|
+
if (!b.has(i[0])) return false;
|
|
34
|
+
return true;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
if (ArrayBuffer.isView(a) && ArrayBuffer.isView(b)) {
|
|
38
|
+
length = (a as any).length;
|
|
39
|
+
if (length != (b as any).length) return false;
|
|
40
|
+
for (i = length; i-- !== 0;)
|
|
41
|
+
if ((a as any)[i] !== (b as any)[i]) return false;
|
|
42
|
+
return true;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
if (a.constructor === RegExp)
|
|
47
|
+
return a.source === b.source && a.flags === b.flags;
|
|
48
|
+
if (a.valueOf !== Object.prototype.valueOf)
|
|
49
|
+
return a.valueOf() === b.valueOf();
|
|
50
|
+
if (a.toString !== Object.prototype.toString)
|
|
51
|
+
return a.toString() === b.toString();
|
|
52
|
+
|
|
53
|
+
keys = Object.keys(a);
|
|
54
|
+
length = keys.length;
|
|
55
|
+
if (length !== Object.keys(b).length) return false;
|
|
56
|
+
|
|
57
|
+
for (i = length; i-- !== 0;)
|
|
58
|
+
if (!Object.prototype.hasOwnProperty.call(b, keys[i]!))
|
|
59
|
+
return false;
|
|
60
|
+
|
|
61
|
+
for (i = length; i-- !== 0;) {
|
|
62
|
+
var key = keys[i];
|
|
63
|
+
|
|
64
|
+
if (key === '_owner' && a.$$typeof) {
|
|
65
|
+
// React-specific: avoid traversing React elements'
|
|
66
|
+
// _owner. _owner contains circular references
|
|
67
|
+
// and is not needed when comparing the actual elements
|
|
68
|
+
// (and not their owners)
|
|
69
|
+
continue;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
if (!equal(a[key as any], b[key as any])) return false;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
return true;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
// true if both NaN, false otherwise
|
|
79
|
+
return a !== a && b !== b;
|
|
80
|
+
};
|
package/src/general.ts
CHANGED
|
@@ -1,152 +1,192 @@
|
|
|
1
|
-
// import * as fetchJsonp from 'fetch-jsonp';
|
|
2
|
-
import type { AnyObj, JsType } from '.';
|
|
3
|
-
import { isType, objMap, hasKey, clone } from '.';
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* @example
|
|
7
|
-
* ```js
|
|
8
|
-
* interval((i) => console.log('idx: ' + i), 1000, 2))
|
|
9
|
-
* // (log =>) "idx: 0"
|
|
10
|
-
* // (log =>) "idx: 1"
|
|
11
|
-
* // only ran twice because maxTimes was set to: `2` (third parameter)
|
|
12
|
-
*
|
|
13
|
-
* interval((i, stop) => i === 10 && stop());
|
|
14
|
-
* // when i is 10 will stop the interval
|
|
15
|
-
* ```
|
|
16
|
-
*/
|
|
17
|
-
export const interval = (
|
|
18
|
-
|
|
19
|
-
ms: number,
|
|
20
|
-
|
|
21
|
-
/** */
|
|
22
|
-
maxTimes
|
|
23
|
-
) => {
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
let i = 0;
|
|
28
|
-
|
|
29
|
-
const stop = () => clearInterval(itv);
|
|
30
|
-
const itv = setInterval(() => {
|
|
31
|
-
|
|
32
|
-
i++;
|
|
33
|
-
|
|
34
|
-
if (maxTimes && i >= maxTimes) stop();
|
|
35
|
-
}, ms);
|
|
36
|
-
|
|
37
|
-
return {
|
|
38
|
-
/** Stop the interval */
|
|
39
|
-
stop
|
|
40
|
-
};
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
/** A promise that waits `ms` amount of milliseconds to execute */
|
|
44
|
-
export const wait = (ms: number): Promise<void> =>
|
|
45
|
-
new Promise((res) => setTimeout(() => res(), ms));
|
|
46
|
-
|
|
47
|
-
const usubAllFunct = (x: any, unsubName = 'unsubscribe') =>
|
|
48
|
-
isType(x, 'object')
|
|
49
|
-
&&
|
|
50
|
-
hasKey(x, unsubName) ? x[unsubName]() : null;
|
|
51
|
-
|
|
52
|
-
export function unsubAll(objOrArr: AnyObj | any[], unsubName = 'unsubscribe') {
|
|
53
|
-
if (isType(objOrArr, 'array'))
|
|
54
|
-
objOrArr.forEach(x => usubAllFunct(x, unsubName));
|
|
55
|
-
else if (isType(objOrArr, 'object'))
|
|
56
|
-
objMap(objOrArr, ({ val }) => usubAllFunct(val, unsubName));
|
|
57
|
-
else
|
|
58
|
-
throw Error('argument "objOrArr" must be of type "object" or "array"');
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
/**
|
|
62
|
-
* Alternative to console.log in that it will clone the obj.
|
|
63
|
-
*
|
|
64
|
-
* Useful for when it is need to see the object in a specific
|
|
65
|
-
* state instance.
|
|
66
|
-
*/
|
|
67
|
-
export const cloneLog = (x: any) => console.log(clone(x));
|
|
68
|
-
|
|
69
|
-
/** An improved version of native `typeof` */
|
|
70
|
-
export function type(val: any): JsType
|
|
71
|
-
{
|
|
72
|
-
if (typeof val === 'object') {
|
|
73
|
-
if (Array.isArray(val)) return 'array';
|
|
74
|
-
else if (val === null) return 'null';
|
|
75
|
-
else return 'object';
|
|
76
|
-
} else {
|
|
77
|
-
if (val !== val) return 'NaN';
|
|
78
|
-
else return typeof val;
|
|
79
|
-
}
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
// https://httptoolkit.tech/blog/5-big-features-of-typescript-3.7#assert-signatures
|
|
83
|
-
export function assertType<T extends JsType>(
|
|
84
|
-
val: any,
|
|
85
|
-
types: T | T[]
|
|
86
|
-
): asserts val is T {
|
|
87
|
-
if (!isType(types, 'array')) types = [types];
|
|
88
|
-
for (const t of types) if (isType(val, t)) return;
|
|
89
|
-
|
|
90
|
-
throw Error(`value needs to be of type ${types.join(' || ')}`)
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
export const uuid = () =>
|
|
94
|
-
{
|
|
95
|
-
let d = new Date().getTime();
|
|
96
|
-
let d2 = (
|
|
97
|
-
performance
|
|
98
|
-
&&
|
|
99
|
-
performance.now
|
|
100
|
-
&&
|
|
101
|
-
(performance.now() * 1000)
|
|
102
|
-
) || 0;
|
|
103
|
-
|
|
104
|
-
const str = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx';
|
|
105
|
-
return str.replace(/[xy]/g, (c) => {
|
|
106
|
-
let r = Math.random() * 16;
|
|
107
|
-
if (d > 0) {
|
|
108
|
-
r = (d + r)%16 | 0;
|
|
109
|
-
d = Math.floor(d/16);
|
|
110
|
-
} else {
|
|
111
|
-
r = (d2 + r)%16 | 0;
|
|
112
|
-
d2 = Math.floor(d2/16);
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
return (c === 'x' ? r : (r & 0x3 | 0x8)).toString(16);
|
|
116
|
-
});
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
export const randomColorHex = () =>
|
|
120
|
-
'#' + ((1<<24)*Math.random() | 0).toString(16);
|
|
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
|
-
|
|
1
|
+
// import * as fetchJsonp from 'fetch-jsonp';
|
|
2
|
+
import type { AnyObj, JsType } from '.';
|
|
3
|
+
import { isType, objMap, hasKey, clone } from '.';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* @example
|
|
7
|
+
* ```js
|
|
8
|
+
* interval((i) => console.log('idx: ' + i), 1000, 2))
|
|
9
|
+
* // (log =>) "idx: 0"
|
|
10
|
+
* // (log =>) "idx: 1"
|
|
11
|
+
* // only ran twice because maxTimes was set to: `2` (third parameter)
|
|
12
|
+
*
|
|
13
|
+
* interval((i, stop) => i === 10 && stop());
|
|
14
|
+
* // when i is 10 will stop the interval
|
|
15
|
+
* ```
|
|
16
|
+
*/
|
|
17
|
+
export const interval = (
|
|
18
|
+
func: (i: number, stop: () => void) => any,
|
|
19
|
+
ms: number = 0,
|
|
20
|
+
|
|
21
|
+
/** n times to run the interval */
|
|
22
|
+
maxTimes: number = Infinity
|
|
23
|
+
) => {
|
|
24
|
+
maxTimes = Math.floor(maxTimes);
|
|
25
|
+
if (maxTimes < 1) return { stop: () => void(0) };
|
|
26
|
+
|
|
27
|
+
let i = 0;
|
|
28
|
+
|
|
29
|
+
const stop = () => clearInterval(itv);
|
|
30
|
+
const itv = setInterval(() => {
|
|
31
|
+
func(i, stop);
|
|
32
|
+
i++;
|
|
33
|
+
|
|
34
|
+
if (maxTimes && i >= maxTimes) stop();
|
|
35
|
+
}, ms);
|
|
36
|
+
|
|
37
|
+
return {
|
|
38
|
+
/** Stop the interval */
|
|
39
|
+
stop
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/** A promise that waits `ms` amount of milliseconds to execute */
|
|
44
|
+
export const wait = (ms: number): Promise<void> =>
|
|
45
|
+
new Promise((res) => setTimeout(() => res(), ms));
|
|
46
|
+
|
|
47
|
+
const usubAllFunct = (x: any, unsubName = 'unsubscribe') =>
|
|
48
|
+
isType(x, 'object')
|
|
49
|
+
&&
|
|
50
|
+
hasKey(x, unsubName) ? x[unsubName]() : null;
|
|
51
|
+
|
|
52
|
+
export function unsubAll(objOrArr: AnyObj | any[], unsubName = 'unsubscribe') {
|
|
53
|
+
if (isType(objOrArr, 'array'))
|
|
54
|
+
objOrArr.forEach(x => usubAllFunct(x, unsubName));
|
|
55
|
+
else if (isType(objOrArr, 'object'))
|
|
56
|
+
objMap(objOrArr, ({ val }) => usubAllFunct(val, unsubName));
|
|
57
|
+
else
|
|
58
|
+
throw Error('argument "objOrArr" must be of type "object" or "array"');
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Alternative to console.log in that it will clone the obj.
|
|
63
|
+
*
|
|
64
|
+
* Useful for when it is need to see the object in a specific
|
|
65
|
+
* state instance.
|
|
66
|
+
*/
|
|
67
|
+
export const cloneLog = (x: any) => console.log(clone(x));
|
|
68
|
+
|
|
69
|
+
/** An improved version of native `typeof` */
|
|
70
|
+
export function type(val: any): JsType
|
|
71
|
+
{
|
|
72
|
+
if (typeof val === 'object') {
|
|
73
|
+
if (Array.isArray(val)) return 'array';
|
|
74
|
+
else if (val === null) return 'null';
|
|
75
|
+
else return 'object';
|
|
76
|
+
} else {
|
|
77
|
+
if (val !== val) return 'NaN';
|
|
78
|
+
else return typeof val;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
// https://httptoolkit.tech/blog/5-big-features-of-typescript-3.7#assert-signatures
|
|
83
|
+
export function assertType<T extends JsType>(
|
|
84
|
+
val: any,
|
|
85
|
+
types: T | T[]
|
|
86
|
+
): asserts val is T {
|
|
87
|
+
if (!isType(types, 'array')) types = [types];
|
|
88
|
+
for (const t of types) if (isType(val, t)) return;
|
|
89
|
+
|
|
90
|
+
throw Error(`value needs to be of type: ${types.join(' || ')}`)
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
export const uuid = () =>
|
|
94
|
+
{
|
|
95
|
+
let d = new Date().getTime();
|
|
96
|
+
let d2 = (
|
|
97
|
+
performance
|
|
98
|
+
&&
|
|
99
|
+
performance.now
|
|
100
|
+
&&
|
|
101
|
+
(performance.now() * 1000)
|
|
102
|
+
) || 0;
|
|
103
|
+
|
|
104
|
+
const str = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx';
|
|
105
|
+
return str.replace(/[xy]/g, (c) => {
|
|
106
|
+
let r = Math.random() * 16;
|
|
107
|
+
if (d > 0) {
|
|
108
|
+
r = (d + r)%16 | 0;
|
|
109
|
+
d = Math.floor(d/16);
|
|
110
|
+
} else {
|
|
111
|
+
r = (d2 + r)%16 | 0;
|
|
112
|
+
d2 = Math.floor(d2/16);
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
return (c === 'x' ? r : (r & 0x3 | 0x8)).toString(16);
|
|
116
|
+
});
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
export const randomColorHex = () =>
|
|
120
|
+
'#' + ((1<<24)*Math.random() | 0).toString(16);
|
|
121
|
+
|
|
122
|
+
export const debounce = (fn: AnyFnc, ms: num) => {
|
|
123
|
+
let timeoutId: ReturnType<typeof setTimeout>;
|
|
124
|
+
return function (this: any, ...args: any[]) {
|
|
125
|
+
clearTimeout(timeoutId);
|
|
126
|
+
timeoutId = setTimeout(() => fn.apply(this, args), ms);
|
|
127
|
+
};
|
|
128
|
+
};
|
|
129
|
+
|
|
130
|
+
export function debounceTimeOut() {
|
|
131
|
+
let timeoutId: ReturnType<typeof setTimeout> | null = null;
|
|
132
|
+
|
|
133
|
+
return (fct: Function | 'cancel', ms?: number) => {
|
|
134
|
+
if (timeoutId) clearTimeout(timeoutId);
|
|
135
|
+
|
|
136
|
+
if (fct !== 'cancel')
|
|
137
|
+
timeoutId = setTimeout(fct, ms || 0) as any;
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
export const debounceById = (() => {
|
|
142
|
+
const debounceDict: Dict<num> = {};
|
|
143
|
+
return (fn: AnyFnc, ms: num, id: str | num) => {
|
|
144
|
+
const dId = debounceDict[id];
|
|
145
|
+
if (dId) clearTimeout(dId);
|
|
146
|
+
|
|
147
|
+
debounceDict[id] = setTimeout(fn, ms) as any;
|
|
148
|
+
}
|
|
149
|
+
})();
|
|
150
|
+
|
|
151
|
+
export function promiseOut<T = any>() {
|
|
152
|
+
let resolve: any;
|
|
153
|
+
let error: any;
|
|
154
|
+
|
|
155
|
+
const promise: Promise<T> = new Promise((res, err) => {
|
|
156
|
+
resolve = res;
|
|
157
|
+
error = err;
|
|
158
|
+
});
|
|
159
|
+
|
|
160
|
+
return {
|
|
161
|
+
error: error as (reason?: any) => void,
|
|
162
|
+
resolve: resolve as (value: T) => void,
|
|
163
|
+
promise,
|
|
164
|
+
};
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
export async function concurrentTasks<T, Res = any>(
|
|
168
|
+
arr: T[],
|
|
169
|
+
fn: (x: T, idx: number) => Promise<Res> | Res,
|
|
170
|
+
nOfConcurrentTasks = 8
|
|
171
|
+
): Promise<Res[]> {
|
|
172
|
+
let idx = -1;
|
|
173
|
+
const result: Res[] = [];
|
|
174
|
+
|
|
175
|
+
await Promise.all(Array(nOfConcurrentTasks).fill(0).map(async () => {
|
|
176
|
+
let data: T | undefined;
|
|
177
|
+
while (data = arr[++idx]) {
|
|
178
|
+
const i = idx;
|
|
179
|
+
result[i] = await fn(data, idx)
|
|
180
|
+
}
|
|
181
|
+
}));
|
|
182
|
+
|
|
183
|
+
return result;
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
export function hash(str: string) {
|
|
187
|
+
let hash = 0n;
|
|
188
|
+
for (let i = 0; i < str.length; i++)
|
|
189
|
+
hash = (hash * 31n + BigInt(str.charCodeAt(i))) & 0xFFFFFFFFFFFFFFFFn;
|
|
190
|
+
|
|
191
|
+
return hash.toString(36);
|
|
192
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
export * from './@types';
|
|
2
|
-
export * from './array';
|
|
3
|
-
export * from './clone';
|
|
4
|
-
export * from './equality';
|
|
5
|
-
export * from './general';
|
|
6
|
-
export * from './iterate';
|
|
7
|
-
export * from './number';
|
|
8
|
-
export * from './object';
|
|
9
|
-
export * from './string';
|
|
10
|
-
export * from './test';
|
|
11
|
-
export * from './time';
|
|
1
|
+
export * from './@types';
|
|
2
|
+
export * from './array';
|
|
3
|
+
export * from './clone';
|
|
4
|
+
export * from './equality';
|
|
5
|
+
export * from './general';
|
|
6
|
+
export * from './iterate';
|
|
7
|
+
export * from './number';
|
|
8
|
+
export * from './object';
|
|
9
|
+
export * from './string';
|
|
10
|
+
export * from './test';
|
|
11
|
+
export * from './time';
|