@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/iterate.ts
CHANGED
|
@@ -1,86 +1,86 @@
|
|
|
1
|
-
import { arrDivide } from './array';
|
|
2
|
-
|
|
3
|
-
type x = { x: number };
|
|
4
|
-
type xy = { x: number, y: number };
|
|
5
|
-
type xyz = { x: number, y: number, z: number };
|
|
6
|
-
|
|
7
|
-
/**
|
|
8
|
-
* Iterate over a function like nested for loops
|
|
9
|
-
* each cycle would return an object eg:
|
|
10
|
-
*
|
|
11
|
-
* xLength 3, yLength: 3, zLength: 3
|
|
12
|
-
* ```ts
|
|
13
|
-
* { 0, 0, 0 } ... { 0, 0, 1 } ... { 0, 0, 2 }
|
|
14
|
-
*
|
|
15
|
-
* { 0, 1, 0 } ... { 0, 1, 1 } ... { 0, 1, 2 }
|
|
16
|
-
* ```
|
|
17
|
-
* ...
|
|
18
|
-
* ```ts
|
|
19
|
-
* { 2, 2, 0 } ... { 2, 2, 1 } ... { 2, 2, 2 } <- last object
|
|
20
|
-
* ```
|
|
21
|
-
* Starting index is 0
|
|
22
|
-
*/
|
|
23
|
-
export function iterate(xLength: number): {
|
|
24
|
-
for: (fct: ({ x }: x) => any) => void;
|
|
25
|
-
map: <T>(fct: ({ x }: x) => T) => T[];
|
|
26
|
-
nestedMap: <T>(fct: ({ x }: x) => T) => T[];
|
|
27
|
-
};
|
|
28
|
-
|
|
29
|
-
export function iterate(xLength: number, yLength?: number): {
|
|
30
|
-
for: (fct: ({ x, y }: xy) => any) => void;
|
|
31
|
-
map: <T>(fct: ({ x, y }: xy) => T) => T[];
|
|
32
|
-
nestedMap: <T>(fct: ({ x, y }: xy) => T) => T[][];
|
|
33
|
-
};
|
|
34
|
-
|
|
35
|
-
export function iterate(
|
|
36
|
-
xLength: number, yLength?: number, zLength?: number
|
|
37
|
-
): {
|
|
38
|
-
for: (fct: ({ x, y, z }: xyz) => any) => void;
|
|
39
|
-
map: <T>(fct: ({ x, y, z }: xyz) => T) => T[];
|
|
40
|
-
nestedMap: <T>(fct: ({ x, y, z }: xyz) => T) => T[][][];
|
|
41
|
-
};
|
|
42
|
-
|
|
43
|
-
export function iterate(
|
|
44
|
-
xLength: number, yLength?: number, zLength?: number
|
|
45
|
-
) {
|
|
46
|
-
const iterator = <T>(fct: (
|
|
47
|
-
idxs: x | xy | xyz) => T, nested = false
|
|
48
|
-
) => {
|
|
49
|
-
const arr: T[] = [];
|
|
50
|
-
|
|
51
|
-
// xLength ->
|
|
52
|
-
// tslint:disable-next-line: no-shadowed-variable
|
|
53
|
-
for (let x = 0; x < xLength; x++) {
|
|
54
|
-
if (!yLength) arr.push(fct({ x }));
|
|
55
|
-
|
|
56
|
-
// yLength ->
|
|
57
|
-
else for (let y = 0; y < yLength; y++) {
|
|
58
|
-
if (!zLength) arr.push(fct({ x, y }));
|
|
59
|
-
|
|
60
|
-
// zLength ->
|
|
61
|
-
else for (let z = 0; z < zLength; z++) {
|
|
62
|
-
arr.push(fct({ x, y, z }));
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
if (!nested) return arr;
|
|
68
|
-
|
|
69
|
-
if (zLength && yLength) {
|
|
70
|
-
const newArr = arrDivide(arr, yLength * zLength);
|
|
71
|
-
return newArr.map((yArr) => arrDivide(yArr, zLength))
|
|
72
|
-
} else if (yLength) {
|
|
73
|
-
return arrDivide(arr, yLength);
|
|
74
|
-
} else {
|
|
75
|
-
return arr;
|
|
76
|
-
}
|
|
77
|
-
};
|
|
78
|
-
|
|
79
|
-
return {
|
|
80
|
-
for: (fct: (idxs: any) => any) => { iterator(fct); },
|
|
81
|
-
|
|
82
|
-
map: <T>(fct: (idxs: any) => T) => iterator(fct) as T[],
|
|
83
|
-
|
|
84
|
-
nestedMap: <T>(fct: (idxs: any) => T) => iterator(fct, true),
|
|
85
|
-
};
|
|
86
|
-
}
|
|
1
|
+
import { arrDivide } from './array';
|
|
2
|
+
|
|
3
|
+
type x = { x: number };
|
|
4
|
+
type xy = { x: number, y: number };
|
|
5
|
+
type xyz = { x: number, y: number, z: number };
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Iterate over a function like nested for loops
|
|
9
|
+
* each cycle would return an object eg:
|
|
10
|
+
*
|
|
11
|
+
* xLength 3, yLength: 3, zLength: 3
|
|
12
|
+
* ```ts
|
|
13
|
+
* { 0, 0, 0 } ... { 0, 0, 1 } ... { 0, 0, 2 }
|
|
14
|
+
*
|
|
15
|
+
* { 0, 1, 0 } ... { 0, 1, 1 } ... { 0, 1, 2 }
|
|
16
|
+
* ```
|
|
17
|
+
* ...
|
|
18
|
+
* ```ts
|
|
19
|
+
* { 2, 2, 0 } ... { 2, 2, 1 } ... { 2, 2, 2 } <- last object
|
|
20
|
+
* ```
|
|
21
|
+
* Starting index is 0
|
|
22
|
+
*/
|
|
23
|
+
export function iterate(xLength: number): {
|
|
24
|
+
for: (fct: ({ x }: x) => any) => void;
|
|
25
|
+
map: <T>(fct: ({ x }: x) => T) => T[];
|
|
26
|
+
nestedMap: <T>(fct: ({ x }: x) => T) => T[];
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
export function iterate(xLength: number, yLength?: number): {
|
|
30
|
+
for: (fct: ({ x, y }: xy) => any) => void;
|
|
31
|
+
map: <T>(fct: ({ x, y }: xy) => T) => T[];
|
|
32
|
+
nestedMap: <T>(fct: ({ x, y }: xy) => T) => T[][];
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
export function iterate(
|
|
36
|
+
xLength: number, yLength?: number, zLength?: number
|
|
37
|
+
): {
|
|
38
|
+
for: (fct: ({ x, y, z }: xyz) => any) => void;
|
|
39
|
+
map: <T>(fct: ({ x, y, z }: xyz) => T) => T[];
|
|
40
|
+
nestedMap: <T>(fct: ({ x, y, z }: xyz) => T) => T[][][];
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
export function iterate(
|
|
44
|
+
xLength: number, yLength?: number, zLength?: number
|
|
45
|
+
) {
|
|
46
|
+
const iterator = <T>(fct: (
|
|
47
|
+
idxs: x | xy | xyz) => T, nested = false
|
|
48
|
+
) => {
|
|
49
|
+
const arr: T[] = [];
|
|
50
|
+
|
|
51
|
+
// xLength ->
|
|
52
|
+
// tslint:disable-next-line: no-shadowed-variable
|
|
53
|
+
for (let x = 0; x < xLength; x++) {
|
|
54
|
+
if (!yLength) arr.push(fct({ x }));
|
|
55
|
+
|
|
56
|
+
// yLength ->
|
|
57
|
+
else for (let y = 0; y < yLength; y++) {
|
|
58
|
+
if (!zLength) arr.push(fct({ x, y }));
|
|
59
|
+
|
|
60
|
+
// zLength ->
|
|
61
|
+
else for (let z = 0; z < zLength; z++) {
|
|
62
|
+
arr.push(fct({ x, y, z }));
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
if (!nested) return arr;
|
|
68
|
+
|
|
69
|
+
if (zLength && yLength) {
|
|
70
|
+
const newArr = arrDivide(arr, yLength * zLength);
|
|
71
|
+
return newArr.map((yArr) => arrDivide(yArr, zLength))
|
|
72
|
+
} else if (yLength) {
|
|
73
|
+
return arrDivide(arr, yLength);
|
|
74
|
+
} else {
|
|
75
|
+
return arr;
|
|
76
|
+
}
|
|
77
|
+
};
|
|
78
|
+
|
|
79
|
+
return {
|
|
80
|
+
for: (fct: (idxs: any) => any) => { iterator(fct); },
|
|
81
|
+
|
|
82
|
+
map: <T>(fct: (idxs: any) => T) => iterator(fct) as T[],
|
|
83
|
+
|
|
84
|
+
nestedMap: <T>(fct: (idxs: any) => T) => iterator(fct, true),
|
|
85
|
+
};
|
|
86
|
+
}
|
package/src/number.ts
CHANGED
|
@@ -1,62 +1,64 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
*
|
|
6
|
-
* `min`
|
|
7
|
-
*
|
|
8
|
-
* @param
|
|
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
|
-
|
|
1
|
+
import { arrToBoolDict, isType } from '.';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Returns a random whole number between `min` and `max`,
|
|
5
|
+
* `min` and `max` are inclusive.
|
|
6
|
+
* `min` & `max` must be whole numbers
|
|
7
|
+
* @param min - whole number & `< max`
|
|
8
|
+
* @param max - whole number & `> min`
|
|
9
|
+
*/
|
|
10
|
+
export const randInt = (min: number, max: number) =>
|
|
11
|
+
Math.floor(Math.random() * ((max + 1) - min)) + min;
|
|
12
|
+
|
|
13
|
+
export const rand = (min: number, max: number) =>
|
|
14
|
+
Math.random() * (max - min) + min;
|
|
15
|
+
|
|
16
|
+
/** Takes an array of numbers and finds and average */
|
|
17
|
+
export const average = (nArr: number[]) =>
|
|
18
|
+
nArr.reduce((a = 0, b = 0) => a + b, 0) / nArr.length;
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Takes a number `n` & fixes to decimal places `places`
|
|
22
|
+
* @param n - the number to fix decimal places of
|
|
23
|
+
* @param places - number of decimal places to round to
|
|
24
|
+
* @example
|
|
25
|
+
* ```js
|
|
26
|
+
* decPlaces(1.1276, 2) //=> 1.13
|
|
27
|
+
* ```
|
|
28
|
+
*/
|
|
29
|
+
export const decPlace = (n: number, places: number) =>
|
|
30
|
+
parseFloat(n.toFixed(places));
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* @example
|
|
34
|
+
* ```js
|
|
35
|
+
* numberWithCommas(1000000) //=> '1,000,000'
|
|
36
|
+
* ```
|
|
37
|
+
*/
|
|
38
|
+
export const numberWithCommas = (n: number | string) =>
|
|
39
|
+
n.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ',');
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Takes a number || number[] and min & max, will generate a
|
|
43
|
+
* new random number.
|
|
44
|
+
*/
|
|
45
|
+
export function newNum(
|
|
46
|
+
prevNum: number | number[], min: number, max: number
|
|
47
|
+
) {
|
|
48
|
+
let num;
|
|
49
|
+
let loop = 0;
|
|
50
|
+
const dict: Dict<boolean> = isType(prevNum, 'array') ?
|
|
51
|
+
arrToBoolDict(prevNum) : { [prevNum]: true };
|
|
52
|
+
|
|
53
|
+
do {
|
|
54
|
+
num = randInt(min, max);
|
|
55
|
+
loop++;
|
|
56
|
+
|
|
57
|
+
if (loop > 1_000_000) {
|
|
58
|
+
console.error('Check for infinite loops');
|
|
59
|
+
throw new Error('Looped 1 million times');
|
|
60
|
+
}
|
|
61
|
+
} while (dict[num]);
|
|
62
|
+
|
|
63
|
+
return num;
|
|
64
|
+
}
|
package/src/object.ts
CHANGED
|
@@ -1,123 +1,109 @@
|
|
|
1
|
-
import type { Omit, StrKeys, ResolvedValue,
|
|
2
|
-
|
|
3
|
-
/** Maps over an object just as a [ ].map would on an array */
|
|
4
|
-
export function objMap<
|
|
5
|
-
T, O extends {}
|
|
6
|
-
>(o: O, funct: (keyVal: { key: StrKeys<O>, val: O[StrKeys<O>] }) => T)
|
|
7
|
-
{
|
|
8
|
-
const newObj = { } as { [P in StrKeys<O>]: T };
|
|
9
|
-
|
|
10
|
-
for (const key in o) {
|
|
11
|
-
newObj[key] = funct({ key, val: o[key] });
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
return newObj;
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
/** Filters out keys based on bool value returned by function */
|
|
18
|
-
export function objFilter<O extends {}>(
|
|
19
|
-
o: O, funct: (keyVal: {
|
|
20
|
-
key: StrKeys<O>,
|
|
21
|
-
val: O[StrKeys<O>]
|
|
22
|
-
}) => boolean
|
|
23
|
-
)
|
|
24
|
-
{
|
|
25
|
-
const newObj = { ...o };
|
|
26
|
-
|
|
27
|
-
for (const key in o) {
|
|
28
|
-
if (!funct({ key, val: o[key] })) delete newObj[key];
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
return newObj as { [P in StrKeys<O>]?: O[P] };
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
/**
|
|
35
|
-
* Removes all keys from object in the `filterOut` array.
|
|
36
|
-
* The original object is not mutated.
|
|
37
|
-
*/
|
|
38
|
-
export function objRemoveKeys<
|
|
39
|
-
T extends {}, K extends keyof T
|
|
40
|
-
>(obj: T, filterOut: K[])
|
|
41
|
-
{
|
|
42
|
-
const newObj: T = { ...obj as any };
|
|
43
|
-
filterOut.forEach((key) => delete newObj[key]);
|
|
44
|
-
|
|
45
|
-
return newObj as Omit<T, K>;
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
export function objKeys<T extends {}>(o: T): StrKeys<T>[]
|
|
49
|
-
{
|
|
50
|
-
if (Object.keys) return Object.keys(o) as StrKeys<T>[];
|
|
51
|
-
|
|
52
|
-
const keys: StrKeys<T>[] = [];
|
|
53
|
-
for (const k in o) keys.push(k);
|
|
54
|
-
|
|
55
|
-
return keys;
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
export const objVals = <T = any>(o: { [key: string]: T }): T[] =>
|
|
59
|
-
Object.values
|
|
60
|
-
|
|
61
|
-
export const objKeyVals = <T extends {}>(o: T) =>
|
|
62
|
-
objKeys(o).map((key) => ({ key, val: o[key] }));
|
|
63
|
-
|
|
64
|
-
/**
|
|
65
|
-
* Creates a new object from `extract` object and an array of `keys` to
|
|
66
|
-
* transfer
|
|
67
|
-
*/
|
|
68
|
-
export function objExtract<
|
|
69
|
-
T extends {},
|
|
70
|
-
K extends keyof T,
|
|
71
|
-
U extends { [P in K]: T[P] }
|
|
72
|
-
>(extract: T, keys: K[]): U
|
|
73
|
-
{
|
|
74
|
-
const newObj = { } as U;
|
|
75
|
-
keys.forEach((key) => (newObj[key] as any as T[K]) = extract[key]);
|
|
76
|
-
|
|
77
|
-
return newObj;
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
/**
|
|
81
|
-
* @param o the object that is to be resolved
|
|
82
|
-
* @param path path to the desired value eg:
|
|
83
|
-
* ```ts
|
|
84
|
-
* "first.second.stuff" => obj.first.second.stuff
|
|
85
|
-
* ```
|
|
86
|
-
*/
|
|
87
|
-
export const objResolve = (o: AnyObj, path: string): any =>
|
|
88
|
-
path.split('.').reduce((prev, key) => prev[key], o);
|
|
89
|
-
|
|
90
|
-
/**
|
|
91
|
-
*
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
)
|
|
96
|
-
const
|
|
97
|
-
|
|
98
|
-
const
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
return
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
const keyValues = objKeyVals(obj);
|
|
111
|
-
const toEqual = keyValues.length;
|
|
112
|
-
const values = { } as { [P in keyof T]: ResolvedValue<T[P]> };
|
|
113
|
-
|
|
114
|
-
let total = 0;
|
|
115
|
-
return new Promise<typeof values>((resolve) =>
|
|
116
|
-
keyValues.forEach(({ key, val: x }) => x.then((data) => {
|
|
117
|
-
values[key] = data;
|
|
118
|
-
total++;
|
|
119
|
-
|
|
120
|
-
if (toEqual === total) resolve(values);
|
|
121
|
-
}))
|
|
122
|
-
);
|
|
123
|
-
}
|
|
1
|
+
import type { Omit, StrKeys, ResolvedValue, AnyObj } from '.';
|
|
2
|
+
|
|
3
|
+
/** Maps over an object just as a [ ].map would on an array */
|
|
4
|
+
export function objMap<
|
|
5
|
+
T, O extends {}
|
|
6
|
+
>(o: O, funct: (keyVal: { key: StrKeys<O>, val: O[StrKeys<O>] }) => T)
|
|
7
|
+
{
|
|
8
|
+
const newObj = { } as { [P in StrKeys<O>]: T };
|
|
9
|
+
|
|
10
|
+
for (const key in o) {
|
|
11
|
+
newObj[key] = funct({ key, val: o[key] });
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
return newObj;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/** Filters out keys based on bool value returned by function */
|
|
18
|
+
export function objFilter<O extends {}>(
|
|
19
|
+
o: O, funct: (keyVal: {
|
|
20
|
+
key: StrKeys<O>,
|
|
21
|
+
val: O[StrKeys<O>]
|
|
22
|
+
}) => boolean
|
|
23
|
+
)
|
|
24
|
+
{
|
|
25
|
+
const newObj = { ...o };
|
|
26
|
+
|
|
27
|
+
for (const key in o) {
|
|
28
|
+
if (!funct({ key, val: o[key] })) delete newObj[key];
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
return newObj as { [P in StrKeys<O>]?: O[P] };
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Removes all keys from object in the `filterOut` array.
|
|
36
|
+
* The original object is not mutated.
|
|
37
|
+
*/
|
|
38
|
+
export function objRemoveKeys<
|
|
39
|
+
T extends {}, K extends keyof T
|
|
40
|
+
>(obj: T, filterOut: K[])
|
|
41
|
+
{
|
|
42
|
+
const newObj: T = { ...obj as any };
|
|
43
|
+
filterOut.forEach((key) => delete newObj[key]);
|
|
44
|
+
|
|
45
|
+
return newObj as Omit<T, K>;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export function objKeys<T extends {}>(o: T): StrKeys<T>[]
|
|
49
|
+
{
|
|
50
|
+
if (Object.keys) return Object.keys(o) as StrKeys<T>[];
|
|
51
|
+
|
|
52
|
+
const keys: StrKeys<T>[] = [];
|
|
53
|
+
for (const k in o) keys.push(k);
|
|
54
|
+
|
|
55
|
+
return keys;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export const objVals = <T = any>(o: { [key: string]: T }): T[] =>
|
|
59
|
+
Object.values(o)
|
|
60
|
+
|
|
61
|
+
export const objKeyVals = <T extends {}>(o: T) =>
|
|
62
|
+
objKeys(o).map((key) => ({ key, val: o[key] }));
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Creates a new object from `extract` object and an array of `keys` to
|
|
66
|
+
* transfer
|
|
67
|
+
*/
|
|
68
|
+
export function objExtract<
|
|
69
|
+
T extends {},
|
|
70
|
+
K extends keyof T,
|
|
71
|
+
U extends { [P in K]: T[P] }
|
|
72
|
+
>(extract: T, keys: K[]): U
|
|
73
|
+
{
|
|
74
|
+
const newObj = { } as U;
|
|
75
|
+
keys.forEach((key) => (newObj[key] as any as T[K]) = extract[key]);
|
|
76
|
+
|
|
77
|
+
return newObj;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* @param o the object that is to be resolved
|
|
82
|
+
* @param path path to the desired value eg:
|
|
83
|
+
* ```ts
|
|
84
|
+
* "first.second.stuff" => obj.first.second.stuff
|
|
85
|
+
* ```
|
|
86
|
+
*/
|
|
87
|
+
export const objResolve = (o: AnyObj, path: string): any =>
|
|
88
|
+
path.split('.').reduce((prev, key) => prev[key], o);
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* Takes a dictionary/object made of Promises and Observables and
|
|
92
|
+
* extracts all values. This function will return resolved values from all
|
|
93
|
+
* Observable/Promise on the obj when all promises on object resolve.
|
|
94
|
+
*/
|
|
95
|
+
export function objPromiseAll<T extends Dict<Promise<any>>>(obj: T) {
|
|
96
|
+
const keyValues = objKeyVals(obj);
|
|
97
|
+
const toEqual = keyValues.length;
|
|
98
|
+
const values = { } as { [P in keyof T]: ResolvedValue<T[P]> };
|
|
99
|
+
|
|
100
|
+
let total = 0;
|
|
101
|
+
return new Promise<typeof values>((resolve) =>
|
|
102
|
+
keyValues.forEach(({ key, val: x }) => x!.then((data) => {
|
|
103
|
+
values[key] = data;
|
|
104
|
+
total++;
|
|
105
|
+
|
|
106
|
+
if (toEqual === total) resolve(values);
|
|
107
|
+
}))
|
|
108
|
+
);
|
|
109
|
+
}
|
package/src/string.ts
CHANGED
|
@@ -1,20 +1,6 @@
|
|
|
1
|
-
/** Checks if string includes a substring */
|
|
2
|
-
export const strIncludes = (str: string, subStr: string) =>
|
|
3
|
-
str.indexOf(subStr) !== -1;
|
|
4
|
-
|
|
5
|
-
export const
|
|
6
|
-
str.replace(new RegExp(remove, 'g'), '');
|
|
7
|
-
|
|
8
|
-
// export const minAppend = (
|
|
9
|
-
// item: string | number, length: number, append = '0'
|
|
10
|
-
// ) => {
|
|
11
|
-
// item = item + '';
|
|
12
|
-
// if (length < 1) console.error(length, 'length can not be less than 1');
|
|
13
|
-
// if (item.length > length) console.error(item, 'item can not exceed length');
|
|
14
|
-
|
|
15
|
-
// return (append.repeat(length - 1) + item).slice(-(length))
|
|
16
|
-
// }
|
|
17
|
-
|
|
18
|
-
export const minAppend = (
|
|
19
|
-
item: string | number, length: number, append = '0'
|
|
20
|
-
) => (append.repeat(length - 1) + item).slice(-(length));
|
|
1
|
+
/** Checks if string includes a substring */
|
|
2
|
+
export const strIncludes = (str: string, subStr: string) =>
|
|
3
|
+
str.indexOf(subStr) !== -1;
|
|
4
|
+
|
|
5
|
+
export const strRemoveAll = (str: string, remove: string | RegExp) =>
|
|
6
|
+
str.replace(new RegExp(remove, 'g'), '');
|