@webdeveric/utils 0.8.0 → 0.11.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/convert.js +19 -0
- package/dist/cjs/convert.js.map +1 -0
- package/dist/cjs/delayAnimationFrame.js +3 -3
- package/dist/cjs/delayAnimationFrame.js.map +1 -1
- package/dist/cjs/getOwnKeys.js +1 -4
- package/dist/cjs/getOwnKeys.js.map +1 -1
- package/dist/cjs/getOwnProperties.js +11 -0
- package/dist/cjs/getOwnProperties.js.map +1 -0
- package/dist/cjs/index.js +3 -0
- package/dist/cjs/index.js.map +1 -1
- package/dist/cjs/isEmpty.js +17 -0
- package/dist/cjs/isEmpty.js.map +1 -0
- package/dist/cjs/node/fcrdns.js +7 -8
- package/dist/cjs/node/fcrdns.js.map +1 -1
- package/dist/cjs/normalize.js +37 -37
- package/dist/cjs/normalize.js.map +1 -1
- package/dist/cjs/secToString.js +36 -23
- package/dist/cjs/secToString.js.map +1 -1
- package/dist/cjs/toPascalCase.js +2 -2
- package/dist/cjs/toPascalCase.js.map +1 -1
- package/dist/cjs/uniqueItems.js.map +1 -1
- package/dist/cjs/until.js +10 -13
- package/dist/cjs/until.js.map +1 -1
- package/dist/mjs/asArray.js.map +1 -1
- package/dist/mjs/convert.js +15 -0
- package/dist/mjs/convert.js.map +1 -0
- package/dist/mjs/delayAnimationFrame.js +3 -3
- package/dist/mjs/delayAnimationFrame.js.map +1 -1
- package/dist/mjs/getOwnKeys.js +1 -4
- package/dist/mjs/getOwnKeys.js.map +1 -1
- package/dist/mjs/getOwnProperties.js +7 -0
- package/dist/mjs/getOwnProperties.js.map +1 -0
- package/dist/mjs/index.js +3 -0
- package/dist/mjs/index.js.map +1 -1
- package/dist/mjs/isEmpty.js +13 -0
- package/dist/mjs/isEmpty.js.map +1 -0
- package/dist/mjs/node/fcrdns.js +4 -5
- package/dist/mjs/node/fcrdns.js.map +1 -1
- package/dist/mjs/normalize.js +37 -37
- package/dist/mjs/normalize.js.map +1 -1
- package/dist/mjs/secToString.js +35 -22
- package/dist/mjs/secToString.js.map +1 -1
- package/dist/mjs/toPascalCase.js +2 -2
- package/dist/mjs/toPascalCase.js.map +1 -1
- package/dist/mjs/uniqueItems.js.map +1 -1
- package/dist/mjs/until.js +10 -13
- 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/convert.d.ts +8 -0
- package/dist/types/convert.d.ts.map +1 -0
- package/dist/types/getOwnKeys.d.ts +1 -1
- package/dist/types/getOwnKeys.d.ts.map +1 -1
- package/dist/types/getOwnProperties.d.ts +2 -0
- package/dist/types/getOwnProperties.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/isEmpty.d.ts +2 -0
- package/dist/types/isEmpty.d.ts.map +1 -0
- package/dist/types/node/fcrdns.d.ts.map +1 -1
- package/dist/types/normalize.d.ts +23 -9
- package/dist/types/normalize.d.ts.map +1 -1
- package/dist/types/secToString.d.ts +13 -1
- package/dist/types/secToString.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 +7 -10
- package/dist/types/until.d.ts.map +1 -1
- package/package.json +17 -9
- package/src/asArray.ts +1 -1
- package/src/convert.ts +30 -0
- package/src/delayAnimationFrame.ts +3 -3
- package/src/getOwnKeys.ts +2 -5
- package/src/getOwnProperties.ts +6 -0
- package/src/index.ts +3 -0
- package/src/isEmpty.ts +15 -0
- package/src/node/fcrdns.ts +7 -8
- package/src/normalize.ts +56 -59
- package/src/secToString.ts +39 -24
- package/src/toPascalCase.ts +2 -2
- package/src/uniqueItems.ts +1 -1
- package/src/until.ts +21 -28
package/src/index.ts
CHANGED
|
@@ -1,9 +1,12 @@
|
|
|
1
1
|
export * from './asArray.js';
|
|
2
2
|
export * from './compose.js';
|
|
3
|
+
export * from './convert.js';
|
|
3
4
|
export * from './delay.js';
|
|
4
5
|
export * from './delayAnimationFrame.js';
|
|
5
6
|
export * from './getOwnKeys.js';
|
|
7
|
+
export * from './getOwnProperties.js';
|
|
6
8
|
export * from './getType.js';
|
|
9
|
+
export * from './isEmpty.js';
|
|
7
10
|
export * from './joinStrings.js';
|
|
8
11
|
export * from './normalize.js';
|
|
9
12
|
export * from './secToString.js';
|
package/src/isEmpty.ts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
export function isEmpty(input: unknown): boolean {
|
|
2
|
+
if (typeof input === 'undefined' || input === null || (typeof input === 'string' && ! input.length)) {
|
|
3
|
+
return true;
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
if (Array.isArray(input)) {
|
|
7
|
+
return input.length === 0;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
if (typeof input === 'object') {
|
|
11
|
+
return Object.keys(input).length === 0;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
return false;
|
|
15
|
+
}
|
package/src/node/fcrdns.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { promises as dns } from 'dns';
|
|
2
|
-
import { isIP } from 'net';
|
|
1
|
+
import { promises as dns } from 'node:dns';
|
|
2
|
+
import { isIP } from 'node:net';
|
|
3
3
|
|
|
4
4
|
export async function hostnameResolvesToIp( hostname: string, ip: string ) : Promise<boolean>
|
|
5
5
|
{
|
|
@@ -11,9 +11,9 @@ export async function hostnameResolvesToIp( hostname: string, ip: string ) : Pro
|
|
|
11
11
|
|
|
12
12
|
const resolve = ipVersion === 4 ? dns.resolve4 : dns.resolve6;
|
|
13
13
|
|
|
14
|
-
const resolved
|
|
14
|
+
const resolved = await resolve( hostname );
|
|
15
15
|
|
|
16
|
-
return resolved.some(
|
|
16
|
+
return resolved.some( addr => addr === ip );
|
|
17
17
|
}
|
|
18
18
|
|
|
19
19
|
/**
|
|
@@ -23,18 +23,17 @@ export async function hostnameResolvesToIp( hostname: string, ip: string ) : Pro
|
|
|
23
23
|
*/
|
|
24
24
|
export async function fcrdns( hostname: string, ip: string ) : Promise<boolean>
|
|
25
25
|
{
|
|
26
|
-
const hostnames
|
|
26
|
+
const hostnames = await dns.reverse(ip);
|
|
27
27
|
|
|
28
28
|
if ( ! hostnames.length ) {
|
|
29
29
|
throw new Error(`Unable to get hostnames for ${ip}`);
|
|
30
30
|
}
|
|
31
31
|
|
|
32
|
-
if ( ! hostnames.every(
|
|
32
|
+
if ( ! hostnames.every( name => name.endsWith( hostname ) ) ) {
|
|
33
33
|
throw new Error(`${ip} does not belong to ${hostname}`);
|
|
34
34
|
}
|
|
35
35
|
|
|
36
|
-
|
|
37
|
-
const results: boolean[] = await Promise.all(
|
|
36
|
+
const results = await Promise.all(
|
|
38
37
|
hostnames.map(
|
|
39
38
|
name => hostnameResolvesToIp( name, ip ),
|
|
40
39
|
),
|
package/src/normalize.ts
CHANGED
|
@@ -2,82 +2,79 @@ import cloneDeep from 'lodash.clonedeep';
|
|
|
2
2
|
|
|
3
3
|
import type { Builtin } from 'ts-essentials';
|
|
4
4
|
|
|
5
|
-
import {
|
|
5
|
+
import { getOwnProperties } from './getOwnProperties.js';
|
|
6
|
+
import { getType } from './getType.js';
|
|
6
7
|
|
|
7
|
-
export type NormalizeContext<
|
|
8
|
-
|
|
9
|
-
current:
|
|
10
|
-
data: Record<
|
|
8
|
+
export type NormalizeContext<OwnerRecordType> = {
|
|
9
|
+
original: Readonly<OwnerRecordType>;
|
|
10
|
+
current: OwnerRecordType;
|
|
11
|
+
data: Record<PropertyKey, unknown>;
|
|
11
12
|
};
|
|
12
13
|
|
|
13
|
-
export type NormalizerFn<
|
|
14
|
-
input:
|
|
15
|
-
context: NormalizeContext<
|
|
16
|
-
) =>
|
|
14
|
+
export type NormalizerFn<OutputType, OwnerRecordType> = (
|
|
15
|
+
input: unknown,
|
|
16
|
+
context: NormalizeContext<OwnerRecordType>,
|
|
17
|
+
) => OutputType;
|
|
17
18
|
|
|
18
|
-
export type
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
? NormalizerFn<T[P], R>
|
|
23
|
-
: (NormalizerFn<T[P], R> | NormalizersRecord<T[P], T> | NormalizersTuple<T[P], R>);
|
|
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>;
|
|
24
23
|
};
|
|
25
24
|
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
};
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
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,
|
|
42
49
|
data: {},
|
|
43
|
-
};
|
|
50
|
+
});
|
|
44
51
|
|
|
45
|
-
const walk = <
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
throw new TypeError(`initialize is not a function: ${typeof initialize} was provided.`);
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
return walk(
|
|
54
|
-
currentRecord ?? initialize(currentRecord, context),
|
|
55
|
-
normalizers,
|
|
56
|
-
);
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
if (typeof currentRecord === 'undefined') {
|
|
52
|
+
const walk = <CurrentRecord>(
|
|
53
|
+
currentRecord: CurrentRecord,
|
|
54
|
+
currentNormalizers?: NormalizerFn<CurrentRecord, Data> | NormalizersRecord<CurrentRecord, Data>,
|
|
55
|
+
): CurrentRecord => {
|
|
56
|
+
if (typeof currentNormalizers === 'undefined') {
|
|
60
57
|
return currentRecord;
|
|
61
58
|
}
|
|
62
59
|
|
|
63
|
-
|
|
60
|
+
if (typeof currentNormalizers === 'function') {
|
|
61
|
+
return currentNormalizers(currentRecord, context);
|
|
62
|
+
}
|
|
64
63
|
|
|
65
|
-
|
|
66
|
-
|
|
64
|
+
if (typeof currentNormalizers === 'object' && currentNormalizers !== null) {
|
|
65
|
+
if (typeof currentRecord === 'object' && currentRecord !== null) {
|
|
66
|
+
return getOwnProperties(currentNormalizers).reduce((data, key) => {
|
|
67
|
+
data[ key ] = walk(data[ key ], currentNormalizers[ key ]);
|
|
67
68
|
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
} else if (isObject<NormalizersRecord<CR[keyof CR], R>>(normalizer)) {
|
|
71
|
-
currentRecord[ key ] = walk(currentRecord[ key ], normalizer);
|
|
72
|
-
} else if (isNormalizersTuple<CR[keyof CR], R>(normalizer)) {
|
|
73
|
-
currentRecord[ key ] = walk(currentRecord[ key ], normalizer);
|
|
74
|
-
} else {
|
|
75
|
-
throw new TypeError(`Unknown normalizer: ${typeof normalizer}`);
|
|
69
|
+
return data;
|
|
70
|
+
}, currentRecord);
|
|
76
71
|
}
|
|
72
|
+
} else {
|
|
73
|
+
throw new TypeError(`normalizers must be function or object. ${getType(currentNormalizers)} was received.`);
|
|
77
74
|
}
|
|
78
75
|
|
|
79
76
|
return currentRecord;
|
|
80
77
|
};
|
|
81
78
|
|
|
82
|
-
return walk
|
|
79
|
+
return walk(normalized, normalizers);
|
|
83
80
|
}
|
package/src/secToString.ts
CHANGED
|
@@ -1,33 +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
|
|
32
|
-
return
|
|
47
|
+
return parts.join(joinString).replace('centurys', 'centuries');
|
|
33
48
|
}
|
package/src/toPascalCase.ts
CHANGED
|
@@ -10,9 +10,9 @@ export function toPascalCase(text: string, customWords?: Record<string, string>)
|
|
|
10
10
|
|
|
11
11
|
// Remove apostrophes then uppercase the first letter of each word.
|
|
12
12
|
words = words.map( word => {
|
|
13
|
-
const
|
|
13
|
+
const fixedWord = word.replace(/'/g, '');
|
|
14
14
|
|
|
15
|
-
return
|
|
15
|
+
return fixedWord[ 0 ].toUpperCase() + fixedWord.substring(1);
|
|
16
16
|
});
|
|
17
17
|
|
|
18
18
|
if ( customWords && getType(customWords) === 'Object' ) {
|
package/src/uniqueItems.ts
CHANGED
package/src/until.ts
CHANGED
|
@@ -1,13 +1,16 @@
|
|
|
1
|
+
import { getType } from './getType.js';
|
|
2
|
+
|
|
1
3
|
export interface UntilContext {
|
|
2
4
|
callCount: number;
|
|
3
5
|
lastCall?: number;
|
|
4
6
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
5
|
-
data: Record<
|
|
7
|
+
data: Record<PropertyKey, any>;
|
|
8
|
+
options: UntilOptions;
|
|
6
9
|
}
|
|
7
10
|
|
|
8
|
-
type DelayFn = (context: Readonly<UntilContext>) => number;
|
|
11
|
+
export type DelayFn = (context: Readonly<UntilContext>) => number;
|
|
9
12
|
|
|
10
|
-
type Delay = DelayFn | number;
|
|
13
|
+
export type Delay = DelayFn | number;
|
|
11
14
|
|
|
12
15
|
export type UntilOptions = {
|
|
13
16
|
delay: Delay;
|
|
@@ -15,29 +18,15 @@ export type UntilOptions = {
|
|
|
15
18
|
callLimit?: number;
|
|
16
19
|
};
|
|
17
20
|
|
|
18
|
-
export
|
|
19
|
-
options: UntilOptions;
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
type ResolverValue<T> = T | PromiseLike<T>;
|
|
21
|
+
export type ResolverValue<T> = T | PromiseLike<T>;
|
|
23
22
|
|
|
24
|
-
type ResolveFn<T> = (value: ResolverValue<T>) => void;
|
|
23
|
+
export type ResolveFn<T> = (value: ResolverValue<T>) => void;
|
|
25
24
|
|
|
26
|
-
type RejectFn = (error?: Error) => void;
|
|
25
|
+
export type RejectFn = (error?: Error) => void;
|
|
27
26
|
|
|
28
27
|
export type UntilCallback<T> = (resolve: ResolveFn<T>, reject: RejectFn, context: Readonly<UntilContext>) => void;
|
|
29
28
|
|
|
30
|
-
|
|
31
|
-
{
|
|
32
|
-
return Number.isInteger( delay ) || typeof delay === 'function';
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
function getDelay( delay: Delay, context: UntilContext ) : number
|
|
36
|
-
{
|
|
37
|
-
return typeof delay === 'function' ? delay( context ) : delay;
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
const defaultOptions: UntilOptions = {
|
|
29
|
+
const defaultOptions: Readonly<UntilOptions> = {
|
|
41
30
|
delay: 10,
|
|
42
31
|
};
|
|
43
32
|
|
|
@@ -47,25 +36,29 @@ const defaultOptions: UntilOptions = {
|
|
|
47
36
|
export function until<T>( fn: UntilCallback<T>, options: UntilOptions = defaultOptions ) : Promise<T>
|
|
48
37
|
{
|
|
49
38
|
if ( typeof fn !== 'function' ) {
|
|
50
|
-
|
|
39
|
+
return Promise.reject(new Error('fn must be a function'));
|
|
51
40
|
}
|
|
52
41
|
|
|
53
42
|
if ( ! options || typeof options !== 'object' ) {
|
|
54
|
-
|
|
43
|
+
return Promise.reject(new Error(`options must be an object. ${getType(options)} provided.`));
|
|
55
44
|
}
|
|
56
45
|
|
|
57
46
|
const { delay, timeout, callLimit } = Object.assign({}, defaultOptions, options);
|
|
58
47
|
|
|
59
|
-
|
|
60
|
-
|
|
48
|
+
const isDelay = ( input: unknown ) : input is Delay => Number.isInteger( input ) || typeof input === 'function';
|
|
49
|
+
|
|
50
|
+
const getDelay = ( delay: Delay, context: UntilContext ) : number => typeof delay === 'function' ? delay( context ) : delay;
|
|
51
|
+
|
|
52
|
+
if ( ! isDelay( delay ) ) {
|
|
53
|
+
return Promise.reject(new Error('invalid delay value'));
|
|
61
54
|
}
|
|
62
55
|
|
|
63
|
-
if ( timeout !== undefined && !
|
|
64
|
-
|
|
56
|
+
if ( timeout !== undefined && ! isDelay( timeout ) ) {
|
|
57
|
+
return Promise.reject(new Error('invalid timeout value'));
|
|
65
58
|
}
|
|
66
59
|
|
|
67
60
|
if ( callLimit !== undefined && ! Number.isInteger( callLimit ) ) {
|
|
68
|
-
|
|
61
|
+
return Promise.reject(new Error('invalid callLimit value'));
|
|
69
62
|
}
|
|
70
63
|
|
|
71
64
|
return new Promise<T>( ( resolutionFn: ResolveFn<T>, rejectionFn: RejectFn ) => {
|