@webdeveric/utils 0.13.0 → 0.15.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.map +1 -1
- package/dist/cjs/delay.js.map +1 -1
- package/dist/cjs/delayAnimationFrame.js.map +1 -1
- package/dist/cjs/getMaxValue.js.map +1 -1
- package/dist/cjs/getMinValue.js.map +1 -1
- package/dist/cjs/getOwnProperties.js +1 -4
- package/dist/cjs/getOwnProperties.js.map +1 -1
- package/dist/cjs/isEmpty.js +5 -1
- package/dist/cjs/isEmpty.js.map +1 -1
- package/dist/cjs/node/fcrdns.js.map +1 -1
- package/dist/cjs/normalize.js +0 -15
- package/dist/cjs/normalize.js.map +1 -1
- package/dist/cjs/secToString.js.map +1 -1
- package/dist/cjs/toPascalCase.js +1 -4
- package/dist/cjs/toPascalCase.js.map +1 -1
- package/dist/cjs/type-assertion.js.map +1 -1
- package/dist/cjs/type-predicate.js +5 -1
- package/dist/cjs/type-predicate.js.map +1 -1
- package/dist/cjs/uniqueItems.js.map +1 -1
- package/dist/cjs/until.js +9 -8
- package/dist/cjs/until.js.map +1 -1
- package/dist/mjs/asArray.js.map +1 -1
- package/dist/mjs/convert.js.map +1 -1
- package/dist/mjs/delay.js.map +1 -1
- package/dist/mjs/delayAnimationFrame.js.map +1 -1
- package/dist/mjs/getMaxValue.js.map +1 -1
- package/dist/mjs/getMinValue.js.map +1 -1
- package/dist/mjs/getOwnProperties.js +1 -4
- package/dist/mjs/getOwnProperties.js.map +1 -1
- package/dist/mjs/isEmpty.js +5 -1
- package/dist/mjs/isEmpty.js.map +1 -1
- package/dist/mjs/node/fcrdns.js.map +1 -1
- package/dist/mjs/normalize.js +0 -15
- package/dist/mjs/normalize.js.map +1 -1
- package/dist/mjs/secToString.js.map +1 -1
- package/dist/mjs/toPascalCase.js +1 -4
- package/dist/mjs/toPascalCase.js.map +1 -1
- package/dist/mjs/type-assertion.js.map +1 -1
- package/dist/mjs/type-predicate.js +3 -0
- package/dist/mjs/type-predicate.js.map +1 -1
- package/dist/mjs/uniqueItems.js.map +1 -1
- package/dist/mjs/until.js +5 -5
- package/dist/mjs/until.js.map +1 -1
- package/dist/types/asArray.d.ts.map +1 -1
- package/dist/types/delay.d.ts.map +1 -1
- package/dist/types/delayAnimationFrame.d.ts.map +1 -1
- package/dist/types/getMaxValue.d.ts.map +1 -1
- package/dist/types/getMinValue.d.ts.map +1 -1
- package/dist/types/getOwnProperties.d.ts.map +1 -1
- package/dist/types/isEmpty.d.ts.map +1 -1
- package/dist/types/node/fcrdns.d.ts.map +1 -1
- package/dist/types/normalize.d.ts +4 -2
- package/dist/types/normalize.d.ts.map +1 -1
- package/dist/types/secToString.d.ts.map +1 -1
- package/dist/types/toPascalCase.d.ts.map +1 -1
- package/dist/types/type-predicate.d.ts +3 -0
- package/dist/types/type-predicate.d.ts.map +1 -1
- package/dist/types/types.d.ts +3 -0
- package/dist/types/types.d.ts.map +1 -1
- package/dist/types/uniqueItems.d.ts.map +1 -1
- package/dist/types/until.d.ts +2 -1
- package/dist/types/until.d.ts.map +1 -1
- package/package.json +32 -13
- package/readme.md +1 -1
- package/src/asArray.ts +2 -3
- package/src/convert.ts +1 -1
- package/src/delay.ts +6 -7
- package/src/delayAnimationFrame.ts +6 -8
- package/src/getMaxValue.ts +4 -6
- package/src/getMinValue.ts +4 -6
- package/src/getOwnProperties.ts +1 -4
- package/src/isEmpty.ts +8 -2
- package/src/node/fcrdns.ts +10 -16
- package/src/normalize.ts +26 -9
- package/src/secToString.ts +19 -22
- package/src/toPascalCase.ts +9 -13
- package/src/type-assertion.ts +3 -3
- package/src/type-predicate.ts +5 -1
- package/src/types.ts +7 -0
- package/src/uniqueItems.ts +2 -3
- package/src/until.ts +35 -38
package/src/until.ts
CHANGED
|
@@ -3,7 +3,7 @@ import { getType } from './getType.js';
|
|
|
3
3
|
|
|
4
4
|
export interface UntilContext {
|
|
5
5
|
callCount: number;
|
|
6
|
-
lastCall
|
|
6
|
+
lastCall: number | undefined;
|
|
7
7
|
data: AnyRecord;
|
|
8
8
|
options: UntilOptions;
|
|
9
9
|
}
|
|
@@ -30,38 +30,38 @@ const defaultOptions: Readonly<UntilOptions> = {
|
|
|
30
30
|
delay: 10,
|
|
31
31
|
};
|
|
32
32
|
|
|
33
|
+
export const isDelay = (input: unknown): input is Delay => Number.isInteger(input) || typeof input === 'function';
|
|
34
|
+
|
|
33
35
|
/**
|
|
34
36
|
* Return a Promise that delegates resolving/rejecting to the passed in function.
|
|
35
37
|
*/
|
|
36
|
-
export function until<T>(
|
|
37
|
-
{
|
|
38
|
-
if ( typeof fn !== 'function' ) {
|
|
38
|
+
export function until<T>(fn: UntilCallback<T>, options: UntilOptions = defaultOptions): Promise<T> {
|
|
39
|
+
if (typeof fn !== 'function') {
|
|
39
40
|
return Promise.reject(new Error('fn must be a function'));
|
|
40
41
|
}
|
|
41
42
|
|
|
42
|
-
if (
|
|
43
|
-
return Promise.reject(new
|
|
43
|
+
if (!options || typeof options !== 'object') {
|
|
44
|
+
return Promise.reject(new TypeError(`options must be an object. ${getType(options)} provided.`));
|
|
44
45
|
}
|
|
45
46
|
|
|
46
47
|
const { delay, timeout, callLimit } = Object.assign({}, defaultOptions, options);
|
|
47
48
|
|
|
48
|
-
const
|
|
49
|
-
|
|
50
|
-
const getDelay = ( delay: Delay, context: UntilContext ) : number => typeof delay === 'function' ? delay( context ) : delay;
|
|
49
|
+
const getDelay = (delay: Delay, context: UntilContext): number =>
|
|
50
|
+
typeof delay === 'function' ? delay(context) : delay;
|
|
51
51
|
|
|
52
|
-
if (
|
|
53
|
-
return Promise.reject(new
|
|
52
|
+
if (!isDelay(delay)) {
|
|
53
|
+
return Promise.reject(new TypeError('invalid delay value'));
|
|
54
54
|
}
|
|
55
55
|
|
|
56
|
-
if (
|
|
57
|
-
return Promise.reject(new
|
|
56
|
+
if (timeout !== undefined && !isDelay(timeout)) {
|
|
57
|
+
return Promise.reject(new TypeError('invalid timeout value'));
|
|
58
58
|
}
|
|
59
59
|
|
|
60
|
-
if (
|
|
61
|
-
return Promise.reject(new
|
|
60
|
+
if (callLimit !== undefined && !Number.isInteger(callLimit)) {
|
|
61
|
+
return Promise.reject(new TypeError('invalid callLimit value'));
|
|
62
62
|
}
|
|
63
63
|
|
|
64
|
-
return new Promise<T>(
|
|
64
|
+
return new Promise<T>((resolutionFn: ResolveFn<T>, rejectionFn: RejectFn) => {
|
|
65
65
|
let callCount = 0;
|
|
66
66
|
let lastCall: number | undefined;
|
|
67
67
|
let done = false;
|
|
@@ -70,19 +70,19 @@ export function until<T>( fn: UntilCallback<T>, options: UntilOptions = defaultO
|
|
|
70
70
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
71
71
|
let timeoutTimer: any;
|
|
72
72
|
|
|
73
|
-
const finish = ()
|
|
73
|
+
const finish = (): void => {
|
|
74
74
|
done = true;
|
|
75
|
-
clearTimeout(
|
|
76
|
-
clearTimeout(
|
|
75
|
+
clearTimeout(timer);
|
|
76
|
+
clearTimeout(timeoutTimer);
|
|
77
77
|
};
|
|
78
78
|
|
|
79
|
-
const resolve: ResolveFn<T> = (value: ResolverValue<T>)
|
|
80
|
-
resolutionFn(
|
|
79
|
+
const resolve: ResolveFn<T> = (value: ResolverValue<T>): void => {
|
|
80
|
+
resolutionFn(value);
|
|
81
81
|
finish();
|
|
82
82
|
};
|
|
83
83
|
|
|
84
|
-
const reject: RejectFn = (error?: Error)
|
|
85
|
-
rejectionFn(
|
|
84
|
+
const reject: RejectFn = (error?: Error): void => {
|
|
85
|
+
rejectionFn(error);
|
|
86
86
|
finish();
|
|
87
87
|
};
|
|
88
88
|
|
|
@@ -99,35 +99,32 @@ export function until<T>( fn: UntilCallback<T>, options: UntilOptions = defaultO
|
|
|
99
99
|
data: {},
|
|
100
100
|
});
|
|
101
101
|
|
|
102
|
-
const callUntilDone = (
|
|
102
|
+
const callUntilDone = (fn: UntilCallback<T>, delay: Delay, resolve: ResolveFn<T>, reject: RejectFn): void => {
|
|
103
103
|
try {
|
|
104
|
-
if (
|
|
104
|
+
if (callLimit && callCount >= callLimit) {
|
|
105
105
|
throw new Error(`until callLimit reached: ${callLimit}`);
|
|
106
106
|
}
|
|
107
107
|
|
|
108
108
|
++callCount;
|
|
109
109
|
|
|
110
|
-
fn(
|
|
110
|
+
fn(resolve, reject, context);
|
|
111
111
|
|
|
112
112
|
lastCall = Date.now();
|
|
113
113
|
|
|
114
|
-
if (
|
|
115
|
-
timer = setTimeout(
|
|
114
|
+
if (!done) {
|
|
115
|
+
timer = setTimeout(callUntilDone, getDelay(delay, context), fn, delay, resolve, reject);
|
|
116
116
|
}
|
|
117
|
-
} catch (
|
|
118
|
-
reject(
|
|
117
|
+
} catch (error) {
|
|
118
|
+
reject(error instanceof Error ? error : new Error(`${error}`));
|
|
119
119
|
}
|
|
120
120
|
};
|
|
121
121
|
|
|
122
|
-
callUntilDone(
|
|
122
|
+
callUntilDone(fn, delay, resolve, reject);
|
|
123
123
|
|
|
124
|
-
if (
|
|
125
|
-
timeoutTimer = setTimeout(
|
|
126
|
-
(
|
|
127
|
-
|
|
128
|
-
},
|
|
129
|
-
getDelay( timeout, context ),
|
|
130
|
-
);
|
|
124
|
+
if (!done && timeout !== undefined) {
|
|
125
|
+
timeoutTimer = setTimeout(() => {
|
|
126
|
+
reject(new Error('until: timed out'));
|
|
127
|
+
}, getDelay(timeout, context));
|
|
131
128
|
}
|
|
132
129
|
});
|
|
133
130
|
}
|