@webdeveric/utils 0.7.1 → 0.9.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/dist/cjs/asArray.js.map +1 -1
- package/dist/cjs/delay.js +15 -5
- package/dist/cjs/delay.js.map +1 -1
- package/dist/cjs/delayAnimationFrame.js +7 -5
- package/dist/cjs/delayAnimationFrame.js.map +1 -1
- package/dist/cjs/getOwnKeys.js +11 -0
- package/dist/cjs/getOwnKeys.js.map +1 -0
- package/dist/cjs/getType.js +8 -0
- package/dist/cjs/getType.js.map +1 -0
- package/dist/cjs/index.js +3 -0
- package/dist/cjs/index.js.map +1 -1
- package/dist/cjs/joinStrings.js +1 -1
- package/dist/cjs/joinStrings.js.map +1 -1
- package/dist/cjs/node/fcrdns.js +7 -8
- package/dist/cjs/node/fcrdns.js.map +1 -1
- package/dist/cjs/normalize.js +55 -0
- package/dist/cjs/normalize.js.map +1 -0
- package/dist/cjs/secToString.js +37 -23
- package/dist/cjs/secToString.js.map +1 -1
- package/dist/cjs/toPascalCase.js +4 -3
- package/dist/cjs/toPascalCase.js.map +1 -1
- package/dist/cjs/trimEnd.js +1 -1
- package/dist/cjs/trimEnd.js.map +1 -1
- package/dist/cjs/trimStart.js +1 -1
- package/dist/cjs/trimStart.js.map +1 -1
- package/dist/cjs/uniqueItems.js.map +1 -1
- package/dist/cjs/until.js +1 -1
- package/dist/cjs/until.js.map +1 -1
- package/dist/mjs/asArray.js.map +1 -1
- package/dist/mjs/delay.js +15 -5
- package/dist/mjs/delay.js.map +1 -1
- package/dist/mjs/delayAnimationFrame.js +7 -5
- package/dist/mjs/delayAnimationFrame.js.map +1 -1
- package/dist/mjs/getOwnKeys.js +7 -0
- package/dist/mjs/getOwnKeys.js.map +1 -0
- package/dist/mjs/getType.js +4 -0
- package/dist/mjs/getType.js.map +1 -0
- package/dist/mjs/index.js +3 -0
- package/dist/mjs/index.js.map +1 -1
- package/dist/mjs/node/fcrdns.js +4 -5
- package/dist/mjs/node/fcrdns.js.map +1 -1
- package/dist/mjs/normalize.js +48 -0
- package/dist/mjs/normalize.js.map +1 -0
- package/dist/mjs/secToString.js +36 -22
- package/dist/mjs/secToString.js.map +1 -1
- package/dist/mjs/toPascalCase.js +4 -3
- package/dist/mjs/toPascalCase.js.map +1 -1
- package/dist/mjs/uniqueItems.js.map +1 -1
- package/dist/mjs/until.js +1 -1
- package/dist/mjs/until.js.map +1 -1
- package/dist/types/asArray.d.ts +1 -1
- package/dist/types/asArray.d.ts.map +1 -1
- package/dist/types/delay.d.ts +7 -1
- package/dist/types/delay.d.ts.map +1 -1
- package/dist/types/delayAnimationFrame.d.ts.map +1 -1
- package/dist/types/getOwnKeys.d.ts +2 -0
- package/dist/types/getOwnKeys.d.ts.map +1 -0
- package/dist/types/getType.d.ts +2 -0
- package/dist/types/getType.d.ts.map +1 -0
- package/dist/types/index.d.ts +3 -0
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/node/fcrdns.d.ts.map +1 -1
- package/dist/types/normalize.d.ts +27 -0
- package/dist/types/normalize.d.ts.map +1 -0
- package/dist/types/secToString.d.ts +13 -1
- package/dist/types/secToString.d.ts.map +1 -1
- package/dist/types/toPascalCase.d.ts.map +1 -1
- package/dist/types/uniqueItems.d.ts +1 -1
- package/dist/types/uniqueItems.d.ts.map +1 -1
- package/dist/types/until.d.ts +4 -4
- package/dist/types/until.d.ts.map +1 -1
- package/package.json +30 -21
- package/src/asArray.ts +1 -1
- package/src/delay.ts +17 -5
- package/src/delayAnimationFrame.ts +7 -5
- package/src/getOwnKeys.ts +6 -0
- package/src/getType.ts +3 -0
- package/src/index.ts +3 -0
- package/src/node/fcrdns.ts +7 -8
- package/src/normalize.ts +80 -0
- package/src/secToString.ts +40 -24
- package/src/toPascalCase.ts +5 -3
- package/src/uniqueItems.ts +1 -1
- package/src/until.ts +6 -6
package/src/normalize.ts
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import cloneDeep from 'lodash.clonedeep';
|
|
2
|
+
|
|
3
|
+
import type { Builtin } from 'ts-essentials';
|
|
4
|
+
|
|
5
|
+
import { getOwnKeys } from './getOwnKeys';
|
|
6
|
+
import { getType } from './getType';
|
|
7
|
+
|
|
8
|
+
export type NormalizeContext<OwnerRecordType> = {
|
|
9
|
+
original: Readonly<OwnerRecordType>;
|
|
10
|
+
current: OwnerRecordType;
|
|
11
|
+
data: Record<PropertyKey, unknown>;
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
export type NormalizerFn<OutputType, OwnerRecordType> = (
|
|
15
|
+
input: unknown,
|
|
16
|
+
context: NormalizeContext<OwnerRecordType>,
|
|
17
|
+
) => OutputType;
|
|
18
|
+
|
|
19
|
+
export type NormalizersRecord<RecordType, OwnerRecordType = RecordType> = {
|
|
20
|
+
[Property in keyof RecordType]?: RecordType[Property] extends Builtin
|
|
21
|
+
? NormalizerFn<RecordType[Property], OwnerRecordType>
|
|
22
|
+
: NormalizerFn<RecordType[Property], OwnerRecordType> | NormalizersRecord<RecordType[Property], OwnerRecordType>;
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* @example
|
|
27
|
+
*
|
|
28
|
+
* ```ts
|
|
29
|
+
* normalize({ demo: true }, { demo: () => false });
|
|
30
|
+
* ```
|
|
31
|
+
*
|
|
32
|
+
* ```ts
|
|
33
|
+
* normalize({ demo: true }, () => ({ demo: false }) });
|
|
34
|
+
* ```
|
|
35
|
+
*
|
|
36
|
+
* ```ts
|
|
37
|
+
* normalize(true, () => false );
|
|
38
|
+
* ```
|
|
39
|
+
*/
|
|
40
|
+
export function normalize<Data>(
|
|
41
|
+
input: Data,
|
|
42
|
+
normalizers?: NormalizerFn<Data, Data> | NormalizersRecord<Data, Data>,
|
|
43
|
+
): Data {
|
|
44
|
+
const normalized = cloneDeep(input);
|
|
45
|
+
|
|
46
|
+
const context: NormalizeContext<Data> = Object.seal({
|
|
47
|
+
original: input,
|
|
48
|
+
current: normalized,
|
|
49
|
+
data: {},
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
const walk = <CurrentRecord>(
|
|
53
|
+
currentRecord: CurrentRecord,
|
|
54
|
+
currentNormalizers?: NormalizerFn<CurrentRecord, Data> | NormalizersRecord<CurrentRecord, Data>,
|
|
55
|
+
): CurrentRecord => {
|
|
56
|
+
if (typeof currentNormalizers === 'undefined') {
|
|
57
|
+
return currentRecord;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
if (typeof currentNormalizers === 'function') {
|
|
61
|
+
return currentNormalizers(currentRecord, context);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
if (typeof currentNormalizers === 'object' && currentNormalizers !== null) {
|
|
65
|
+
if (typeof currentRecord === 'object' && currentRecord !== null) {
|
|
66
|
+
return getOwnKeys(currentNormalizers).reduce((data, key) => {
|
|
67
|
+
data[ key ] = walk(data[ key ], currentNormalizers[ key ]);
|
|
68
|
+
|
|
69
|
+
return data;
|
|
70
|
+
}, currentRecord);
|
|
71
|
+
}
|
|
72
|
+
} else {
|
|
73
|
+
throw new TypeError(`normalizers must be function or object. ${getType(currentNormalizers)} was received.`);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
return currentRecord;
|
|
77
|
+
};
|
|
78
|
+
|
|
79
|
+
return walk(normalized, normalizers);
|
|
80
|
+
}
|
package/src/secToString.ts
CHANGED
|
@@ -1,32 +1,48 @@
|
|
|
1
|
-
export
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
};
|
|
1
|
+
export enum Seconds {
|
|
2
|
+
Second = 1,
|
|
3
|
+
Minute = 60 * Second,
|
|
4
|
+
Hour = 60 * Minute,
|
|
5
|
+
Day = 24 * Hour,
|
|
6
|
+
Week = 7 * Day,
|
|
7
|
+
Month = (365.25 / 12) * Day,
|
|
8
|
+
Year = 12 * Month,
|
|
9
|
+
Decade = 10 * Year,
|
|
10
|
+
Century = 10 * Decade,
|
|
11
|
+
Millennium = 10 * Century,
|
|
12
|
+
}
|
|
14
13
|
|
|
15
|
-
|
|
14
|
+
type SecondsTuple = [key: Lowercase<keyof typeof Seconds>, seconds: Seconds];
|
|
16
15
|
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
16
|
+
export function secToString(time: number, joinString = ' '): string {
|
|
17
|
+
const secTime: SecondsTuple[] = [
|
|
18
|
+
[ 'millennium', Seconds.Millennium ],
|
|
19
|
+
[ 'century', Seconds.Century ],
|
|
20
|
+
[ 'decade', Seconds.Decade ],
|
|
21
|
+
[ 'year', Seconds.Year ],
|
|
22
|
+
[ 'month', Seconds.Month ],
|
|
23
|
+
[ 'week', Seconds.Week ],
|
|
24
|
+
[ 'day', Seconds.Day ],
|
|
25
|
+
[ 'hour', Seconds.Hour ],
|
|
26
|
+
[ 'minute', Seconds.Minute ],
|
|
27
|
+
[ 'second', Seconds.Second ],
|
|
28
|
+
];
|
|
21
29
|
|
|
22
|
-
|
|
30
|
+
const parts = secTime.reduce<string[]>(
|
|
31
|
+
(output, [ units, seconds ]) => {
|
|
32
|
+
if (seconds <= time) {
|
|
33
|
+
const currentValue = Math.floor(time / seconds);
|
|
34
|
+
const suffix = currentValue === 1 ? units : `${units}s`;
|
|
23
35
|
|
|
24
|
-
|
|
36
|
+
time %= seconds;
|
|
25
37
|
|
|
26
|
-
|
|
38
|
+
output.push(`${currentValue} ${suffix}`);
|
|
39
|
+
}
|
|
27
40
|
|
|
28
|
-
|
|
29
|
-
|
|
41
|
+
return output;
|
|
42
|
+
},
|
|
43
|
+
[],
|
|
44
|
+
);
|
|
30
45
|
|
|
31
|
-
|
|
46
|
+
// cSpell:ignore centurys
|
|
47
|
+
return parts.join(joinString).replace('centurys', 'centuries');
|
|
32
48
|
}
|
package/src/toPascalCase.ts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { getType } from './getType.js';
|
|
2
|
+
|
|
1
3
|
export function toPascalCase(text: string, customWords?: Record<string, string>) : string
|
|
2
4
|
{
|
|
3
5
|
let words = String(text).match( /[A-Z][a-z']+|\d+|[a-z']+/g );
|
|
@@ -8,12 +10,12 @@ export function toPascalCase(text: string, customWords?: Record<string, string>)
|
|
|
8
10
|
|
|
9
11
|
// Remove apostrophes then uppercase the first letter of each word.
|
|
10
12
|
words = words.map( word => {
|
|
11
|
-
const
|
|
13
|
+
const fixedWord = word.replace(/'/g, '');
|
|
12
14
|
|
|
13
|
-
return
|
|
15
|
+
return fixedWord[ 0 ].toUpperCase() + fixedWord.substring(1);
|
|
14
16
|
});
|
|
15
17
|
|
|
16
|
-
if ( customWords &&
|
|
18
|
+
if ( customWords && getType(customWords) === 'Object' ) {
|
|
17
19
|
const replacements = Object.entries( customWords ).map(([ key, value ]) => [
|
|
18
20
|
key.toLowerCase(),
|
|
19
21
|
value,
|
package/src/uniqueItems.ts
CHANGED
package/src/until.ts
CHANGED
|
@@ -9,14 +9,14 @@ type DelayFn = (context: Readonly<UntilContext>) => number;
|
|
|
9
9
|
|
|
10
10
|
type Delay = DelayFn | number;
|
|
11
11
|
|
|
12
|
-
type
|
|
12
|
+
export type UntilOptions = {
|
|
13
13
|
delay: Delay;
|
|
14
14
|
timeout?: Delay;
|
|
15
15
|
callLimit?: number;
|
|
16
16
|
};
|
|
17
17
|
|
|
18
18
|
export interface UntilContext {
|
|
19
|
-
options:
|
|
19
|
+
options: UntilOptions;
|
|
20
20
|
}
|
|
21
21
|
|
|
22
22
|
type ResolverValue<T> = T | PromiseLike<T>;
|
|
@@ -25,7 +25,7 @@ type ResolveFn<T> = (value: ResolverValue<T>) => void;
|
|
|
25
25
|
|
|
26
26
|
type RejectFn = (error?: Error) => void;
|
|
27
27
|
|
|
28
|
-
type UntilCallback<T> = (resolve: ResolveFn<T>, reject: RejectFn, context: Readonly<UntilContext>) => void;
|
|
28
|
+
export type UntilCallback<T> = (resolve: ResolveFn<T>, reject: RejectFn, context: Readonly<UntilContext>) => void;
|
|
29
29
|
|
|
30
30
|
function looksLikeDelay( delay: unknown ) : delay is Delay
|
|
31
31
|
{
|
|
@@ -37,14 +37,14 @@ function getDelay( delay: Delay, context: UntilContext ) : number
|
|
|
37
37
|
return typeof delay === 'function' ? delay( context ) : delay;
|
|
38
38
|
}
|
|
39
39
|
|
|
40
|
-
const defaultOptions:
|
|
40
|
+
const defaultOptions: UntilOptions = {
|
|
41
41
|
delay: 10,
|
|
42
42
|
};
|
|
43
43
|
|
|
44
44
|
/**
|
|
45
45
|
* Return a Promise that delegates resolving/rejecting to the passed in function.
|
|
46
46
|
*/
|
|
47
|
-
export function until<T>( fn: UntilCallback<T>, options:
|
|
47
|
+
export function until<T>( fn: UntilCallback<T>, options: UntilOptions = defaultOptions ) : Promise<T>
|
|
48
48
|
{
|
|
49
49
|
if ( typeof fn !== 'function' ) {
|
|
50
50
|
throw new Error('fn must be a function');
|
|
@@ -122,7 +122,7 @@ export function until<T>( fn: UntilCallback<T>, options: Options = defaultOption
|
|
|
122
122
|
timer = setTimeout( callUntilDone, getDelay( delay, context ), fn, delay, resolve, reject );
|
|
123
123
|
}
|
|
124
124
|
} catch ( error ) {
|
|
125
|
-
reject( error );
|
|
125
|
+
reject( error instanceof Error ? error : new Error(`${error}`) );
|
|
126
126
|
}
|
|
127
127
|
};
|
|
128
128
|
|