@webdeveric/utils 0.9.0 → 0.12.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/clamp.js +10 -0
- package/dist/cjs/clamp.js.map +1 -0
- package/dist/cjs/convert.js +19 -0
- package/dist/cjs/convert.js.map +1 -0
- package/dist/cjs/getMaxValue.js +14 -0
- package/dist/cjs/getMaxValue.js.map +1 -0
- package/dist/cjs/getMinValue.js +14 -0
- package/dist/cjs/getMinValue.js.map +1 -0
- 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 +14 -1
- 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/isObject.js +8 -0
- package/dist/cjs/isObject.js.map +1 -0
- package/dist/cjs/node/index.js +5 -1
- package/dist/cjs/node/index.js.map +1 -1
- package/dist/cjs/normalize.js +14 -15
- package/dist/cjs/normalize.js.map +1 -1
- package/dist/cjs/type-assertion.js +16 -0
- package/dist/cjs/type-assertion.js.map +1 -0
- package/dist/cjs/types.js +3 -0
- package/dist/cjs/types.js.map +1 -0
- package/dist/cjs/until.js +10 -13
- package/dist/cjs/until.js.map +1 -1
- package/dist/mjs/clamp.js +6 -0
- package/dist/mjs/clamp.js.map +1 -0
- package/dist/mjs/compose.js +1 -2
- package/dist/mjs/compose.js.map +1 -1
- package/dist/mjs/convert.js +15 -0
- package/dist/mjs/convert.js.map +1 -0
- package/dist/mjs/getMaxValue.js +10 -0
- package/dist/mjs/getMaxValue.js.map +1 -0
- package/dist/mjs/getMinValue.js +10 -0
- package/dist/mjs/getMinValue.js.map +1 -0
- 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 +9 -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/isObject.js +4 -0
- package/dist/mjs/isObject.js.map +1 -0
- package/dist/mjs/normalize.js +14 -15
- package/dist/mjs/normalize.js.map +1 -1
- package/dist/mjs/type-assertion.js +11 -0
- package/dist/mjs/type-assertion.js.map +1 -0
- package/dist/mjs/types.js +2 -0
- package/dist/mjs/types.js.map +1 -0
- package/dist/mjs/until.js +10 -13
- package/dist/mjs/until.js.map +1 -1
- package/dist/types/clamp.d.ts +3 -0
- package/dist/types/clamp.d.ts.map +1 -0
- package/dist/types/convert.d.ts +8 -0
- package/dist/types/convert.d.ts.map +1 -0
- package/dist/types/getMaxValue.d.ts +3 -0
- package/dist/types/getMaxValue.d.ts.map +1 -0
- package/dist/types/getMinValue.d.ts +3 -0
- package/dist/types/getMinValue.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 +9 -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/isObject.d.ts +2 -0
- package/dist/types/isObject.d.ts.map +1 -0
- package/dist/types/normalize.d.ts +8 -6
- package/dist/types/normalize.d.ts.map +1 -1
- package/dist/types/type-assertion.d.ts +3 -0
- package/dist/types/type-assertion.d.ts.map +1 -0
- package/dist/types/types.d.ts +7 -0
- package/dist/types/types.d.ts.map +1 -0
- package/dist/types/until.d.ts +8 -10
- package/dist/types/until.d.ts.map +1 -1
- package/package.json +27 -12
- package/src/clamp.ts +7 -0
- package/src/convert.ts +30 -0
- package/src/getMaxValue.ts +16 -0
- package/src/getMinValue.ts +16 -0
- package/src/getOwnKeys.ts +2 -5
- package/src/getOwnProperties.ts +6 -0
- package/src/index.ts +9 -0
- package/src/isEmpty.ts +15 -0
- package/src/isObject.ts +3 -0
- package/src/normalize.ts +36 -29
- package/src/type-assertion.ts +11 -0
- package/src/types.ts +12 -0
- package/src/until.ts +22 -29
package/src/normalize.ts
CHANGED
|
@@ -2,26 +2,32 @@ import cloneDeep from 'lodash.clonedeep';
|
|
|
2
2
|
|
|
3
3
|
import type { Builtin } from 'ts-essentials';
|
|
4
4
|
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
5
|
+
import { getOwnProperties } from './getOwnProperties.js';
|
|
6
|
+
import { isObject } from './isObject.js';
|
|
7
7
|
|
|
8
|
-
|
|
8
|
+
import type { AnyRecord } from './types';
|
|
9
|
+
|
|
10
|
+
export type NormalizeContext<OwnerRecordType, ContextData extends AnyRecord> = {
|
|
9
11
|
original: Readonly<OwnerRecordType>;
|
|
10
12
|
current: OwnerRecordType;
|
|
11
|
-
data:
|
|
13
|
+
data: ContextData;
|
|
12
14
|
};
|
|
13
15
|
|
|
14
|
-
export type NormalizerFn<
|
|
15
|
-
input:
|
|
16
|
-
context: NormalizeContext<OwnerRecordType>,
|
|
17
|
-
) =>
|
|
16
|
+
export type NormalizerFn<PropertyType, OwnerRecordType, ContextData extends AnyRecord = AnyRecord> = (
|
|
17
|
+
input: PropertyType,
|
|
18
|
+
context: NormalizeContext<OwnerRecordType, ContextData>,
|
|
19
|
+
) => PropertyType;
|
|
18
20
|
|
|
19
|
-
export type NormalizersRecord<RecordType, OwnerRecordType =
|
|
20
|
-
[Property in keyof RecordType]?: RecordType[Property] extends Builtin
|
|
21
|
-
? NormalizerFn<RecordType[Property], OwnerRecordType>
|
|
22
|
-
:
|
|
21
|
+
export type NormalizersRecord<RecordType, OwnerRecordType, ContextData extends AnyRecord = AnyRecord> = {
|
|
22
|
+
readonly [Property in keyof RecordType]?: RecordType[Property] extends Builtin
|
|
23
|
+
? NormalizerFn<RecordType[Property], OwnerRecordType, ContextData>
|
|
24
|
+
: AnyNormalizer<RecordType[Property], OwnerRecordType, ContextData>
|
|
23
25
|
};
|
|
24
26
|
|
|
27
|
+
export type AnyNormalizer<RecordType, OwnerRecordType, ContextData extends AnyRecord = AnyRecord> =
|
|
28
|
+
| NormalizerFn<RecordType, OwnerRecordType, ContextData>
|
|
29
|
+
| NormalizersRecord<RecordType, OwnerRecordType, ContextData>;
|
|
30
|
+
|
|
25
31
|
/**
|
|
26
32
|
* @example
|
|
27
33
|
*
|
|
@@ -37,44 +43,45 @@ export type NormalizersRecord<RecordType, OwnerRecordType = RecordType> = {
|
|
|
37
43
|
* normalize(true, () => false );
|
|
38
44
|
* ```
|
|
39
45
|
*/
|
|
40
|
-
export function normalize<Data>(
|
|
41
|
-
input: Data
|
|
42
|
-
normalizers?:
|
|
46
|
+
export function normalize<Data, ContextData extends AnyRecord = AnyRecord>(
|
|
47
|
+
input: Readonly<Data>,
|
|
48
|
+
normalizers?: AnyNormalizer<Data, Data, ContextData>,
|
|
49
|
+
initContextData: (data: typeof input, dataNormalizers: typeof normalizers) => ContextData = (): AnyRecord => ({}),
|
|
43
50
|
): Data {
|
|
44
|
-
const
|
|
51
|
+
const current = cloneDeep(input);
|
|
45
52
|
|
|
46
|
-
const context: NormalizeContext<Data> = Object.seal({
|
|
53
|
+
const context: NormalizeContext<Data, ContextData> = Object.seal({
|
|
47
54
|
original: input,
|
|
48
|
-
current
|
|
49
|
-
data:
|
|
55
|
+
current,
|
|
56
|
+
data: initContextData(input, normalizers),
|
|
50
57
|
});
|
|
51
58
|
|
|
52
59
|
const walk = <CurrentRecord>(
|
|
53
60
|
currentRecord: CurrentRecord,
|
|
54
|
-
currentNormalizers?: NormalizerFn<CurrentRecord, Data> | NormalizersRecord<CurrentRecord, Data>,
|
|
61
|
+
currentNormalizers?: NormalizerFn<CurrentRecord, Data, ContextData> | NormalizersRecord<CurrentRecord, Data, ContextData>,
|
|
55
62
|
): CurrentRecord => {
|
|
56
|
-
if (typeof currentNormalizers === 'undefined') {
|
|
57
|
-
return currentRecord;
|
|
58
|
-
}
|
|
59
|
-
|
|
60
63
|
if (typeof currentNormalizers === 'function') {
|
|
61
64
|
return currentNormalizers(currentRecord, context);
|
|
62
65
|
}
|
|
63
66
|
|
|
64
67
|
if (typeof currentNormalizers === 'object' && currentNormalizers !== null) {
|
|
65
68
|
if (typeof currentRecord === 'object' && currentRecord !== null) {
|
|
66
|
-
return
|
|
67
|
-
|
|
69
|
+
return getOwnProperties(currentNormalizers).reduce((data, key) => {
|
|
70
|
+
const normalizers = currentNormalizers[ key ];
|
|
71
|
+
// If the value is undefined but there is a normalizer record, set value to empty object.
|
|
72
|
+
const value = typeof data[ key ] === 'undefined' && isObject(normalizers)
|
|
73
|
+
? {} as CurrentRecord[keyof CurrentRecord]
|
|
74
|
+
: data[ key ];
|
|
75
|
+
|
|
76
|
+
data[ key ] = walk(value, normalizers);
|
|
68
77
|
|
|
69
78
|
return data;
|
|
70
79
|
}, currentRecord);
|
|
71
80
|
}
|
|
72
|
-
} else {
|
|
73
|
-
throw new TypeError(`normalizers must be function or object. ${getType(currentNormalizers)} was received.`);
|
|
74
81
|
}
|
|
75
82
|
|
|
76
83
|
return currentRecord;
|
|
77
84
|
};
|
|
78
85
|
|
|
79
|
-
return walk(
|
|
86
|
+
return walk(current, normalizers);
|
|
80
87
|
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export function assertsIsInteger(input: unknown): asserts input is number {
|
|
2
|
+
if (! Number.isInteger(input)) {
|
|
3
|
+
throw new TypeError('input is not an integer');
|
|
4
|
+
}
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
export function assertsIsFiniteNumber(input: unknown): asserts input is number {
|
|
8
|
+
if (! Number.isFinite(input)) {
|
|
9
|
+
throw new TypeError('input is not a finite number');
|
|
10
|
+
}
|
|
11
|
+
}
|
package/src/types.ts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
2
|
+
export type AnyRecord = Record<PropertyKey, any>;
|
|
3
|
+
|
|
4
|
+
export type UnknownRecord = Record<PropertyKey, unknown>;
|
|
5
|
+
|
|
6
|
+
export type NumericString<T extends string | number = number> = `${T}` extends `${number}` ? `${T}` : never;
|
|
7
|
+
|
|
8
|
+
export type NumericValue = number | bigint | NumericString;
|
|
9
|
+
|
|
10
|
+
export type MaybePlural<T extends string> = T | `${T}s`;
|
|
11
|
+
|
|
12
|
+
export type StringKeys<T> = keyof T & string;
|
package/src/until.ts
CHANGED
|
@@ -1,13 +1,16 @@
|
|
|
1
|
+
import type { AnyRecord } from './types';
|
|
2
|
+
import { getType } from './getType.js';
|
|
3
|
+
|
|
1
4
|
export interface UntilContext {
|
|
2
5
|
callCount: number;
|
|
3
6
|
lastCall?: number;
|
|
4
|
-
|
|
5
|
-
|
|
7
|
+
data: AnyRecord;
|
|
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 ) => {
|