@webdeveric/utils 0.12.0 → 0.14.1
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 +3 -1
- package/dist/cjs/getMaxValue.js.map +1 -1
- package/dist/cjs/getMinValue.js +3 -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 +3 -6
- package/dist/cjs/toPascalCase.js.map +1 -1
- package/dist/cjs/type-assertion.js +12 -5
- package/dist/cjs/type-assertion.js.map +1 -1
- package/dist/cjs/type-predicate.js +32 -0
- package/dist/cjs/type-predicate.js.map +1 -0
- 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 +3 -1
- package/dist/mjs/getMaxValue.js.map +1 -1
- package/dist/mjs/getMinValue.js +3 -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 +3 -6
- package/dist/mjs/toPascalCase.js.map +1 -1
- package/dist/mjs/type-assertion.js +8 -2
- package/dist/mjs/type-assertion.js.map +1 -1
- package/dist/mjs/type-predicate.js +16 -0
- package/dist/mjs/type-predicate.js.map +1 -0
- 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 +1 -1
- package/dist/types/getMaxValue.d.ts.map +1 -1
- package/dist/types/getMinValue.d.ts +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-assertion.d.ts +4 -2
- package/dist/types/type-assertion.d.ts.map +1 -1
- package/dist/types/type-predicate.d.ts +17 -0
- package/dist/types/type-predicate.d.ts.map +1 -0
- 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 +27 -10
- 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 +8 -8
- package/src/getMinValue.ts +8 -8
- package/src/getOwnProperties.ts +1 -4
- package/src/isEmpty.ts +8 -2
- package/src/node/fcrdns.ts +10 -16
- package/src/normalize.ts +27 -10
- package/src/secToString.ts +19 -22
- package/src/toPascalCase.ts +11 -15
- package/src/type-assertion.ts +13 -4
- package/src/type-predicate.ts +36 -0
- package/src/types.ts +7 -0
- package/src/uniqueItems.ts +2 -3
- package/src/until.ts +35 -38
package/src/normalize.ts
CHANGED
|
@@ -13,7 +13,7 @@ export type NormalizeContext<OwnerRecordType, ContextData extends AnyRecord> = {
|
|
|
13
13
|
data: ContextData;
|
|
14
14
|
};
|
|
15
15
|
|
|
16
|
-
export type NormalizerFn<PropertyType, OwnerRecordType, ContextData extends AnyRecord = AnyRecord> = (
|
|
16
|
+
export type NormalizerFn<PropertyType, OwnerRecordType = unknown, ContextData extends AnyRecord = AnyRecord> = (
|
|
17
17
|
input: PropertyType,
|
|
18
18
|
context: NormalizeContext<OwnerRecordType, ContextData>,
|
|
19
19
|
) => PropertyType;
|
|
@@ -21,13 +21,18 @@ export type NormalizerFn<PropertyType, OwnerRecordType, ContextData extends AnyR
|
|
|
21
21
|
export type NormalizersRecord<RecordType, OwnerRecordType, ContextData extends AnyRecord = AnyRecord> = {
|
|
22
22
|
readonly [Property in keyof RecordType]?: RecordType[Property] extends Builtin
|
|
23
23
|
? NormalizerFn<RecordType[Property], OwnerRecordType, ContextData>
|
|
24
|
-
: AnyNormalizer<RecordType[Property], OwnerRecordType, ContextData
|
|
24
|
+
: AnyNormalizer<RecordType[Property], OwnerRecordType, ContextData>;
|
|
25
25
|
};
|
|
26
26
|
|
|
27
27
|
export type AnyNormalizer<RecordType, OwnerRecordType, ContextData extends AnyRecord = AnyRecord> =
|
|
28
28
|
| NormalizerFn<RecordType, OwnerRecordType, ContextData>
|
|
29
29
|
| NormalizersRecord<RecordType, OwnerRecordType, ContextData>;
|
|
30
30
|
|
|
31
|
+
export type ContextInitializer<OwnerRecordType, ContextData extends AnyRecord = AnyRecord> = (
|
|
32
|
+
data: OwnerRecordType,
|
|
33
|
+
normalizers: AnyNormalizer<OwnerRecordType, OwnerRecordType, ContextData>,
|
|
34
|
+
) => ContextData;
|
|
35
|
+
|
|
31
36
|
/**
|
|
32
37
|
* @example
|
|
33
38
|
*
|
|
@@ -45,8 +50,19 @@ export type AnyNormalizer<RecordType, OwnerRecordType, ContextData extends AnyRe
|
|
|
45
50
|
*/
|
|
46
51
|
export function normalize<Data, ContextData extends AnyRecord = AnyRecord>(
|
|
47
52
|
input: Readonly<Data>,
|
|
48
|
-
normalizers
|
|
49
|
-
|
|
53
|
+
normalizers: AnyNormalizer<Data, Data, Partial<ContextData>>, // No initializer so data must be partial
|
|
54
|
+
): Data;
|
|
55
|
+
|
|
56
|
+
export function normalize<Data, ContextData extends AnyRecord = AnyRecord>(
|
|
57
|
+
input: Readonly<Data>,
|
|
58
|
+
normalizers: AnyNormalizer<Data, Data, ContextData>,
|
|
59
|
+
initContextData: ContextInitializer<Data, ContextData>,
|
|
60
|
+
): Data;
|
|
61
|
+
|
|
62
|
+
export function normalize<Data, ContextData extends AnyRecord = AnyRecord>(
|
|
63
|
+
input: Readonly<Data>,
|
|
64
|
+
normalizers: AnyNormalizer<Data, Data, ContextData>,
|
|
65
|
+
initContextData: ContextInitializer<Data, ContextData> = (): AnyRecord => ({}),
|
|
50
66
|
): Data {
|
|
51
67
|
const current = cloneDeep(input);
|
|
52
68
|
|
|
@@ -58,7 +74,7 @@ export function normalize<Data, ContextData extends AnyRecord = AnyRecord>(
|
|
|
58
74
|
|
|
59
75
|
const walk = <CurrentRecord>(
|
|
60
76
|
currentRecord: CurrentRecord,
|
|
61
|
-
currentNormalizers?:
|
|
77
|
+
currentNormalizers?: AnyNormalizer<CurrentRecord, Data, ContextData>,
|
|
62
78
|
): CurrentRecord => {
|
|
63
79
|
if (typeof currentNormalizers === 'function') {
|
|
64
80
|
return currentNormalizers(currentRecord, context);
|
|
@@ -67,13 +83,14 @@ export function normalize<Data, ContextData extends AnyRecord = AnyRecord>(
|
|
|
67
83
|
if (typeof currentNormalizers === 'object' && currentNormalizers !== null) {
|
|
68
84
|
if (typeof currentRecord === 'object' && currentRecord !== null) {
|
|
69
85
|
return getOwnProperties(currentNormalizers).reduce((data, key) => {
|
|
70
|
-
const normalizers = currentNormalizers[
|
|
86
|
+
const normalizers = currentNormalizers[key];
|
|
71
87
|
// If the value is undefined but there is a normalizer record, set value to empty object.
|
|
72
|
-
const value =
|
|
73
|
-
|
|
74
|
-
|
|
88
|
+
const value =
|
|
89
|
+
typeof data[key] === 'undefined' && isObject(normalizers)
|
|
90
|
+
? ({} as unknown as CurrentRecord[keyof CurrentRecord])
|
|
91
|
+
: data[key];
|
|
75
92
|
|
|
76
|
-
data[
|
|
93
|
+
data[key] = walk(value, normalizers);
|
|
77
94
|
|
|
78
95
|
return data;
|
|
79
96
|
}, currentRecord);
|
package/src/secToString.ts
CHANGED
|
@@ -15,33 +15,30 @@ type SecondsTuple = [key: Lowercase<keyof typeof Seconds>, seconds: Seconds];
|
|
|
15
15
|
|
|
16
16
|
export function secToString(time: number, joinString = ' '): string {
|
|
17
17
|
const secTime: SecondsTuple[] = [
|
|
18
|
-
[
|
|
19
|
-
[
|
|
20
|
-
[
|
|
21
|
-
[
|
|
22
|
-
[
|
|
23
|
-
[
|
|
24
|
-
[
|
|
25
|
-
[
|
|
26
|
-
[
|
|
27
|
-
[
|
|
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
28
|
];
|
|
29
29
|
|
|
30
|
-
const parts = secTime.reduce<string[]>(
|
|
31
|
-
(
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
const suffix = currentValue === 1 ? units : `${units}s`;
|
|
30
|
+
const parts = secTime.reduce<string[]>((output, [units, seconds]) => {
|
|
31
|
+
if (seconds <= time) {
|
|
32
|
+
const currentValue = Math.floor(time / seconds);
|
|
33
|
+
const suffix = currentValue === 1 ? units : `${units}s`;
|
|
35
34
|
|
|
36
|
-
|
|
35
|
+
time %= seconds;
|
|
37
36
|
|
|
38
|
-
|
|
39
|
-
|
|
37
|
+
output.push(`${currentValue} ${suffix}`);
|
|
38
|
+
}
|
|
40
39
|
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
[],
|
|
44
|
-
);
|
|
40
|
+
return output;
|
|
41
|
+
}, []);
|
|
45
42
|
|
|
46
43
|
// cSpell:ignore centurys
|
|
47
44
|
return parts.join(joinString).replace('centurys', 'centuries');
|
package/src/toPascalCase.ts
CHANGED
|
@@ -1,33 +1,29 @@
|
|
|
1
1
|
import { getType } from './getType.js';
|
|
2
2
|
|
|
3
|
-
export function toPascalCase(text: string, customWords?: Record<string, string>)
|
|
4
|
-
|
|
5
|
-
let words = String(text).match( /[A-Z][a-z']+|\d+|[a-z']+/g );
|
|
3
|
+
export function toPascalCase(text: string, customWords?: Record<string, string>): string {
|
|
4
|
+
let words = String(text).match(/[A-Z][a-z']+|\d+|[a-z']+/g);
|
|
6
5
|
|
|
7
|
-
if (
|
|
6
|
+
if (!words) {
|
|
8
7
|
return '';
|
|
9
8
|
}
|
|
10
9
|
|
|
11
10
|
// Remove apostrophes then uppercase the first letter of each word.
|
|
12
|
-
words = words.map(
|
|
11
|
+
words = words.map(word => {
|
|
13
12
|
const fixedWord = word.replace(/'/g, '');
|
|
14
13
|
|
|
15
|
-
return fixedWord
|
|
14
|
+
return fixedWord.substring(0, 1).toUpperCase() + fixedWord.substring(1);
|
|
16
15
|
});
|
|
17
16
|
|
|
18
|
-
if (
|
|
19
|
-
const replacements = Object.entries(
|
|
20
|
-
key.toLowerCase(),
|
|
21
|
-
value,
|
|
22
|
-
]);
|
|
17
|
+
if (customWords && getType(customWords) === 'Object') {
|
|
18
|
+
const replacements = Object.entries(customWords).map(([key, value]) => [key.toLowerCase(), value]);
|
|
23
19
|
|
|
24
|
-
if (
|
|
20
|
+
if (replacements.length) {
|
|
25
21
|
// This takes a single word and returns the first matching replacement, if any.
|
|
26
|
-
const replaceCustomWords = (word: string)
|
|
22
|
+
const replaceCustomWords = (word: string): string => {
|
|
27
23
|
const lowerWord = word.toLowerCase();
|
|
28
24
|
|
|
29
|
-
for (const [
|
|
30
|
-
if (key === lowerWord) {
|
|
25
|
+
for (const [key, value] of replacements) {
|
|
26
|
+
if (key === lowerWord && typeof value === 'string') {
|
|
31
27
|
return value;
|
|
32
28
|
}
|
|
33
29
|
}
|
package/src/type-assertion.ts
CHANGED
|
@@ -1,11 +1,20 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
import { isNumericValueArray } from './type-predicate.js';
|
|
2
|
+
import type { NumericValue } from './types';
|
|
3
|
+
|
|
4
|
+
export function assertIsInteger(input: unknown): asserts input is number {
|
|
5
|
+
if (!Number.isInteger(input)) {
|
|
3
6
|
throw new TypeError('input is not an integer');
|
|
4
7
|
}
|
|
5
8
|
}
|
|
6
9
|
|
|
7
|
-
export function
|
|
8
|
-
if (!
|
|
10
|
+
export function assertIsFiniteNumber(input: unknown): asserts input is number {
|
|
11
|
+
if (!Number.isFinite(input)) {
|
|
9
12
|
throw new TypeError('input is not a finite number');
|
|
10
13
|
}
|
|
11
14
|
}
|
|
15
|
+
|
|
16
|
+
export function assertIsNumericValueArray(input: unknown): asserts input is NumericValue[] {
|
|
17
|
+
if (!isNumericValueArray(input)) {
|
|
18
|
+
throw new TypeError('all items must be a NumericValue');
|
|
19
|
+
}
|
|
20
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import type { NumericString, NumericValue } from './types';
|
|
2
|
+
|
|
3
|
+
export const isOptionalString = (input: unknown): input is string | undefined =>
|
|
4
|
+
typeof input === 'string' || typeof input === 'undefined';
|
|
5
|
+
|
|
6
|
+
export const isOptionalNumber = (input: unknown): input is number | undefined =>
|
|
7
|
+
(typeof input === 'number' && !Number.isNaN(input)) || typeof input === 'undefined';
|
|
8
|
+
|
|
9
|
+
export const isStringMatching = (input: unknown, pattern: RegExp): input is string =>
|
|
10
|
+
typeof input === 'string' && pattern.test(input);
|
|
11
|
+
|
|
12
|
+
export const isPropertyKey = (input: unknown): input is PropertyKey =>
|
|
13
|
+
typeof input === 'string' || typeof input === 'number' || typeof input === 'symbol';
|
|
14
|
+
|
|
15
|
+
export const isNumericString = (input: unknown): input is NumericString =>
|
|
16
|
+
isStringMatching(input, /^[-+]?\d+(\.\d+)?$/);
|
|
17
|
+
|
|
18
|
+
export const isNumericValue = (input: unknown): input is NumericValue =>
|
|
19
|
+
typeof input === 'number' || typeof input === 'bigint' || isNumericString(input);
|
|
20
|
+
|
|
21
|
+
export const isNumericValueArray = (input: unknown): input is NumericValue[] =>
|
|
22
|
+
Array.isArray(input) && input.every(isNumericValue);
|
|
23
|
+
|
|
24
|
+
export const isIntString = (input: unknown): input is NumericString => isStringMatching(input, /^[-+]?\d+$/);
|
|
25
|
+
|
|
26
|
+
export const isFiniteNumber = (input: unknown): input is number => typeof input === 'number' && Number.isFinite(input);
|
|
27
|
+
|
|
28
|
+
export const isInteger = (input: unknown): input is number => Number.isInteger(input);
|
|
29
|
+
|
|
30
|
+
export const isPositiveInteger = (input: unknown): input is number => isInteger(input) && input >= 0;
|
|
31
|
+
|
|
32
|
+
export const isPositiveFiniteNumber = (input: unknown): input is number => isFiniteNumber(input) && input >= 0;
|
|
33
|
+
|
|
34
|
+
export const isLengthAware = (input: unknown): input is { length: number } =>
|
|
35
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
36
|
+
typeof input === 'object' && input !== null && 'length' in input && typeof (input as any).length === 'number';
|
package/src/types.ts
CHANGED
|
@@ -10,3 +10,10 @@ export type NumericValue = number | bigint | NumericString;
|
|
|
10
10
|
export type MaybePlural<T extends string> = T | `${T}s`;
|
|
11
11
|
|
|
12
12
|
export type StringKeys<T> = keyof T & string;
|
|
13
|
+
|
|
14
|
+
export type IfDefined<T, D, U> = undefined extends T ? U : D;
|
|
15
|
+
|
|
16
|
+
export type IfNever<T, N, O> = [T] extends [never] ? N : O;
|
|
17
|
+
|
|
18
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
19
|
+
export type ReturnTypeDefault<F, D = undefined> = F extends (...args: any) => infer R ? R : D;
|
package/src/uniqueItems.ts
CHANGED
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
|
}
|