@oscarpalmer/atoms 0.204.1 → 0.206.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/array/filter.d.mts +1 -1
- package/dist/array/find.d.mts +3 -3
- package/dist/array/first.d.mts +3 -4
- package/dist/array/first.mjs +1 -1
- package/dist/array/group-by.d.mts +3 -3
- package/dist/array/index.d.mts +2 -5
- package/dist/array/index.mjs +2 -5
- package/dist/array/last.d.mts +3 -4
- package/dist/array/last.mjs +1 -1
- package/dist/array/move.d.mts +4 -4
- package/dist/array/push.d.mts +2 -1
- package/dist/array/push.mjs +1 -1
- package/dist/array/reverse.mjs +2 -1
- package/dist/array/select.d.mts +95 -84
- package/dist/array/select.mjs +6 -6
- package/dist/array/single.d.mts +3 -3
- package/dist/array/sort.d.mts +6 -6
- package/dist/array/sort.mjs +3 -2
- package/dist/array/swap.d.mts +3 -3
- package/dist/array/swap.mjs +1 -1
- package/dist/array/to-map.d.mts +3 -3
- package/dist/array/to-record.d.mts +3 -3
- package/dist/beacon.mjs +2 -2
- package/dist/color/constants.d.mts +14 -41
- package/dist/color/instance.d.mts +0 -1
- package/dist/color/misc/alpha.mjs +1 -1
- package/dist/color/misc/get.mjs +1 -1
- package/dist/color/misc/state.d.mts +2 -29
- package/dist/color/models.d.mts +18 -4
- package/dist/color/space/hsl.mjs +7 -2
- package/dist/color/space/hwb.mjs +2 -1
- package/dist/color/space/rgb.mjs +22 -13
- package/dist/function/limit.d.mts +4 -4
- package/dist/function/once.d.mts +6 -8
- package/dist/function/retry.d.mts +2 -2
- package/dist/function/retry.mjs +12 -14
- package/dist/index.d.mts +1104 -1127
- package/dist/index.mjs +385 -383
- package/dist/internal/array/compact.d.mts +1 -1
- package/dist/internal/array/find.d.mts +4 -4
- package/dist/internal/array/index-of.d.mts +3 -3
- package/dist/internal/array/insert.d.mts +1 -1
- package/dist/internal/array/insert.mjs +2 -1
- package/dist/internal/array/match.d.mts +1 -1
- package/dist/internal/array/sets.d.mts +1 -1
- package/dist/internal/function/assert.d.mts +7 -7
- package/dist/internal/function/limit.d.mts +1 -1
- package/dist/internal/function/limit.mjs +9 -9
- package/dist/internal/function/work.d.mts +4 -4
- package/dist/internal/herald.d.mts +1 -1
- package/dist/internal/math/aggregate.d.mts +43 -2
- package/dist/internal/math/aggregate.mjs +4 -1
- package/dist/internal/math/misc.d.mts +8 -0
- package/dist/internal/math/misc.mjs +11 -1
- package/dist/internal/random.mjs +2 -1
- package/dist/internal/result/attempt.d.mts +59 -0
- package/dist/internal/result/attempt.mjs +24 -0
- package/dist/internal/result/misc.d.mts +6 -6
- package/dist/internal/sized/map.d.mts +2 -2
- package/dist/internal/string/misc.mjs +4 -5
- package/dist/internal/value/compare.d.mts +7 -19
- package/dist/internal/value/equal.d.mts +8 -31
- package/dist/internal/value/equal.mjs +2 -1
- package/dist/internal/value/handlers.d.mts +2 -5
- package/dist/internal/value/has.d.mts +3 -3
- package/dist/internal/value/misc.mjs +1 -1
- package/dist/is.d.mts +4 -4
- package/dist/math.d.mts +3 -52
- package/dist/math.mjs +3 -16
- package/dist/promise/constants.d.mts +0 -1
- package/dist/promise/constants.mjs +1 -2
- package/dist/promise/helpers.d.mts +2 -2
- package/dist/promise/helpers.mjs +1 -1
- package/dist/promise/index.d.mts +7 -7
- package/dist/promise/index.mjs +9 -9
- package/dist/promise/misc.mjs +1 -1
- package/dist/promise/models.d.mts +1 -1
- package/dist/promise/timed.d.mts +1 -1
- package/dist/queue.d.mts +1 -1
- package/dist/queue.mjs +11 -6
- package/dist/result/index.d.mts +2 -59
- package/dist/result/index.mjs +1 -21
- package/dist/result/match.d.mts +3 -3
- package/dist/result/work/flow.d.mts +1 -1
- package/dist/result/work/flow.mjs +2 -1
- package/dist/result/work/pipe.d.mts +1 -1
- package/dist/result/work/pipe.mjs +2 -1
- package/dist/sized/set.d.mts +1 -1
- package/dist/string/fuzzy.d.mts +4 -4
- package/dist/string/fuzzy.mjs +7 -5
- package/dist/string/index.mjs +2 -1
- package/dist/string/normalize.d.mts +1 -1
- package/dist/string/template.d.mts +1 -2
- package/dist/value/clone.d.mts +8 -20
- package/dist/value/clone.mjs +5 -8
- package/dist/value/diff.d.mts +1 -1
- package/dist/value/diff.mjs +3 -2
- package/dist/value/freeze.d.mts +4 -4
- package/dist/value/merge.d.mts +5 -5
- package/dist/value/transform.d.mts +1 -1
- package/package.json +25 -10
- package/plugin/index.js +1 -1
|
@@ -1,17 +1,17 @@
|
|
|
1
1
|
import { SubscriptionProperty } from "../internal/subscription.mjs";
|
|
2
|
-
import { ColorTypeExtended, HSLAColor, HSLColor, HWBAColor, HWBColor, RGBAColor, RGBColor } from "./models.mjs";
|
|
2
|
+
import { ColorDefaults, ColorExpressions, ColorTypeExtended, HSLAColor, HSLColor, HWBAColor, HWBColor, RGBAColor, RGBColor } from "./models.mjs";
|
|
3
3
|
//#region src/color/constants.d.ts
|
|
4
4
|
type ColorTypes = {
|
|
5
|
-
all: Set<
|
|
6
|
-
hex:
|
|
7
|
-
hexa:
|
|
8
|
-
hsl:
|
|
9
|
-
hsla:
|
|
10
|
-
hwb:
|
|
11
|
-
hwba:
|
|
12
|
-
rgb:
|
|
13
|
-
rgba:
|
|
14
|
-
wildcard:
|
|
5
|
+
all: Set<"alpha" | ColorTypeExtended>;
|
|
6
|
+
hex: "hex";
|
|
7
|
+
hexa: "hexa";
|
|
8
|
+
hsl: "hsl";
|
|
9
|
+
hsla: "hsla";
|
|
10
|
+
hwb: "hwb";
|
|
11
|
+
hwba: "hwba";
|
|
12
|
+
rgb: "rgb";
|
|
13
|
+
rgba: "rgba";
|
|
14
|
+
wildcard: "*";
|
|
15
15
|
};
|
|
16
16
|
export declare const COLOR_ALPHA: {
|
|
17
17
|
fullHexShort: string;
|
|
@@ -20,35 +20,8 @@ export declare const COLOR_ALPHA: {
|
|
|
20
20
|
noneHex: string;
|
|
21
21
|
noneValue: number;
|
|
22
22
|
};
|
|
23
|
-
export declare const COLOR_DEFAULTS:
|
|
24
|
-
|
|
25
|
-
hex: string;
|
|
26
|
-
value: number;
|
|
27
|
-
};
|
|
28
|
-
hexBlack: string;
|
|
29
|
-
hexWhite: string;
|
|
30
|
-
hsl: {
|
|
31
|
-
hue: number;
|
|
32
|
-
saturation: number;
|
|
33
|
-
lightness: number;
|
|
34
|
-
};
|
|
35
|
-
hwb: {
|
|
36
|
-
hue: number;
|
|
37
|
-
whiteness: number;
|
|
38
|
-
blackness: number;
|
|
39
|
-
};
|
|
40
|
-
rgb: {
|
|
41
|
-
blue: number;
|
|
42
|
-
green: number;
|
|
43
|
-
red: number;
|
|
44
|
-
};
|
|
45
|
-
};
|
|
46
|
-
export declare const COLOR_EXPRESSION: {
|
|
47
|
-
alphaHex: RegExp;
|
|
48
|
-
hexLong: RegExp;
|
|
49
|
-
hexShort: RegExp;
|
|
50
|
-
prefix: RegExp;
|
|
51
|
-
};
|
|
23
|
+
export declare const COLOR_DEFAULTS: ColorDefaults;
|
|
24
|
+
export declare const COLOR_EXPRESSION: ColorExpressions;
|
|
52
25
|
export declare const COLOR_LENGTHS: {
|
|
53
26
|
hexLong: number;
|
|
54
27
|
hexShort: number;
|
|
@@ -68,7 +41,7 @@ export declare const COLOR_MAX: {
|
|
|
68
41
|
};
|
|
69
42
|
export declare const COLOR_NAME = "color";
|
|
70
43
|
export declare const COLOR_PROPERTY = "$color";
|
|
71
|
-
export declare const COLOR_SYMBOL:
|
|
44
|
+
export declare const COLOR_SYMBOL: symbol;
|
|
72
45
|
export declare const COLOR_TYPE: ColorTypes;
|
|
73
46
|
export declare const COLOR_SRGB: {
|
|
74
47
|
luminanceBlue: number;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
+
import { round } from "../../internal/math/misc.mjs";
|
|
1
2
|
import { clamp } from "../../internal/number.mjs";
|
|
2
3
|
import { COLOR_ALPHA, COLOR_DEFAULTS, COLOR_EXPRESSION, COLOR_MAX, COLOR_SYMBOL } from "../constants.mjs";
|
|
3
|
-
import { round } from "../../internal/math/misc.mjs";
|
|
4
4
|
//#region src/color/misc/alpha.ts
|
|
5
5
|
function getAlpha(value, hex) {
|
|
6
6
|
if (typeof value === "number") return getAlphaFromValue(value);
|
package/dist/color/misc/get.mjs
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
+
import { round } from "../../internal/math/misc.mjs";
|
|
1
2
|
import { clamp } from "../../internal/number.mjs";
|
|
2
3
|
import { COLOR_DEFAULTS, COLOR_MAX, COLOR_SRGB, COLOR_TYPE } from "../constants.mjs";
|
|
3
|
-
import { round } from "../../internal/math/misc.mjs";
|
|
4
4
|
import { getColorFromState, getColorState } from "./state.mjs";
|
|
5
5
|
import { color } from "../instance.mjs";
|
|
6
6
|
//#region src/color/misc/get.ts
|
|
@@ -1,34 +1,7 @@
|
|
|
1
|
-
import { ColorState, ColorType, InternalColor } from "../models.mjs";
|
|
1
|
+
import { ColorState, ColorType, HSLAColor, HSLColor, HWBAColor, HWBColor, InternalColor, RGBAColor, RGBColor } from "../models.mjs";
|
|
2
2
|
//#region src/color/misc/state.d.ts
|
|
3
3
|
export declare function getColorFromState<Type extends ColorType>(state: ColorState, type: Type): NonNullable<ColorState[Type]>;
|
|
4
4
|
export declare function getColorState(value: unknown): ColorState;
|
|
5
|
-
export declare function getStateValue(instance: InternalColor, type: ColorType, alpha: boolean): string |
|
|
6
|
-
alpha: number;
|
|
7
|
-
hue: number;
|
|
8
|
-
lightness: number;
|
|
9
|
-
saturation: number;
|
|
10
|
-
} | {
|
|
11
|
-
alpha: number;
|
|
12
|
-
blackness: number;
|
|
13
|
-
hue: number;
|
|
14
|
-
whiteness: number;
|
|
15
|
-
} | {
|
|
16
|
-
alpha: number;
|
|
17
|
-
blue: number;
|
|
18
|
-
green: number;
|
|
19
|
-
red: number;
|
|
20
|
-
} | {
|
|
21
|
-
hue: number;
|
|
22
|
-
lightness: number;
|
|
23
|
-
saturation: number;
|
|
24
|
-
} | {
|
|
25
|
-
blackness: number;
|
|
26
|
-
hue: number;
|
|
27
|
-
whiteness: number;
|
|
28
|
-
} | {
|
|
29
|
-
blue: number;
|
|
30
|
-
green: number;
|
|
31
|
-
red: number;
|
|
32
|
-
};
|
|
5
|
+
export declare function getStateValue(instance: InternalColor, type: ColorType, alpha: boolean): string | HSLAColor | HSLColor | HWBAColor | HWBColor | RGBAColor | RGBColor;
|
|
33
6
|
export declare function setStateValue<Type extends ColorType>(instance: InternalColor, type: Type, value: ColorState[Type], alpha?: number | string): void;
|
|
34
7
|
//#endregion
|
package/dist/color/models.d.mts
CHANGED
|
@@ -187,7 +187,7 @@ export type Color = {
|
|
|
187
187
|
unsubscribe(): void;
|
|
188
188
|
};
|
|
189
189
|
export type ColorChanges = {
|
|
190
|
-
|
|
190
|
+
"*": (value: Color) => void;
|
|
191
191
|
alpha: (value: number) => void;
|
|
192
192
|
hex: (value: string) => void;
|
|
193
193
|
hexa: (value: string) => void;
|
|
@@ -198,6 +198,20 @@ export type ColorChanges = {
|
|
|
198
198
|
rgb: (value: RGBColor) => void;
|
|
199
199
|
rgba: (value: RGBAColor) => void;
|
|
200
200
|
};
|
|
201
|
+
export type ColorDefaults = {
|
|
202
|
+
alpha: Alpha;
|
|
203
|
+
hexBlack: string;
|
|
204
|
+
hexWhite: string;
|
|
205
|
+
hsl: HSLColor;
|
|
206
|
+
hwb: HWBColor;
|
|
207
|
+
rgb: RGBColor;
|
|
208
|
+
};
|
|
209
|
+
export type ColorExpressions = {
|
|
210
|
+
alphaHex: RegExp;
|
|
211
|
+
hexLong: RegExp;
|
|
212
|
+
hexShort: RegExp;
|
|
213
|
+
prefix: RegExp;
|
|
214
|
+
};
|
|
201
215
|
type ColorWithAlpha = {
|
|
202
216
|
/**
|
|
203
217
|
* Alpha channel _(opacity)_ of the color _(in percentage; 0-100)_
|
|
@@ -268,7 +282,7 @@ export type RGBColor = {
|
|
|
268
282
|
*/
|
|
269
283
|
red: number;
|
|
270
284
|
};
|
|
271
|
-
export type ColorProperty =
|
|
285
|
+
export type ColorProperty = "alpha" | keyof HSLColor | keyof HWBColor | keyof RGBColor;
|
|
272
286
|
export type ColorState = {
|
|
273
287
|
alpha: Alpha;
|
|
274
288
|
hex?: string;
|
|
@@ -280,11 +294,11 @@ export type ColorState = {
|
|
|
280
294
|
/**
|
|
281
295
|
* Base color types
|
|
282
296
|
*/
|
|
283
|
-
export type ColorType =
|
|
297
|
+
export type ColorType = "hex" | "hsl" | "hwb" | "rgb";
|
|
284
298
|
/**
|
|
285
299
|
* Extended color types
|
|
286
300
|
*/
|
|
287
|
-
export type ColorTypeExtended = ColorType |
|
|
301
|
+
export type ColorTypeExtended = ColorType | "hsla" | "hwba" | "rgba";
|
|
288
302
|
/**
|
|
289
303
|
* Base color values
|
|
290
304
|
*/
|
package/dist/color/space/hsl.mjs
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { max, min } from "../../internal/math/aggregate.mjs";
|
|
1
2
|
import { COLOR_DEFAULTS, COLOR_MAX, COLOR_TYPE } from "../constants.mjs";
|
|
2
3
|
import { getAlphaValue } from "../misc/alpha.mjs";
|
|
3
4
|
import { getDegrees, getHexValue, getPercentage } from "../misc/get.mjs";
|
|
@@ -9,7 +10,7 @@ function convertHslToHwba(input) {
|
|
|
9
10
|
let { hue, lightness, saturation } = isHslLike(input) ? getHslValues(input) : { ...COLOR_DEFAULTS.hsl };
|
|
10
11
|
lightness /= COLOR_MAX.percent;
|
|
11
12
|
saturation /= COLOR_MAX.percent;
|
|
12
|
-
const value = lightness + saturation *
|
|
13
|
+
const value = lightness + saturation * min([lightness, 1 - lightness]);
|
|
13
14
|
if (value === 0) saturation = 0;
|
|
14
15
|
else saturation = 2 * (1 - lightness / value);
|
|
15
16
|
const blackness = 1 - value;
|
|
@@ -46,7 +47,11 @@ function getColorFromHsl(state, type) {
|
|
|
46
47
|
}
|
|
47
48
|
function getHexyValue(hue, lightness, saturation, value) {
|
|
48
49
|
const part = (value + hue / 30) % 12;
|
|
49
|
-
return (lightness - saturation *
|
|
50
|
+
return (lightness - saturation * min([lightness, 1 - lightness]) * max([-1, min([
|
|
51
|
+
part - 3,
|
|
52
|
+
9 - part,
|
|
53
|
+
1
|
|
54
|
+
])])) * COLOR_MAX.hex;
|
|
50
55
|
}
|
|
51
56
|
function getHslValue() {
|
|
52
57
|
return getStateValue(this, COLOR_TYPE.hsl, false);
|
package/dist/color/space/hwb.mjs
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { min } from "../../internal/math/aggregate.mjs";
|
|
1
2
|
import { COLOR_DEFAULTS, COLOR_MAX, COLOR_TYPE } from "../constants.mjs";
|
|
2
3
|
import { getAlphaValue } from "../misc/alpha.mjs";
|
|
3
4
|
import { getDegrees, getPercentage } from "../misc/get.mjs";
|
|
@@ -38,7 +39,7 @@ function getColorFromHwb(state, type) {
|
|
|
38
39
|
function getHwbSaturation(blackness, whiteness, lightness) {
|
|
39
40
|
const hue = 1 - blackness - whiteness;
|
|
40
41
|
if (hue <= 0) return 0;
|
|
41
|
-
return hue /
|
|
42
|
+
return hue / min([2 * lightness, 2 - 2 * lightness]);
|
|
42
43
|
}
|
|
43
44
|
function getHwbValue() {
|
|
44
45
|
return getStateValue(this, COLOR_TYPE.hwb, false);
|
package/dist/color/space/rgb.mjs
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { max, min } from "../../internal/math/aggregate.mjs";
|
|
1
2
|
import { join } from "../../internal/string/misc.mjs";
|
|
2
3
|
import { COLOR_DEFAULTS, COLOR_MAX, COLOR_TYPE } from "../constants.mjs";
|
|
3
4
|
import { getAlpha, getAlphaValue } from "../misc/alpha.mjs";
|
|
@@ -20,12 +21,12 @@ function convertRgbToHex(rgb, alpha) {
|
|
|
20
21
|
}
|
|
21
22
|
function convertRgbToHsla(value) {
|
|
22
23
|
const values = getRgbCalculationValues(isRgbLike(value) ? getRgbValues(value) : { ...COLOR_DEFAULTS.rgb });
|
|
23
|
-
const { delta,
|
|
24
|
-
const lightness = (
|
|
24
|
+
const { delta, maximum, minimum } = values;
|
|
25
|
+
const lightness = (minimum + maximum) / 2;
|
|
25
26
|
let hue = 0;
|
|
26
27
|
let saturation = 0;
|
|
27
28
|
if (delta !== 0) {
|
|
28
|
-
saturation = (
|
|
29
|
+
saturation = (maximum - lightness) / min([lightness, 1 - lightness]);
|
|
29
30
|
hue = getRgbHue(values);
|
|
30
31
|
hue *= 60;
|
|
31
32
|
}
|
|
@@ -38,7 +39,7 @@ function convertRgbToHsla(value) {
|
|
|
38
39
|
}
|
|
39
40
|
function convertRgbToHwba(value) {
|
|
40
41
|
const values = getRgbCalculationValues(isRgbLike(value) ? getRgbValues(value) : { ...COLOR_DEFAULTS.rgb });
|
|
41
|
-
const { delta,
|
|
42
|
+
const { delta, maximum, minimum } = values;
|
|
42
43
|
let hue = 0;
|
|
43
44
|
if (delta !== 0) {
|
|
44
45
|
hue = getRgbHue(values);
|
|
@@ -46,8 +47,8 @@ function convertRgbToHwba(value) {
|
|
|
46
47
|
}
|
|
47
48
|
return {
|
|
48
49
|
hue,
|
|
49
|
-
whiteness:
|
|
50
|
-
blackness: (1 -
|
|
50
|
+
whiteness: minimum * COLOR_MAX.percent,
|
|
51
|
+
blackness: (1 - maximum) * COLOR_MAX.percent,
|
|
51
52
|
alpha: getAlphaValue(value?.alpha ?? COLOR_MAX.percent)
|
|
52
53
|
};
|
|
53
54
|
}
|
|
@@ -67,14 +68,22 @@ function getRgbCalculationValues(rgb) {
|
|
|
67
68
|
const blue = rgb.blue / COLOR_MAX.hex;
|
|
68
69
|
const green = rgb.green / COLOR_MAX.hex;
|
|
69
70
|
const red = rgb.red / COLOR_MAX.hex;
|
|
70
|
-
const
|
|
71
|
-
|
|
71
|
+
const maximum = max([
|
|
72
|
+
blue,
|
|
73
|
+
green,
|
|
74
|
+
red
|
|
75
|
+
]);
|
|
76
|
+
const minimum = min([
|
|
77
|
+
blue,
|
|
78
|
+
green,
|
|
79
|
+
red
|
|
80
|
+
]);
|
|
72
81
|
return {
|
|
73
82
|
blue,
|
|
74
|
-
delta:
|
|
83
|
+
delta: maximum - minimum,
|
|
75
84
|
green,
|
|
76
|
-
|
|
77
|
-
|
|
85
|
+
maximum,
|
|
86
|
+
minimum,
|
|
78
87
|
red
|
|
79
88
|
};
|
|
80
89
|
}
|
|
@@ -85,8 +94,8 @@ function getRgbaColor() {
|
|
|
85
94
|
return getStateValue(this, COLOR_TYPE.rgb, true);
|
|
86
95
|
}
|
|
87
96
|
function getRgbHue(values) {
|
|
88
|
-
const { blue, delta, green,
|
|
89
|
-
switch (
|
|
97
|
+
const { blue, delta, green, maximum, red } = values;
|
|
98
|
+
switch (maximum) {
|
|
90
99
|
case blue: return (red - green) / delta + 4;
|
|
91
100
|
case green: return (blue - red) / delta + 2;
|
|
92
101
|
default: return (green - blue) / delta + (green < blue ? 6 : 0);
|
|
@@ -37,9 +37,6 @@ export declare function asyncThrottle<Callback extends GenericAsyncCallback | Ge
|
|
|
37
37
|
* @returns Debounced callback handler with a `cancel` method
|
|
38
38
|
*/
|
|
39
39
|
export declare function debounce<Callback extends GenericCallback>(callback: Callback, time?: number): Limiter<Callback>;
|
|
40
|
-
export declare namespace debounce {
|
|
41
|
-
var async: typeof asyncDebounce;
|
|
42
|
-
}
|
|
43
40
|
/**
|
|
44
41
|
* Throttle a function, ensuring it is only called once every `time` milliseconds
|
|
45
42
|
*
|
|
@@ -48,8 +45,11 @@ export declare namespace debounce {
|
|
|
48
45
|
* @returns Throttled callback handler with a `cancel` method
|
|
49
46
|
*/
|
|
50
47
|
export declare function throttle<Callback extends GenericCallback>(callback: Callback, time?: number): Limiter<Callback>;
|
|
48
|
+
export declare namespace debounce {
|
|
49
|
+
export var async: typeof asyncDebounce;
|
|
50
|
+
}
|
|
51
51
|
export declare namespace throttle {
|
|
52
|
-
var async: typeof asyncThrottle;
|
|
52
|
+
export var async: typeof asyncThrottle;
|
|
53
53
|
}
|
|
54
54
|
//#endregion
|
|
55
55
|
export type { AsyncLimiter, Limiter };
|
package/dist/function/once.d.mts
CHANGED
|
@@ -1,7 +1,5 @@
|
|
|
1
1
|
import { GenericAsyncCallback, GenericCallback, Once, OnceAsync } from "../models.mjs";
|
|
2
2
|
//#region src/function/once.d.ts
|
|
3
|
-
declare function OnceAsync(this: any, callback: GenericAsyncCallback): void;
|
|
4
|
-
declare function Once(this: any, callback: GenericCallback): void;
|
|
5
3
|
/**
|
|
6
4
|
* Create an asynchronous function that can only be called once, rejecting or resolving the same result on subsequent calls
|
|
7
5
|
*
|
|
@@ -11,9 +9,6 @@ declare function Once(this: any, callback: GenericCallback): void;
|
|
|
11
9
|
* @returns _Once_ callback
|
|
12
10
|
*/
|
|
13
11
|
export declare function asyncOnce<Callback extends GenericAsyncCallback>(callback: Callback): OnceAsync<Callback>;
|
|
14
|
-
export declare namespace asyncOnce {
|
|
15
|
-
var is: typeof isAsyncOnce;
|
|
16
|
-
}
|
|
17
12
|
/**
|
|
18
13
|
* Is the value an asynchronous once callback?
|
|
19
14
|
*
|
|
@@ -35,9 +30,12 @@ export declare function isOnce(value: unknown): boolean;
|
|
|
35
30
|
* @returns _Once_ callback
|
|
36
31
|
*/
|
|
37
32
|
export declare function once<Callback extends GenericCallback>(callback: Callback): Once<Callback>;
|
|
33
|
+
export declare namespace asyncOnce {
|
|
34
|
+
export var is: typeof isAsyncOnce;
|
|
35
|
+
}
|
|
38
36
|
export declare namespace once {
|
|
39
|
-
var async: typeof asyncOnce;
|
|
40
|
-
var is: typeof isOnce;
|
|
41
|
-
var isAsync: typeof isAsyncOnce;
|
|
37
|
+
export var async: typeof asyncOnce;
|
|
38
|
+
export var is: typeof isOnce;
|
|
39
|
+
export var isAsync: typeof isAsyncOnce;
|
|
42
40
|
}
|
|
43
41
|
//#endregion
|
|
@@ -39,8 +39,8 @@ declare function asyncRetry<Callback extends GenericCallback>(callback: Callback
|
|
|
39
39
|
* @param options Retry options
|
|
40
40
|
* @returns Callback result
|
|
41
41
|
*/
|
|
42
|
-
export declare function retry<Callback extends GenericCallback>(callback: Callback, options?: Omit<RetryOptions,
|
|
42
|
+
export declare function retry<Callback extends GenericCallback>(callback: Callback, options?: Omit<RetryOptions, "delay">): ReturnType<Callback>;
|
|
43
43
|
export declare namespace retry {
|
|
44
|
-
var async: typeof asyncRetry;
|
|
44
|
+
export var async: typeof asyncRetry;
|
|
45
45
|
}
|
|
46
46
|
//#endregion
|
package/dist/function/retry.mjs
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { isPlainObject } from "../internal/is.mjs";
|
|
2
|
+
import { asyncAttempt, attempt } from "../internal/result/attempt.mjs";
|
|
2
3
|
import { getNumberOrDefault } from "../internal/defaults.mjs";
|
|
3
4
|
import { LIMITER_WAIT, getLimiter } from "../internal/function/limit.mjs";
|
|
4
5
|
//#region src/function/retry.ts
|
|
@@ -25,15 +26,12 @@ var RetryError = class extends Error {
|
|
|
25
26
|
async function asyncRetry(callback, options) {
|
|
26
27
|
if (typeof callback !== "function") throw new TypeError(RETRY_MESSAGE_EXPECTATION);
|
|
27
28
|
async function handle() {
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
attempts += 1;
|
|
35
|
-
limiter.run();
|
|
36
|
-
}
|
|
29
|
+
const result = await asyncAttempt(async () => callback());
|
|
30
|
+
if (result.ok) resolver(result.value);
|
|
31
|
+
else if (attempts >= times || !when(result.error)) rejector(new RetryError(RETRY_MESSAGE_FAILED, result.error));
|
|
32
|
+
else {
|
|
33
|
+
attempts += 1;
|
|
34
|
+
limiter.run();
|
|
37
35
|
}
|
|
38
36
|
}
|
|
39
37
|
const { delay, times, when } = createRetryOptions(options);
|
|
@@ -66,11 +64,11 @@ function retry(callback, options) {
|
|
|
66
64
|
if (typeof callback !== "function") throw new TypeError(RETRY_MESSAGE_EXPECTATION);
|
|
67
65
|
const { times, when } = createRetryOptions(options);
|
|
68
66
|
let last;
|
|
69
|
-
for (let index = 0; index <= times; index += 1)
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
if (index >= times || !when(error)) {
|
|
73
|
-
last = error;
|
|
67
|
+
for (let index = 0; index <= times; index += 1) {
|
|
68
|
+
const result = attempt(callback);
|
|
69
|
+
if (result.ok) return result.value;
|
|
70
|
+
if (index >= times || !when(result.error)) {
|
|
71
|
+
last = result.error;
|
|
74
72
|
break;
|
|
75
73
|
}
|
|
76
74
|
}
|