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