@oscarpalmer/atoms 0.184.0 → 0.184.2
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/array/single.mjs +2 -2
- package/dist/array/sort.mjs +5 -5
- package/dist/color/instance.mjs +2 -2
- package/dist/color/misc/get.mjs +8 -8
- package/dist/color/misc/state.d.mts +2 -2
- package/dist/color/misc/state.mjs +2 -2
- package/dist/function/once.mjs +9 -9
- package/dist/function/retry.mjs +8 -8
- package/dist/index.d.mts +5 -6
- package/dist/index.mjs +49 -53
- package/dist/internal/number.d.mts +2 -1
- package/dist/internal/number.mjs +4 -1
- package/dist/internal/result.d.mts +2 -2
- package/dist/internal/value/equal.mjs +5 -5
- package/dist/models.d.mts +1 -1
- package/dist/promise/helpers.mjs +3 -5
- package/dist/queue.mjs +5 -7
- package/dist/string/fuzzy.mjs +2 -2
- package/dist/value/freeze.d.mts +2 -2
- package/dist/value/merge.d.mts +0 -1
- package/dist/value/merge.mjs +0 -1
- package/package.json +2 -2
- package/src/array/single.ts +2 -2
- package/src/array/sort.ts +5 -5
- package/src/color/instance.ts +2 -2
- package/src/color/misc/get.ts +8 -8
- package/src/color/misc/state.ts +1 -1
- package/src/function/once.ts +9 -9
- package/src/function/retry.ts +8 -8
- package/src/internal/number.ts +6 -0
- package/src/internal/result.ts +3 -3
- package/src/internal/value/equal.ts +5 -5
- package/src/internal/value/get.ts +2 -1
- package/src/internal/value/set.ts +3 -1
- package/src/models.ts +1 -1
- package/src/promise/helpers.ts +3 -6
- package/src/queue.ts +10 -10
- package/src/string/fuzzy.ts +2 -2
- package/src/value/freeze.ts +2 -2
- package/src/value/merge.ts +0 -1
package/dist/internal/number.mjs
CHANGED
|
@@ -53,9 +53,12 @@ function getNumber(value) {
|
|
|
53
53
|
if (isBinary || EXPRESSION_OCTAL.test(trimmed)) return Number.parseInt(trimmed.slice(2), isBinary ? 2 : OCTAL_VALUE);
|
|
54
54
|
return Number(trimmed);
|
|
55
55
|
}
|
|
56
|
+
function getNumberOrDefault(value, defaultValue, minimum) {
|
|
57
|
+
return typeof value === "number" && !Number.isNaN(value) && value >= (minimum ?? 0) ? Math.floor(value) : defaultValue;
|
|
58
|
+
}
|
|
56
59
|
const EXPRESSION_BINARY = /^0b[01]+$/i;
|
|
57
60
|
const EXPRESSION_OCTAL = /^0o[0-7]+$/i;
|
|
58
61
|
const EXPRESSION_ZEROISH = /^\s*0+\s*$/;
|
|
59
62
|
const OCTAL_VALUE = 8;
|
|
60
63
|
//#endregion
|
|
61
|
-
export { between, clamp, getNumber };
|
|
64
|
+
export { between, clamp, getNumber, getNumberOrDefault };
|
|
@@ -6,13 +6,13 @@ import { Err, ExtendedErr, Ok, Result } from "../result/models.mjs";
|
|
|
6
6
|
* @param result Result to check
|
|
7
7
|
* @returns `true` if the result is an extended error, `false` otherwise
|
|
8
8
|
*/
|
|
9
|
-
declare function isError<Value, E = Error>(
|
|
9
|
+
declare function isError<Value, E = Error>(result: ExtendedErr<E> | Result<Value, E>, extended: true): result is ExtendedErr<E>;
|
|
10
10
|
/**
|
|
11
11
|
* Is the result an error?
|
|
12
12
|
* @param result Result to check
|
|
13
13
|
* @returns `true` if the result is an error, `false` otherwise
|
|
14
14
|
*/
|
|
15
|
-
declare function isError<Value, E = Error>(
|
|
15
|
+
declare function isError<Value, E = Error>(result: Result<Value, E>): result is Err<E>;
|
|
16
16
|
/**
|
|
17
17
|
* Is the value an error?
|
|
18
18
|
* @param value Value to check
|
|
@@ -27,9 +27,9 @@ function equalArray(first, second, options) {
|
|
|
27
27
|
const { length } = first;
|
|
28
28
|
if (length !== second.length) return false;
|
|
29
29
|
let offset = 0;
|
|
30
|
-
if (length >=
|
|
31
|
-
offset = Math.round(length /
|
|
32
|
-
offset = offset >
|
|
30
|
+
if (length >= EQUAL_ARRAY_THRESHOLD) {
|
|
31
|
+
offset = Math.round(length / EQUAL_ARRAY_PEEK_PERCENTAGE);
|
|
32
|
+
offset = offset > EQUAL_ARRAY_THRESHOLD ? EQUAL_ARRAY_THRESHOLD : offset;
|
|
33
33
|
for (let index = 0; index < offset; index += 1) if (!(equalValue(first[index], second[index], options) && equalValue(first[length - index - 1], second[length - index - 1], options))) return false;
|
|
34
34
|
}
|
|
35
35
|
const end = length - offset;
|
|
@@ -164,8 +164,8 @@ function initializeEqualizer(options) {
|
|
|
164
164
|
function registerEqualizer(constructor, handler) {
|
|
165
165
|
equal.handlers.register(constructor, handler);
|
|
166
166
|
}
|
|
167
|
-
const
|
|
168
|
-
const
|
|
167
|
+
const EQUAL_ARRAY_PEEK_PERCENTAGE = 10;
|
|
168
|
+
const EQUAL_ARRAY_THRESHOLD = 100;
|
|
169
169
|
const ERROR_PROPERTIES = ["name", "message"];
|
|
170
170
|
const EXPRESSION_PROPERTIES = ["source", "flags"];
|
|
171
171
|
const MINIMUM_LENGTH_FOR_SET = 16;
|
package/dist/models.d.mts
CHANGED
|
@@ -13,7 +13,7 @@ type AsyncCancelableCallback<Callback extends GenericAsyncCallback | GenericCall
|
|
|
13
13
|
cancel: () => void;
|
|
14
14
|
};
|
|
15
15
|
/**
|
|
16
|
-
* For
|
|
16
|
+
* For matching any `void`, `Date`, primitive, or `RegExp` values
|
|
17
17
|
*
|
|
18
18
|
* (Thanks, type-fest!)
|
|
19
19
|
*/
|
package/dist/promise/helpers.mjs
CHANGED
|
@@ -1,11 +1,9 @@
|
|
|
1
|
+
import { getNumberOrDefault } from "../internal/number.mjs";
|
|
1
2
|
import { error, ok } from "../result/misc.mjs";
|
|
2
3
|
import { PROMISE_STRATEGY_ALL, PROMISE_STRATEGY_DEFAULT, PROMISE_TYPE_FULFILLED, PROMISE_TYPE_REJECTED } from "./models.mjs";
|
|
3
4
|
//#region src/promise/helpers.ts
|
|
4
|
-
function getNumberOrDefault(value) {
|
|
5
|
-
return typeof value === "number" && value > 0 ? value : 0;
|
|
6
|
-
}
|
|
7
5
|
function getPromiseOptions(input) {
|
|
8
|
-
if (typeof input === "number") return { time: getNumberOrDefault(input) };
|
|
6
|
+
if (typeof input === "number") return { time: getNumberOrDefault(input, 0) };
|
|
9
7
|
if (input instanceof AbortSignal) return {
|
|
10
8
|
signal: input,
|
|
11
9
|
time: 0
|
|
@@ -13,7 +11,7 @@ function getPromiseOptions(input) {
|
|
|
13
11
|
const options = typeof input === "object" && input !== null ? input : {};
|
|
14
12
|
return {
|
|
15
13
|
signal: options.signal instanceof AbortSignal ? options.signal : void 0,
|
|
16
|
-
time: getNumberOrDefault(options.time)
|
|
14
|
+
time: getNumberOrDefault(options.time, 0)
|
|
17
15
|
};
|
|
18
16
|
}
|
|
19
17
|
function getPromisesOptions(input) {
|
package/dist/queue.mjs
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { getNumberOrDefault } from "./internal/number.mjs";
|
|
1
2
|
//#region src/queue.ts
|
|
2
3
|
var KeyedQueue = class {
|
|
3
4
|
#callback;
|
|
@@ -315,11 +316,11 @@ var Queue = class {
|
|
|
315
316
|
if (this.#paused) {
|
|
316
317
|
const paused = item;
|
|
317
318
|
this.#handled.push(() => {
|
|
318
|
-
|
|
319
|
+
handleQueuedResult(paused, error, result, this.#items.length === 0);
|
|
319
320
|
});
|
|
320
321
|
break;
|
|
321
322
|
}
|
|
322
|
-
|
|
323
|
+
handleQueuedResult(item, error, result, this.#items.length === 0);
|
|
323
324
|
item = this.#items.shift();
|
|
324
325
|
}
|
|
325
326
|
this.#runners -= 1;
|
|
@@ -334,18 +335,15 @@ var QueueError = class extends Error {
|
|
|
334
335
|
function getBooleanOrDefault(value, defaultValue) {
|
|
335
336
|
return typeof value === "boolean" ? value : defaultValue;
|
|
336
337
|
}
|
|
337
|
-
function getNumberOrDefault(value, defaultValue) {
|
|
338
|
-
return typeof value === "number" && value > 0 ? Math.floor(value) : defaultValue;
|
|
339
|
-
}
|
|
340
338
|
function getOptions(input) {
|
|
341
339
|
const options = typeof input === "object" && input != null ? input : {};
|
|
342
340
|
return {
|
|
343
341
|
autostart: getBooleanOrDefault(options.autostart, true),
|
|
344
|
-
concurrency: getNumberOrDefault(options.concurrency, 1),
|
|
342
|
+
concurrency: getNumberOrDefault(options.concurrency, 1, 1),
|
|
345
343
|
maximum: getNumberOrDefault(options.maximum, 0)
|
|
346
344
|
};
|
|
347
345
|
}
|
|
348
|
-
function
|
|
346
|
+
function handleQueuedResult(item, error, result, finished) {
|
|
349
347
|
item.signal?.removeEventListener(EVENT_NAME, item.abort);
|
|
350
348
|
if (item.signal?.aborted ?? false) item.reject();
|
|
351
349
|
else if (error) item.reject(result);
|
package/dist/string/fuzzy.mjs
CHANGED
|
@@ -57,7 +57,7 @@ function getFuzzyOptions(input, state) {
|
|
|
57
57
|
options.tolerance = getTolerance(options.tolerance, state);
|
|
58
58
|
return options;
|
|
59
59
|
}
|
|
60
|
-
function
|
|
60
|
+
function getFuzzyState(items, input) {
|
|
61
61
|
const handler = getHandler(input);
|
|
62
62
|
const options = getFuzzyOptions(input);
|
|
63
63
|
return {
|
|
@@ -74,7 +74,7 @@ function getTolerance(input, state) {
|
|
|
74
74
|
}
|
|
75
75
|
function fuzzy(items, configuration) {
|
|
76
76
|
if (!Array.isArray(items)) throw new TypeError(MESSAGE_ARRAY);
|
|
77
|
-
return new Fuzzy(
|
|
77
|
+
return new Fuzzy(getFuzzyState(items, configuration));
|
|
78
78
|
}
|
|
79
79
|
fuzzy.match = fuzzyMatch;
|
|
80
80
|
/**
|
package/dist/value/freeze.d.mts
CHANGED
|
@@ -12,8 +12,8 @@ type Frozen<Value extends ArrayOrPlainObject> = { readonly [Key in keyof Value]:
|
|
|
12
12
|
*/
|
|
13
13
|
declare function freeze<Item>(array: Item[]): Frozen<Item[]>;
|
|
14
14
|
/**
|
|
15
|
-
* Freeze a
|
|
16
|
-
* @param
|
|
15
|
+
* Freeze a function
|
|
16
|
+
* @param fn Function to freeze
|
|
17
17
|
* @returns Frozen function
|
|
18
18
|
*/
|
|
19
19
|
declare function freeze<Fn extends GenericCallback>(fn: Fn): Readonly<Fn>;
|
package/dist/value/merge.d.mts
CHANGED
|
@@ -93,7 +93,6 @@ declare function merge<Model extends ArrayOrPlainObject>(values: NestedPartial<M
|
|
|
93
93
|
*/
|
|
94
94
|
declare function merge(values: NestedPartial<ArrayOrPlainObject>[], options?: MergeOptions): ArrayOrPlainObject;
|
|
95
95
|
declare namespace merge {
|
|
96
|
-
var assign: typeof assign;
|
|
97
96
|
var initialize: typeof initializeMerger;
|
|
98
97
|
}
|
|
99
98
|
//#endregion
|
package/dist/value/merge.mjs
CHANGED
|
@@ -61,7 +61,6 @@ function initializeMerger(options) {
|
|
|
61
61
|
function merge(values, options) {
|
|
62
62
|
return handleMerge(values, getMergeOptions(options));
|
|
63
63
|
}
|
|
64
|
-
merge.assign = assign;
|
|
65
64
|
merge.initialize = initializeMerger;
|
|
66
65
|
function mergeObjects(values, options, prefix) {
|
|
67
66
|
const { length } = values;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@oscarpalmer/atoms",
|
|
3
|
-
"version": "0.184.
|
|
3
|
+
"version": "0.184.2",
|
|
4
4
|
"description": "Atomic utilities for making your JavaScript better.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"helper",
|
|
@@ -257,7 +257,7 @@
|
|
|
257
257
|
"@types/node": "^25.6",
|
|
258
258
|
"@vitest/coverage-istanbul": "^4.1",
|
|
259
259
|
"eslint": "^10.2",
|
|
260
|
-
"jsdom": "^29.
|
|
260
|
+
"jsdom": "^29.1",
|
|
261
261
|
"tsdown": "^0.21",
|
|
262
262
|
"typescript": "^5.9",
|
|
263
263
|
"vite": "npm:@voidzero-dev/vite-plus-core@latest",
|
package/src/array/single.ts
CHANGED
|
@@ -49,7 +49,7 @@ export function single(array: unknown[], ...parameters: unknown[]): unknown {
|
|
|
49
49
|
const {matched} = findValues(FIND_VALUES_ALL, array, parameters);
|
|
50
50
|
|
|
51
51
|
if (matched.length > 1) {
|
|
52
|
-
throw new Error(
|
|
52
|
+
throw new Error(SINGLE_MESSAGE);
|
|
53
53
|
}
|
|
54
54
|
|
|
55
55
|
return matched[0];
|
|
@@ -59,6 +59,6 @@ export function single(array: unknown[], ...parameters: unknown[]): unknown {
|
|
|
59
59
|
|
|
60
60
|
// #region Variables
|
|
61
61
|
|
|
62
|
-
const
|
|
62
|
+
const SINGLE_MESSAGE = 'Multiple items were found';
|
|
63
63
|
|
|
64
64
|
// #endregion
|
package/src/array/sort.ts
CHANGED
|
@@ -458,9 +458,9 @@ function isSortedArray(array: unknown[], sorters: InternalSorter[]): boolean {
|
|
|
458
458
|
|
|
459
459
|
let offset = 0;
|
|
460
460
|
|
|
461
|
-
if (length >=
|
|
462
|
-
offset = Math.round(length /
|
|
463
|
-
offset = offset >
|
|
461
|
+
if (length >= SORT_THRESHOLD) {
|
|
462
|
+
offset = Math.round(length / SORT_PEEK_PERCENTAGE);
|
|
463
|
+
offset = offset > SORT_THRESHOLD ? SORT_THRESHOLD : offset;
|
|
464
464
|
|
|
465
465
|
for (let index = 0; index < offset; index += 1) {
|
|
466
466
|
const [firstItem, firstOffset] = [array[index], array[index + 1]];
|
|
@@ -560,9 +560,9 @@ sort.is = isSorted;
|
|
|
560
560
|
|
|
561
561
|
// #region Variables
|
|
562
562
|
|
|
563
|
-
const
|
|
563
|
+
const SORT_PEEK_PERCENTAGE = 10;
|
|
564
564
|
|
|
565
|
-
const
|
|
565
|
+
const SORT_THRESHOLD = 100;
|
|
566
566
|
|
|
567
567
|
export const SORT_DIRECTION_ASCENDING: SortDirection = 'ascending';
|
|
568
568
|
|
package/src/color/instance.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import {SPACE_HSL, SPACE_RGB} from './constants';
|
|
2
2
|
import {formatColor} from './misc';
|
|
3
3
|
import {getAlpha} from './misc/alpha';
|
|
4
|
-
import {
|
|
4
|
+
import {getColorState, setHexColor, setHSLColor, setRGBColor} from './misc/state';
|
|
5
5
|
import type {ColorState, HSLAColor, HSLColor, RGBAColor, RGBColor} from './models';
|
|
6
6
|
|
|
7
7
|
// #region Classes
|
|
@@ -118,7 +118,7 @@ export class Color {
|
|
|
118
118
|
}
|
|
119
119
|
|
|
120
120
|
constructor(value: unknown) {
|
|
121
|
-
this.#state =
|
|
121
|
+
this.#state = getColorState(value);
|
|
122
122
|
|
|
123
123
|
Object.defineProperty(this, '$color', {
|
|
124
124
|
value: true,
|
package/src/color/misc/get.ts
CHANGED
|
@@ -17,7 +17,7 @@ import {
|
|
|
17
17
|
} from '../constants';
|
|
18
18
|
import {Color} from '../instance';
|
|
19
19
|
import type {HSLAColor, HSLColor, RGBAColor, RGBColor} from '../models';
|
|
20
|
-
import {
|
|
20
|
+
import {getColorState} from './state';
|
|
21
21
|
|
|
22
22
|
// #region Functions
|
|
23
23
|
|
|
@@ -31,7 +31,7 @@ function getClampedValue(value: unknown, minimum: number, maximum: number): numb
|
|
|
31
31
|
* @returns Foreground color
|
|
32
32
|
*/
|
|
33
33
|
export function getForegroundColor(value: unknown): Color {
|
|
34
|
-
const state =
|
|
34
|
+
const state = getColorState(value);
|
|
35
35
|
const {blue, green, red} = state.rgb;
|
|
36
36
|
|
|
37
37
|
const values = [blue / MAX_HEX, green / MAX_HEX, red / MAX_HEX];
|
|
@@ -63,7 +63,7 @@ export function getForegroundColor(value: unknown): Color {
|
|
|
63
63
|
* @returns Hex color
|
|
64
64
|
*/
|
|
65
65
|
export function getHexaColor(value: unknown): string {
|
|
66
|
-
const {alpha, hex} =
|
|
66
|
+
const {alpha, hex} = getColorState(value);
|
|
67
67
|
|
|
68
68
|
return `${hex}${alpha.hex}`;
|
|
69
69
|
}
|
|
@@ -74,7 +74,7 @@ export function getHexaColor(value: unknown): string {
|
|
|
74
74
|
* @returns Hex color
|
|
75
75
|
*/
|
|
76
76
|
export function getHexColor(value: unknown): string {
|
|
77
|
-
return
|
|
77
|
+
return getColorState(value).hex;
|
|
78
78
|
}
|
|
79
79
|
|
|
80
80
|
export function getHexValue(value: unknown): number {
|
|
@@ -91,7 +91,7 @@ export function getDegrees(value: unknown): number {
|
|
|
91
91
|
* @returns HSLA color
|
|
92
92
|
*/
|
|
93
93
|
export function getHslaColor(value: unknown): HSLAColor {
|
|
94
|
-
const {alpha, hsl} =
|
|
94
|
+
const {alpha, hsl} = getColorState(value);
|
|
95
95
|
|
|
96
96
|
return {
|
|
97
97
|
...hsl,
|
|
@@ -105,7 +105,7 @@ export function getHslaColor(value: unknown): HSLAColor {
|
|
|
105
105
|
* @returns HSL color
|
|
106
106
|
*/
|
|
107
107
|
export function getHslColor(value: unknown): HSLColor {
|
|
108
|
-
return
|
|
108
|
+
return getColorState(value).hsl;
|
|
109
109
|
}
|
|
110
110
|
|
|
111
111
|
export function getPercentage(value: unknown): number {
|
|
@@ -118,7 +118,7 @@ export function getPercentage(value: unknown): number {
|
|
|
118
118
|
* @returns RGBA color
|
|
119
119
|
*/
|
|
120
120
|
export function getRgbaColor(value: unknown): RGBAColor {
|
|
121
|
-
const {alpha, rgb} =
|
|
121
|
+
const {alpha, rgb} = getColorState(value);
|
|
122
122
|
|
|
123
123
|
return {
|
|
124
124
|
...rgb,
|
|
@@ -132,7 +132,7 @@ export function getRgbaColor(value: unknown): RGBAColor {
|
|
|
132
132
|
* @returns RGB color
|
|
133
133
|
*/
|
|
134
134
|
export function getRgbColor(value: unknown): RGBColor {
|
|
135
|
-
return
|
|
135
|
+
return getColorState(value).rgb;
|
|
136
136
|
}
|
|
137
137
|
|
|
138
138
|
// #endregion
|
package/src/color/misc/state.ts
CHANGED
|
@@ -17,7 +17,7 @@ import {isColor, isHexColor, isHslLike, isRgbLike} from './is';
|
|
|
17
17
|
|
|
18
18
|
// #region Functions
|
|
19
19
|
|
|
20
|
-
export function
|
|
20
|
+
export function getColorState(value: unknown): ColorState {
|
|
21
21
|
if (typeof value === 'string') {
|
|
22
22
|
const normalized = getNormalizedHex(value, true);
|
|
23
23
|
const hex = normalized.slice(0, LENGTH_LONG);
|
package/src/function/once.ts
CHANGED
|
@@ -39,7 +39,7 @@ type OnceState<Value> = {
|
|
|
39
39
|
export function asyncOnce<Callback extends GenericAsyncCallback>(
|
|
40
40
|
callback: Callback,
|
|
41
41
|
): OnceAsyncCallback<Callback> {
|
|
42
|
-
assert(() => typeof callback === 'function',
|
|
42
|
+
assert(() => typeof callback === 'function', ONCE_MESSAGE_EXPECTATION);
|
|
43
43
|
|
|
44
44
|
const state: OnceAsyncState<Awaited<ReturnType<Callback>>> = {
|
|
45
45
|
called: false,
|
|
@@ -52,7 +52,7 @@ export function asyncOnce<Callback extends GenericAsyncCallback>(
|
|
|
52
52
|
|
|
53
53
|
const fn = (...parameters: Parameters<Callback>): Promise<Awaited<ReturnType<Callback>>> => {
|
|
54
54
|
if (state.cleared) {
|
|
55
|
-
return Promise.reject(new Error(
|
|
55
|
+
return Promise.reject(new Error(ONCE_MESSAGE_CLEARED));
|
|
56
56
|
}
|
|
57
57
|
|
|
58
58
|
if (state.finished) {
|
|
@@ -72,10 +72,10 @@ export function asyncOnce<Callback extends GenericAsyncCallback>(
|
|
|
72
72
|
|
|
73
73
|
void callback(...parameters)
|
|
74
74
|
.then(value => {
|
|
75
|
-
|
|
75
|
+
handleOnceResult(state, value, false);
|
|
76
76
|
})
|
|
77
77
|
.catch(error => {
|
|
78
|
-
|
|
78
|
+
handleOnceResult(state, error, true);
|
|
79
79
|
});
|
|
80
80
|
});
|
|
81
81
|
};
|
|
@@ -107,7 +107,7 @@ export function asyncOnce<Callback extends GenericAsyncCallback>(
|
|
|
107
107
|
return fn as OnceAsyncCallback<Callback>;
|
|
108
108
|
}
|
|
109
109
|
|
|
110
|
-
function
|
|
110
|
+
function handleOnceResult<Value>(state: OnceAsyncState<Value>, value: unknown, error: boolean): void {
|
|
111
111
|
state.error = error;
|
|
112
112
|
state.finished = true;
|
|
113
113
|
state.value = value as Value;
|
|
@@ -132,7 +132,7 @@ function handleResult<Value>(state: OnceAsyncState<Value>, value: unknown, error
|
|
|
132
132
|
* @returns Once callback
|
|
133
133
|
*/
|
|
134
134
|
export function once<Callback extends GenericCallback>(callback: Callback): OnceCallback<Callback> {
|
|
135
|
-
assert(() => typeof callback === 'function',
|
|
135
|
+
assert(() => typeof callback === 'function', ONCE_MESSAGE_EXPECTATION);
|
|
136
136
|
|
|
137
137
|
const state: OnceState<ReturnType<Callback>> = {
|
|
138
138
|
called: false,
|
|
@@ -142,7 +142,7 @@ export function once<Callback extends GenericCallback>(callback: Callback): Once
|
|
|
142
142
|
|
|
143
143
|
const fn = (...parameters: Parameters<Callback>): ReturnType<Callback> => {
|
|
144
144
|
if (state.cleared) {
|
|
145
|
-
throw new Error(
|
|
145
|
+
throw new Error(ONCE_MESSAGE_CLEARED);
|
|
146
146
|
}
|
|
147
147
|
|
|
148
148
|
if (state.called) {
|
|
@@ -183,8 +183,8 @@ once.async = asyncOnce;
|
|
|
183
183
|
|
|
184
184
|
// #region Variables
|
|
185
185
|
|
|
186
|
-
const
|
|
186
|
+
const ONCE_MESSAGE_CLEARED = 'Once has been cleared';
|
|
187
187
|
|
|
188
|
-
const
|
|
188
|
+
const ONCE_MESSAGE_EXPECTATION = 'Once expected a function';
|
|
189
189
|
|
|
190
190
|
// #endregion
|
package/src/function/retry.ts
CHANGED
|
@@ -11,7 +11,7 @@ export class RetryError extends Error {
|
|
|
11
11
|
) {
|
|
12
12
|
super(message);
|
|
13
13
|
|
|
14
|
-
this.name =
|
|
14
|
+
this.name = RETRY_ERROR_NAME;
|
|
15
15
|
}
|
|
16
16
|
}
|
|
17
17
|
|
|
@@ -64,7 +64,7 @@ async function asyncRetry<Callback extends GenericCallback>(
|
|
|
64
64
|
options?: RetryOptions,
|
|
65
65
|
): Promise<ReturnType<Callback>> {
|
|
66
66
|
if (typeof callback !== 'function') {
|
|
67
|
-
throw new TypeError(
|
|
67
|
+
throw new TypeError(RETRY_MESSAGE_EXPECTATION);
|
|
68
68
|
}
|
|
69
69
|
|
|
70
70
|
async function handle(): Promise<void> {
|
|
@@ -74,7 +74,7 @@ async function asyncRetry<Callback extends GenericCallback>(
|
|
|
74
74
|
resolver(result);
|
|
75
75
|
} catch (error) {
|
|
76
76
|
if (attempts >= times || !when(error)) {
|
|
77
|
-
rejector(new RetryError(
|
|
77
|
+
rejector(new RetryError(RETRY_MESSAGE_FAILED, error));
|
|
78
78
|
} else {
|
|
79
79
|
attempts += 1;
|
|
80
80
|
|
|
@@ -125,7 +125,7 @@ export function retry<Callback extends GenericCallback>(
|
|
|
125
125
|
options?: Omit<RetryOptions, 'delay'>,
|
|
126
126
|
): ReturnType<Callback> {
|
|
127
127
|
if (typeof callback !== 'function') {
|
|
128
|
-
throw new TypeError(
|
|
128
|
+
throw new TypeError(RETRY_MESSAGE_EXPECTATION);
|
|
129
129
|
}
|
|
130
130
|
|
|
131
131
|
const {times, when} = getRetryOptions(options);
|
|
@@ -146,7 +146,7 @@ export function retry<Callback extends GenericCallback>(
|
|
|
146
146
|
}
|
|
147
147
|
}
|
|
148
148
|
|
|
149
|
-
throw new RetryError(
|
|
149
|
+
throw new RetryError(RETRY_MESSAGE_FAILED, last);
|
|
150
150
|
}
|
|
151
151
|
|
|
152
152
|
retry.async = asyncRetry;
|
|
@@ -159,10 +159,10 @@ function shouldRetry(): boolean {
|
|
|
159
159
|
|
|
160
160
|
// #region Variables
|
|
161
161
|
|
|
162
|
-
const
|
|
162
|
+
const RETRY_ERROR_NAME = 'RetryError';
|
|
163
163
|
|
|
164
|
-
const
|
|
164
|
+
const RETRY_MESSAGE_EXPECTATION = 'Retry expected a function';
|
|
165
165
|
|
|
166
|
-
const
|
|
166
|
+
const RETRY_MESSAGE_FAILED = 'Retry failed';
|
|
167
167
|
|
|
168
168
|
// #endregion
|
package/src/internal/number.ts
CHANGED
|
@@ -97,6 +97,12 @@ export function getNumber(value: unknown): number {
|
|
|
97
97
|
return Number(trimmed);
|
|
98
98
|
}
|
|
99
99
|
|
|
100
|
+
export function getNumberOrDefault(value: unknown, defaultValue: number, minimum?: number): number {
|
|
101
|
+
return typeof value === 'number' && !Number.isNaN(value) && value >= (minimum ?? 0)
|
|
102
|
+
? Math.floor(value)
|
|
103
|
+
: defaultValue;
|
|
104
|
+
}
|
|
105
|
+
|
|
100
106
|
// #endregion
|
|
101
107
|
|
|
102
108
|
// #region Variables
|
package/src/internal/result.ts
CHANGED
|
@@ -20,16 +20,16 @@ function _isResult(value: unknown, okValue: boolean): value is Result<unknown, u
|
|
|
20
20
|
* @returns `true` if the result is an extended error, `false` otherwise
|
|
21
21
|
*/
|
|
22
22
|
export function isError<Value, E = Error>(
|
|
23
|
-
|
|
23
|
+
result: ExtendedErr<E> | Result<Value, E>,
|
|
24
24
|
extended: true,
|
|
25
|
-
):
|
|
25
|
+
): result is ExtendedErr<E>;
|
|
26
26
|
|
|
27
27
|
/**
|
|
28
28
|
* Is the result an error?
|
|
29
29
|
* @param result Result to check
|
|
30
30
|
* @returns `true` if the result is an error, `false` otherwise
|
|
31
31
|
*/
|
|
32
|
-
export function isError<Value, E = Error>(
|
|
32
|
+
export function isError<Value, E = Error>(result: Result<Value, E>): result is Err<E>;
|
|
33
33
|
|
|
34
34
|
/**
|
|
35
35
|
* Is the value an error?
|
|
@@ -141,9 +141,9 @@ function equalArray(first: unknown[], second: unknown[], options: Options): bool
|
|
|
141
141
|
|
|
142
142
|
let offset = 0;
|
|
143
143
|
|
|
144
|
-
if (length >=
|
|
145
|
-
offset = Math.round(length /
|
|
146
|
-
offset = offset >
|
|
144
|
+
if (length >= EQUAL_ARRAY_THRESHOLD) {
|
|
145
|
+
offset = Math.round(length / EQUAL_ARRAY_PEEK_PERCENTAGE);
|
|
146
|
+
offset = offset > EQUAL_ARRAY_THRESHOLD ? EQUAL_ARRAY_THRESHOLD : offset;
|
|
147
147
|
|
|
148
148
|
for (let index = 0; index < offset; index += 1) {
|
|
149
149
|
if (
|
|
@@ -438,9 +438,9 @@ export function registerEqualizer<Instance>(
|
|
|
438
438
|
|
|
439
439
|
// #region Variables
|
|
440
440
|
|
|
441
|
-
const
|
|
441
|
+
const EQUAL_ARRAY_PEEK_PERCENTAGE = 10;
|
|
442
442
|
|
|
443
|
-
const
|
|
443
|
+
const EQUAL_ARRAY_THRESHOLD = 100;
|
|
444
444
|
|
|
445
445
|
const ERROR_PROPERTIES: string[] = ['name', 'message'];
|
|
446
446
|
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type {NestedKeys, NestedValue, PlainObject, ToString} from '../../models';
|
|
2
|
+
import type {Ok} from '../../result/models';
|
|
2
3
|
import {getNestedValue} from './misc';
|
|
3
4
|
|
|
4
5
|
// #region Functions
|
|
@@ -28,7 +29,7 @@ export function getValue<Data extends PlainObject>(
|
|
|
28
29
|
): unknown;
|
|
29
30
|
|
|
30
31
|
export function getValue(data: PlainObject, path: string, ignoreCase?: boolean): unknown {
|
|
31
|
-
return getNestedValue(data, path, ignoreCase === true).value;
|
|
32
|
+
return (getNestedValue(data, path, ignoreCase === true) as Ok<unknown>).value;
|
|
32
33
|
}
|
|
33
34
|
|
|
34
35
|
// #endregion
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type {NestedKeys, NestedValue, PlainObject} from '../../models';
|
|
2
|
+
import type {Ok} from '../../result/models';
|
|
2
3
|
import {getPaths, handleValue} from './misc';
|
|
3
4
|
|
|
4
5
|
// #region Functions
|
|
@@ -77,7 +78,8 @@ export function setValue(data: object, path: string, value: unknown, ignoreCase?
|
|
|
77
78
|
break;
|
|
78
79
|
}
|
|
79
80
|
|
|
80
|
-
let next = handleValue(target, currentPath, null, true, shouldIgnoreCase)
|
|
81
|
+
let next = (handleValue(target, currentPath, null, true, shouldIgnoreCase) as Ok<unknown>)
|
|
82
|
+
.value;
|
|
81
83
|
|
|
82
84
|
if (typeof next !== 'object' || next === null) {
|
|
83
85
|
const nextPath = paths[index + 1];
|
package/src/models.ts
CHANGED
|
@@ -19,7 +19,7 @@ export type AsyncCancelableCallback<Callback extends GenericAsyncCallback | Gene
|
|
|
19
19
|
};
|
|
20
20
|
|
|
21
21
|
/**
|
|
22
|
-
* For
|
|
22
|
+
* For matching any `void`, `Date`, primitive, or `RegExp` values
|
|
23
23
|
*
|
|
24
24
|
* (Thanks, type-fest!)
|
|
25
25
|
*/
|
package/src/promise/helpers.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import {getNumberOrDefault} from '../internal/number';
|
|
1
2
|
import type {RequiredKeys} from '../models';
|
|
2
3
|
import {error, ok} from '../result/misc';
|
|
3
4
|
import type {Result} from '../result/models';
|
|
@@ -16,14 +17,10 @@ import {
|
|
|
16
17
|
|
|
17
18
|
// #region Functions
|
|
18
19
|
|
|
19
|
-
function getNumberOrDefault(value: unknown): number {
|
|
20
|
-
return typeof value === 'number' && value > 0 ? value : 0;
|
|
21
|
-
}
|
|
22
|
-
|
|
23
20
|
export function getPromiseOptions(input: unknown): RequiredKeys<PromiseOptions, 'time'> {
|
|
24
21
|
if (typeof input === 'number') {
|
|
25
22
|
return {
|
|
26
|
-
time: getNumberOrDefault(input),
|
|
23
|
+
time: getNumberOrDefault(input, 0),
|
|
27
24
|
};
|
|
28
25
|
}
|
|
29
26
|
|
|
@@ -35,7 +32,7 @@ export function getPromiseOptions(input: unknown): RequiredKeys<PromiseOptions,
|
|
|
35
32
|
|
|
36
33
|
return {
|
|
37
34
|
signal: options.signal instanceof AbortSignal ? options.signal : undefined,
|
|
38
|
-
time: getNumberOrDefault(options.time),
|
|
35
|
+
time: getNumberOrDefault(options.time, 0),
|
|
39
36
|
};
|
|
40
37
|
}
|
|
41
38
|
|
package/src/queue.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import {getNumberOrDefault} from './internal/number';
|
|
1
2
|
import type {GenericAsyncCallback, GenericCallback} from './models';
|
|
2
3
|
|
|
3
4
|
// #region Types
|
|
@@ -471,13 +472,13 @@ class Queue<CallbackParameters extends Parameters<GenericAsyncCallback>, Callbac
|
|
|
471
472
|
const paused = item;
|
|
472
473
|
|
|
473
474
|
this.#handled.push(() => {
|
|
474
|
-
|
|
475
|
+
handleQueuedResult(paused, error, result, this.#items.length === 0);
|
|
475
476
|
});
|
|
476
477
|
|
|
477
478
|
break;
|
|
478
479
|
}
|
|
479
480
|
|
|
480
|
-
|
|
481
|
+
handleQueuedResult(item, error, result, this.#items.length === 0);
|
|
481
482
|
|
|
482
483
|
item = this.#items.shift();
|
|
483
484
|
}
|
|
@@ -554,21 +555,20 @@ function getBooleanOrDefault(value: unknown, defaultValue: boolean): boolean {
|
|
|
554
555
|
return typeof value === 'boolean' ? value : defaultValue;
|
|
555
556
|
}
|
|
556
557
|
|
|
557
|
-
function getNumberOrDefault(value: unknown, defaultValue: number): number {
|
|
558
|
-
return typeof value === 'number' && value > 0 ? Math.floor(value) : defaultValue;
|
|
559
|
-
}
|
|
560
|
-
|
|
561
558
|
function getOptions(input?: QueueOptions): Required<QueueOptions> {
|
|
562
559
|
const options = typeof input === 'object' && input != null ? input : {};
|
|
563
560
|
|
|
564
561
|
return {
|
|
565
562
|
autostart: getBooleanOrDefault(options.autostart, true),
|
|
566
|
-
concurrency: getNumberOrDefault(options.concurrency, 1),
|
|
563
|
+
concurrency: getNumberOrDefault(options.concurrency, 1, 1),
|
|
567
564
|
maximum: getNumberOrDefault(options.maximum, 0),
|
|
568
565
|
};
|
|
569
566
|
}
|
|
570
567
|
|
|
571
|
-
function
|
|
568
|
+
function handleQueuedResult<
|
|
569
|
+
CallbackParameters extends Parameters<GenericAsyncCallback>,
|
|
570
|
+
CallbackResult,
|
|
571
|
+
>(
|
|
572
572
|
item: QueuedItem<CallbackParameters, CallbackResult>,
|
|
573
573
|
error: boolean,
|
|
574
574
|
result: unknown,
|
|
@@ -676,11 +676,11 @@ const STATUS_PAUSED: StatusKey = 'paused';
|
|
|
676
676
|
|
|
677
677
|
export {
|
|
678
678
|
type KeyedQueue,
|
|
679
|
-
type QueueError,
|
|
680
679
|
type Queue,
|
|
681
680
|
type Queued,
|
|
682
|
-
type QueueOptions,
|
|
683
681
|
type QueuedResult,
|
|
682
|
+
type QueueError,
|
|
683
|
+
type QueueOptions,
|
|
684
684
|
};
|
|
685
685
|
|
|
686
686
|
// #endregion
|