@oscarpalmer/atoms 0.184.2 → 0.185.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/index.d.mts +2 -2
- package/dist/array/index.mjs +2 -2
- package/dist/array/{position.d.mts → match.d.mts} +9 -6
- package/dist/array/{position.mjs → match.mjs} +16 -16
- package/dist/array/move.mjs +1 -1
- package/dist/array/sort.d.mts +9 -4
- package/dist/array/sort.mjs +1 -1
- package/dist/array/swap.mjs +1 -1
- package/dist/beacon.d.mts +12 -0
- package/dist/beacon.mjs +9 -0
- package/dist/color/instance.d.mts +8 -0
- package/dist/color/instance.mjs +3 -0
- package/dist/color/models.d.mts +30 -0
- package/dist/function/assert.d.mts +29 -8
- package/dist/function/assert.mjs +29 -8
- package/dist/function/memoize.d.mts +3 -0
- package/dist/function/memoize.mjs +3 -0
- package/dist/function/retry.d.mts +3 -0
- package/dist/function/retry.mjs +3 -0
- package/dist/function/work.mjs +1 -1
- package/dist/index.d.mts +271 -158
- package/dist/index.mjs +230 -163
- package/dist/internal/value/compare.d.mts +2 -1
- package/dist/internal/value/equal.d.mts +5 -0
- package/dist/internal/value/get.d.mts +2 -2
- package/dist/internal/value/has.d.mts +3 -3
- package/dist/internal/value/has.mjs +1 -1
- package/dist/internal/value/misc.d.mts +2 -2
- package/dist/internal/value/misc.mjs +10 -4
- package/dist/logger.d.mts +11 -0
- package/dist/logger.mjs +11 -0
- package/dist/promise/helpers.mjs +1 -1
- package/dist/promise/index.d.mts +0 -6
- package/dist/promise/models.d.mts +36 -0
- package/dist/promise/models.mjs +6 -0
- package/dist/queue.d.mts +13 -1
- package/dist/queue.mjs +9 -0
- package/dist/result/index.d.mts +0 -8
- package/dist/result/index.mjs +0 -8
- package/dist/result/match.d.mts +4 -4
- package/dist/result/work/flow.d.mts +12 -36
- package/dist/result/work/pipe.d.mts +11 -33
- package/dist/sized/set.d.mts +3 -2
- package/dist/sized/set.mjs +3 -2
- package/dist/value/handle.mjs +1 -1
- package/dist/value/shake.d.mts +3 -0
- package/dist/value/smush.d.mts +3 -0
- package/dist/value/transform.d.mts +9 -0
- package/dist/value/unsmush.d.mts +3 -0
- package/package.json +2 -2
- package/src/array/difference.ts +4 -0
- package/src/array/from.ts +4 -0
- package/src/array/index.ts +1 -1
- package/src/array/intersection.ts +4 -0
- package/src/array/{position.ts → match.ts} +28 -25
- package/src/array/move.ts +5 -1
- package/src/array/reverse.ts +4 -0
- package/src/array/select.ts +2 -0
- package/src/array/sort.ts +9 -4
- package/src/array/swap.ts +5 -1
- package/src/array/toggle.ts +4 -0
- package/src/array/union.ts +4 -0
- package/src/beacon.ts +12 -0
- package/src/color/index.ts +0 -3
- package/src/color/instance.ts +9 -1
- package/src/color/models.ts +30 -0
- package/src/function/assert.ts +66 -7
- package/src/function/memoize.ts +3 -0
- package/src/function/once.ts +5 -1
- package/src/function/retry.ts +3 -0
- package/src/internal/value/compare.ts +2 -1
- package/src/internal/value/equal.ts +5 -0
- package/src/internal/value/get.ts +2 -2
- package/src/internal/value/has.ts +6 -6
- package/src/internal/value/misc.ts +24 -13
- package/src/logger.ts +11 -0
- package/src/promise/index.ts +0 -6
- package/src/promise/models.ts +36 -0
- package/src/queue.ts +13 -1
- package/src/result/index.ts +0 -8
- package/src/result/match.ts +4 -4
- package/src/result/work/flow.ts +12 -36
- package/src/result/work/pipe.ts +11 -33
- package/src/sized/set.ts +4 -3
- package/src/value/shake.ts +3 -0
- package/src/value/smush.ts +3 -0
- package/src/value/transform.ts +9 -0
- package/src/value/unsmush.ts +3 -0
package/src/color/instance.ts
CHANGED
|
@@ -4,9 +4,17 @@ import {getAlpha} from './misc/alpha';
|
|
|
4
4
|
import {getColorState, setHexColor, setHSLColor, setRGBColor} from './misc/state';
|
|
5
5
|
import type {ColorState, HSLAColor, HSLColor, RGBAColor, RGBColor} from './models';
|
|
6
6
|
|
|
7
|
-
// #region
|
|
7
|
+
// #region Types
|
|
8
8
|
|
|
9
|
+
/**
|
|
10
|
+
* A color that is represented in multiple color formats
|
|
11
|
+
*/
|
|
9
12
|
export class Color {
|
|
13
|
+
/**
|
|
14
|
+
* A property to identify this as a Color instance, used for type checking
|
|
15
|
+
*
|
|
16
|
+
* @internal
|
|
17
|
+
*/
|
|
10
18
|
declare private readonly $color: boolean;
|
|
11
19
|
|
|
12
20
|
readonly #state: ColorState;
|
package/src/color/models.ts
CHANGED
|
@@ -9,19 +9,49 @@ type ColorWithAlpha = {
|
|
|
9
9
|
alpha: number;
|
|
10
10
|
};
|
|
11
11
|
|
|
12
|
+
/**
|
|
13
|
+
* An _HSL_-color with an alpha channel
|
|
14
|
+
*/
|
|
12
15
|
export type HSLAColor = HSLColor & ColorWithAlpha;
|
|
13
16
|
|
|
17
|
+
/**
|
|
18
|
+
* An _HSL_-color
|
|
19
|
+
*/
|
|
14
20
|
export type HSLColor = {
|
|
21
|
+
/**
|
|
22
|
+
* Hue of the color _(in degrees; 0-360)_
|
|
23
|
+
*/
|
|
15
24
|
hue: number;
|
|
25
|
+
/**
|
|
26
|
+
* Lightness of the color _(in percentage; 0-100)_
|
|
27
|
+
*/
|
|
16
28
|
lightness: number;
|
|
29
|
+
/**
|
|
30
|
+
* Saturation of the color _(in percentage; 0-100)_
|
|
31
|
+
*/
|
|
17
32
|
saturation: number;
|
|
18
33
|
};
|
|
19
34
|
|
|
35
|
+
/**
|
|
36
|
+
* An _RGB_-color with an alpha channel
|
|
37
|
+
*/
|
|
20
38
|
export type RGBAColor = RGBColor & ColorWithAlpha;
|
|
21
39
|
|
|
40
|
+
/**
|
|
41
|
+
* An _RGB_-color
|
|
42
|
+
*/
|
|
22
43
|
export type RGBColor = {
|
|
44
|
+
/**
|
|
45
|
+
* Blue channel of the color _(in hexadecimal; 0-255)_
|
|
46
|
+
*/
|
|
23
47
|
blue: number;
|
|
48
|
+
/**
|
|
49
|
+
* Green channel of the color _(in hexadecimal; 0-255)_
|
|
50
|
+
*/
|
|
24
51
|
green: number;
|
|
52
|
+
/**
|
|
53
|
+
* Red channel of the color _(in hexadecimal; 0-255)_
|
|
54
|
+
*/
|
|
25
55
|
red: number;
|
|
26
56
|
};
|
|
27
57
|
|
package/src/function/assert.ts
CHANGED
|
@@ -1,9 +1,32 @@
|
|
|
1
|
-
import
|
|
1
|
+
import {hasValueResult} from '../internal/value/has';
|
|
2
|
+
import type {Constructor, NestedKeys, NestedValue, PlainObject} from '../models';
|
|
2
3
|
|
|
3
4
|
// #region Types
|
|
4
5
|
|
|
6
|
+
/**
|
|
7
|
+
* Asserter for a nested property of a value
|
|
8
|
+
*/
|
|
9
|
+
export type AssertProperty<
|
|
10
|
+
Value extends PlainObject,
|
|
11
|
+
Path extends NestedKeys<Value>,
|
|
12
|
+
Asserted extends NestedPick<Value, Path> = NestedPick<Value, Path>,
|
|
13
|
+
> = Asserter<Asserted>;
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* A function that asserts a value is of a specific type, throwing an error if it is not
|
|
17
|
+
*/
|
|
5
18
|
export type Asserter<Value> = (value: unknown) => asserts value is Value;
|
|
6
19
|
|
|
20
|
+
type NestedPick<Value, Path extends string> = Value extends PlainObject
|
|
21
|
+
? Path extends `${infer Head}.${infer Rest}`
|
|
22
|
+
? Head extends keyof Value
|
|
23
|
+
? {[Key in Head]: NestedPick<Value[Key], Rest>}
|
|
24
|
+
: never
|
|
25
|
+
: Path extends keyof Value
|
|
26
|
+
? {[Key in Path]: Value[Key]}
|
|
27
|
+
: never
|
|
28
|
+
: never;
|
|
29
|
+
|
|
7
30
|
// #endregion
|
|
8
31
|
|
|
9
32
|
// #region Functions
|
|
@@ -12,7 +35,7 @@ export type Asserter<Value> = (value: unknown) => asserts value is Value;
|
|
|
12
35
|
* Asserts that a condition is true, throwing an error if it is not
|
|
13
36
|
* @param condition Condition to assert
|
|
14
37
|
* @param message Error message
|
|
15
|
-
* @param error Error constructor
|
|
38
|
+
* @param error Error constructor _(defaults to `Error`)_
|
|
16
39
|
*/
|
|
17
40
|
export function assert<Condition extends () => boolean>(
|
|
18
41
|
condition: Condition,
|
|
@@ -28,6 +51,7 @@ assert.condition = assertCondition;
|
|
|
28
51
|
assert.defined = assertDefined;
|
|
29
52
|
assert.instanceOf = assertInstanceOf;
|
|
30
53
|
assert.is = assertIs;
|
|
54
|
+
assert.property = assertProperty;
|
|
31
55
|
|
|
32
56
|
/**
|
|
33
57
|
* Creates an asserter that asserts a condition is true, throwing an error if it is not
|
|
@@ -35,7 +59,7 @@ assert.is = assertIs;
|
|
|
35
59
|
* Available as `assertCondition` and `assert.condition`
|
|
36
60
|
* @param condition Condition to assert
|
|
37
61
|
* @param message Error message
|
|
38
|
-
* @param error Error constructor
|
|
62
|
+
* @param error Error constructor _(defaults to `Error`)_
|
|
39
63
|
* @returns Asserter
|
|
40
64
|
*/
|
|
41
65
|
export function assertCondition<Value>(
|
|
@@ -49,17 +73,19 @@ export function assertCondition<Value>(
|
|
|
49
73
|
}
|
|
50
74
|
|
|
51
75
|
/**
|
|
52
|
-
* Asserts that a value is defined throwing an error if it is not
|
|
76
|
+
* Asserts that a value is defined, throwing an error if it is not
|
|
53
77
|
*
|
|
54
78
|
* Available as `assertDefined` and `assert.defined`
|
|
55
79
|
* @param value Value to assert
|
|
56
80
|
* @param message Error message
|
|
81
|
+
* @param error Error constructor _(defaults to `Error`)_
|
|
57
82
|
*/
|
|
58
83
|
export function assertDefined<Value>(
|
|
59
84
|
value: unknown,
|
|
60
85
|
message?: string,
|
|
86
|
+
error?: ErrorConstructor,
|
|
61
87
|
): asserts value is Exclude<Value, null | undefined> {
|
|
62
|
-
assert(() => value != null, message ?? MESSAGE_VALUE_DEFINED);
|
|
88
|
+
assert(() => value != null, message ?? MESSAGE_VALUE_DEFINED, error);
|
|
63
89
|
}
|
|
64
90
|
|
|
65
91
|
/**
|
|
@@ -68,7 +94,7 @@ export function assertDefined<Value>(
|
|
|
68
94
|
* Available as `assertInstanceOf` and `assert.instanceOf`
|
|
69
95
|
* @param constructor Constructor to check against
|
|
70
96
|
* @param message Error message
|
|
71
|
-
* @param error Error constructor
|
|
97
|
+
* @param error Error constructor _(defaults to `Error`)_
|
|
72
98
|
* @returns Asserter
|
|
73
99
|
*/
|
|
74
100
|
export function assertInstanceOf<Value>(
|
|
@@ -87,7 +113,7 @@ export function assertInstanceOf<Value>(
|
|
|
87
113
|
* Available as `assertIs` and `assert.is`
|
|
88
114
|
* @param condition Type guard function to check the value
|
|
89
115
|
* @param message Error message
|
|
90
|
-
* @param error Error constructor
|
|
116
|
+
* @param error Error constructor _(defaults to `Error`)_
|
|
91
117
|
* @returns Asserter
|
|
92
118
|
*/
|
|
93
119
|
export function assertIs<Value>(
|
|
@@ -100,6 +126,39 @@ export function assertIs<Value>(
|
|
|
100
126
|
};
|
|
101
127
|
}
|
|
102
128
|
|
|
129
|
+
/**
|
|
130
|
+
* Creates an asserter that asserts a property of a value exists and satisfies a condition, throwing an error if it does not
|
|
131
|
+
*
|
|
132
|
+
* Available as `assertProperty` and `assert.property`
|
|
133
|
+
* @param path Path to the property to check, e.g., `foo.bar.baz` for a nested property
|
|
134
|
+
* @param condition Condition to assert for the property
|
|
135
|
+
* @param message Error message
|
|
136
|
+
* @param error Error constructor _(defaults to `Error`)_
|
|
137
|
+
* @returns Asserter
|
|
138
|
+
*/
|
|
139
|
+
export function assertProperty<
|
|
140
|
+
Value extends PlainObject,
|
|
141
|
+
Path extends NestedKeys<Value>,
|
|
142
|
+
Asserted = NestedPick<Value, Path>,
|
|
143
|
+
>(
|
|
144
|
+
path: Path,
|
|
145
|
+
condition: (value: NestedValue<Value, Path>) => boolean,
|
|
146
|
+
message: string,
|
|
147
|
+
error?: ErrorConstructor,
|
|
148
|
+
): Asserter<Asserted> {
|
|
149
|
+
return (value: unknown): asserts value is Asserted => {
|
|
150
|
+
assert(
|
|
151
|
+
() => {
|
|
152
|
+
const result = hasValueResult(value as never, path, false);
|
|
153
|
+
|
|
154
|
+
return result.ok && condition(result.value as never);
|
|
155
|
+
},
|
|
156
|
+
message,
|
|
157
|
+
error,
|
|
158
|
+
);
|
|
159
|
+
};
|
|
160
|
+
}
|
|
161
|
+
|
|
103
162
|
// #endregion
|
|
104
163
|
|
|
105
164
|
// #region Variables
|
package/src/function/memoize.ts
CHANGED
|
@@ -5,6 +5,9 @@ import {SizedMap} from '../sized/map';
|
|
|
5
5
|
|
|
6
6
|
// #region Types
|
|
7
7
|
|
|
8
|
+
/**
|
|
9
|
+
* A memoized function, caching and retrieving results based on the its parameters _(or a custom cache key)_
|
|
10
|
+
*/
|
|
8
11
|
class Memoized<Callback extends GenericCallback> {
|
|
9
12
|
readonly #state: MemoizedState<Callback>;
|
|
10
13
|
|
package/src/function/once.ts
CHANGED
|
@@ -107,7 +107,11 @@ export function asyncOnce<Callback extends GenericAsyncCallback>(
|
|
|
107
107
|
return fn as OnceAsyncCallback<Callback>;
|
|
108
108
|
}
|
|
109
109
|
|
|
110
|
-
function handleOnceResult<Value>(
|
|
110
|
+
function handleOnceResult<Value>(
|
|
111
|
+
state: OnceAsyncState<Value>,
|
|
112
|
+
value: unknown,
|
|
113
|
+
error: boolean,
|
|
114
|
+
): void {
|
|
111
115
|
state.error = error;
|
|
112
116
|
state.finished = true;
|
|
113
117
|
state.value = value as Value;
|
package/src/function/retry.ts
CHANGED
|
@@ -45,6 +45,7 @@ export function compare(first: unknown, second: unknown): number {
|
|
|
45
45
|
const firstParts = getComparisonParts(first);
|
|
46
46
|
const secondParts = getComparisonParts(second);
|
|
47
47
|
const length = max([firstParts.length, secondParts.length]);
|
|
48
|
+
|
|
48
49
|
const lastIndex = length - 1;
|
|
49
50
|
|
|
50
51
|
for (let index = 0; index < length; index += 1) {
|
|
@@ -147,7 +148,7 @@ function getComparisonParts(value: unknown): unknown[] {
|
|
|
147
148
|
*/
|
|
148
149
|
export function registerComparator<Instance>(
|
|
149
150
|
constructor: Constructor<Instance>,
|
|
150
|
-
handler?: string |
|
|
151
|
+
handler?: string | Comparator<Instance>,
|
|
151
152
|
): void {
|
|
152
153
|
compare.handlers.register(constructor, handler);
|
|
153
154
|
}
|
|
@@ -22,6 +22,11 @@ export type EqualOptions = {
|
|
|
22
22
|
relaxedNullish?: boolean;
|
|
23
23
|
};
|
|
24
24
|
|
|
25
|
+
/**
|
|
26
|
+
* An equalizer function for comparing values for equality, with predefined options
|
|
27
|
+
*
|
|
28
|
+
* Can be used to compare values, and register or deregister equality comparison handlers for specific classes
|
|
29
|
+
*/
|
|
25
30
|
type Equalizer = {
|
|
26
31
|
/**
|
|
27
32
|
* Are two strings equal?
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type {NestedKeys, NestedValue, PlainObject
|
|
1
|
+
import type {NestedKeys, NestedValue, PlainObject} from '../../models';
|
|
2
2
|
import type {Ok} from '../../result/models';
|
|
3
3
|
import {getNestedValue} from './misc';
|
|
4
4
|
|
|
@@ -13,7 +13,7 @@ import {getNestedValue} from './misc';
|
|
|
13
13
|
export function getValue<Data extends PlainObject, Path extends NestedKeys<Data>>(
|
|
14
14
|
data: Data,
|
|
15
15
|
path: Path,
|
|
16
|
-
): NestedValue<Data,
|
|
16
|
+
): NestedValue<Data, Path>;
|
|
17
17
|
|
|
18
18
|
/**
|
|
19
19
|
* Get the value from an object using an unknown path
|
|
@@ -43,11 +43,11 @@ hasValue.get = hasValueResult;
|
|
|
43
43
|
* @param ignoreCase If `true`, the path matching is case-insensitive
|
|
44
44
|
* @return Result object
|
|
45
45
|
*/
|
|
46
|
-
function hasValueResult<Data extends PlainObject, Path extends NestedKeys<Data>>(
|
|
46
|
+
export function hasValueResult<Data extends PlainObject, Path extends NestedKeys<Data>>(
|
|
47
47
|
data: Data,
|
|
48
48
|
path: Path,
|
|
49
49
|
ignoreCase?: boolean,
|
|
50
|
-
): Result<NestedValue<Data, ToString<Path>>,
|
|
50
|
+
): Result<NestedValue<Data, ToString<Path>>, string>;
|
|
51
51
|
|
|
52
52
|
/**
|
|
53
53
|
* Check if a nested property is defined in an object, and get its value if it is
|
|
@@ -58,17 +58,17 @@ function hasValueResult<Data extends PlainObject, Path extends NestedKeys<Data>>
|
|
|
58
58
|
* @param ignoreCase If `true`, the path matching is case-insensitive
|
|
59
59
|
* @return Result object
|
|
60
60
|
*/
|
|
61
|
-
function hasValueResult<Data extends PlainObject>(
|
|
61
|
+
export function hasValueResult<Data extends PlainObject>(
|
|
62
62
|
data: Data,
|
|
63
63
|
path: string,
|
|
64
64
|
ignoreCase?: boolean,
|
|
65
|
-
): Result<unknown,
|
|
65
|
+
): Result<unknown, string>;
|
|
66
66
|
|
|
67
|
-
function hasValueResult(
|
|
67
|
+
export function hasValueResult(
|
|
68
68
|
data: PlainObject,
|
|
69
69
|
path: string,
|
|
70
70
|
ignoreCase?: boolean,
|
|
71
|
-
): Result<unknown,
|
|
71
|
+
): Result<unknown, string> {
|
|
72
72
|
return getNestedValue(data, path, ignoreCase === true);
|
|
73
73
|
}
|
|
74
74
|
|
|
@@ -17,14 +17,13 @@ export function getNestedValue(
|
|
|
17
17
|
data: object,
|
|
18
18
|
path: string,
|
|
19
19
|
ignoreCase: boolean,
|
|
20
|
-
): Result<unknown,
|
|
21
|
-
if (
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
return error(undefined);
|
|
20
|
+
): Result<unknown, string> {
|
|
21
|
+
if (typeof data !== 'object' || data === null) {
|
|
22
|
+
return error(NESTED_MESSAGE_INPUT);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
if (typeof path !== 'string' || path.trim().length === 0) {
|
|
26
|
+
return error(NESTED_MESSAGE_PATH);
|
|
28
27
|
}
|
|
29
28
|
|
|
30
29
|
const shouldIgnoreCase = ignoreCase === true;
|
|
@@ -69,7 +68,7 @@ export function handleValue(
|
|
|
69
68
|
value: unknown,
|
|
70
69
|
get: true,
|
|
71
70
|
ignoreCase: boolean,
|
|
72
|
-
): Result<unknown,
|
|
71
|
+
): Result<unknown, string>;
|
|
73
72
|
|
|
74
73
|
export function handleValue(
|
|
75
74
|
data: object,
|
|
@@ -85,19 +84,23 @@ export function handleValue(
|
|
|
85
84
|
value: unknown,
|
|
86
85
|
get: boolean,
|
|
87
86
|
ignoreCase: boolean,
|
|
88
|
-
): Result<unknown,
|
|
89
|
-
if (typeof data === 'object' && data !== null
|
|
87
|
+
): Result<unknown, string> | void {
|
|
88
|
+
if (typeof data === 'object' && data !== null) {
|
|
89
|
+
if (ignoreKey(path)) {
|
|
90
|
+
return error(NESTED_MESSAGE_UNSAFE);
|
|
91
|
+
}
|
|
92
|
+
|
|
90
93
|
const key = ignoreCase ? findKey(path, data) : path;
|
|
91
94
|
|
|
92
95
|
if (get) {
|
|
93
|
-
return key in data ? ok(data[key as never]) : error(
|
|
96
|
+
return key in data ? ok(data[key as never]) : error(NESTED_MESSAGE_MISSING);
|
|
94
97
|
}
|
|
95
98
|
|
|
96
99
|
(data as PlainObject)[key] = typeof value === 'function' ? value(data[key as never]) : value;
|
|
97
100
|
}
|
|
98
101
|
|
|
99
102
|
if (get) {
|
|
100
|
-
return error(
|
|
103
|
+
return error(NESTED_MESSAGE_MISSING);
|
|
101
104
|
}
|
|
102
105
|
}
|
|
103
106
|
|
|
@@ -111,4 +114,12 @@ const EXPRESSION_DOTS = /^\.|\.$/g;
|
|
|
111
114
|
|
|
112
115
|
const EXPRESSION_NESTED = /\.|\[\w+\]/;
|
|
113
116
|
|
|
117
|
+
const NESTED_MESSAGE_INPUT = 'Expected data to be an object';
|
|
118
|
+
|
|
119
|
+
const NESTED_MESSAGE_MISSING = 'Expected property to exist in object';
|
|
120
|
+
|
|
121
|
+
const NESTED_MESSAGE_PATH = 'Expected path to be a string';
|
|
122
|
+
|
|
123
|
+
const NESTED_MESSAGE_UNSAFE = 'Access to this property is not allowed';
|
|
124
|
+
|
|
114
125
|
// #endregion
|
package/src/logger.ts
CHANGED
|
@@ -2,6 +2,11 @@ import {noop} from './internal/function/misc';
|
|
|
2
2
|
|
|
3
3
|
// #region Types
|
|
4
4
|
|
|
5
|
+
/**
|
|
6
|
+
* A logger that can be used to log messages to the console
|
|
7
|
+
*
|
|
8
|
+
* _(Logging can be enabled or disabled by setting the `enabled` property)_
|
|
9
|
+
*/
|
|
5
10
|
class Logger {
|
|
6
11
|
/**
|
|
7
12
|
* Log any number of values at the "debug" log level
|
|
@@ -83,12 +88,18 @@ class Logger {
|
|
|
83
88
|
}
|
|
84
89
|
}
|
|
85
90
|
|
|
91
|
+
/**
|
|
92
|
+
* A named timer that can be used to log durations to the console
|
|
93
|
+
*/
|
|
86
94
|
class Time {
|
|
87
95
|
#logger: typeof console.timeLog | undefined;
|
|
88
96
|
#stopper: typeof console.timeEnd | undefined;
|
|
89
97
|
|
|
90
98
|
readonly #state: TimeState;
|
|
91
99
|
|
|
100
|
+
/**
|
|
101
|
+
* Is the timer active? _(i.e. has it been started and not stopped, and is logging enabled?)_
|
|
102
|
+
*/
|
|
92
103
|
get active(): boolean {
|
|
93
104
|
return this.#state.started && !this.#state.stopped && enabled;
|
|
94
105
|
}
|
package/src/promise/index.ts
CHANGED
|
@@ -24,8 +24,6 @@ import {getTimedPromise} from './timed';
|
|
|
24
24
|
|
|
25
25
|
/**
|
|
26
26
|
* Wrap a promise with safety handlers, with optional abort capabilities and timeout
|
|
27
|
-
*
|
|
28
|
-
* Available as `attemptPromise` and `attempt.promise`
|
|
29
27
|
* @param promise Promise to wrap
|
|
30
28
|
* @param options Options for the promise
|
|
31
29
|
* @returns Wrapped promise
|
|
@@ -37,8 +35,6 @@ export async function attemptPromise<Value>(
|
|
|
37
35
|
|
|
38
36
|
/**
|
|
39
37
|
* Wrap a promise-returning callback with safety handlers, with optional abort capabilities and timeout
|
|
40
|
-
*
|
|
41
|
-
* Available as `attemptPromise` and `attempt.promise`
|
|
42
38
|
* @param callback Callback to wrap
|
|
43
39
|
* @param options Options for the promise
|
|
44
40
|
* @returns Promise-wrapped callback
|
|
@@ -50,8 +46,6 @@ export async function attemptPromise<Value>(
|
|
|
50
46
|
|
|
51
47
|
/**
|
|
52
48
|
* Wrap a callback with a promise and safety handlers, with optional abort capabilities and timeout
|
|
53
|
-
*
|
|
54
|
-
* Available as `attemptPromise` and `attempt.promise`
|
|
55
49
|
* @param callback Callback to wrap
|
|
56
50
|
* @param options Options for the promise
|
|
57
51
|
* @returns Promise-wrapped callback
|
package/src/promise/models.ts
CHANGED
|
@@ -3,6 +3,9 @@ import type {Result} from '../result/models';
|
|
|
3
3
|
|
|
4
4
|
// #region Types
|
|
5
5
|
|
|
6
|
+
/**
|
|
7
|
+
* A promise that can be canceled
|
|
8
|
+
*/
|
|
6
9
|
export class CancelablePromise<Value = void> extends Promise<Value> {
|
|
7
10
|
#rejector!: (reason: unknown) => void;
|
|
8
11
|
|
|
@@ -29,8 +32,17 @@ export class CancelablePromise<Value = void> extends Promise<Value> {
|
|
|
29
32
|
}
|
|
30
33
|
}
|
|
31
34
|
|
|
35
|
+
/**
|
|
36
|
+
* A promise that was fulfilled
|
|
37
|
+
*/
|
|
32
38
|
export type FulfilledPromise<Value> = {
|
|
39
|
+
/**
|
|
40
|
+
* Status of the promise
|
|
41
|
+
*/
|
|
33
42
|
status: typeof PROMISE_TYPE_FULFILLED;
|
|
43
|
+
/**
|
|
44
|
+
* Value of the promise
|
|
45
|
+
*/
|
|
34
46
|
value: Awaited<Value>;
|
|
35
47
|
};
|
|
36
48
|
|
|
@@ -44,6 +56,9 @@ export type PromiseHandlers = {
|
|
|
44
56
|
reject: (reason: unknown) => void;
|
|
45
57
|
};
|
|
46
58
|
|
|
59
|
+
/**
|
|
60
|
+
* Options for a promise-handling function
|
|
61
|
+
*/
|
|
47
62
|
export type PromiseOptions = {
|
|
48
63
|
/**
|
|
49
64
|
* AbortSignal for aborting the promise; when aborted, the promise will reject with the reason of the signal
|
|
@@ -75,6 +90,9 @@ export type PromiseParameters = {
|
|
|
75
90
|
*/
|
|
76
91
|
export type PromiseStrategy = 'complete' | 'first';
|
|
77
92
|
|
|
93
|
+
/**
|
|
94
|
+
* An error thrown when a promise times out
|
|
95
|
+
*/
|
|
78
96
|
export class PromiseTimeoutError extends Error {
|
|
79
97
|
constructor() {
|
|
80
98
|
super(PROMISE_MESSAGE_TIMEOUT);
|
|
@@ -93,8 +111,17 @@ export type PromisesItems<Items extends unknown[]> = {
|
|
|
93
111
|
: Promise<Items[ItemsKey]>;
|
|
94
112
|
};
|
|
95
113
|
|
|
114
|
+
/**
|
|
115
|
+
* Options for handling multiple promises
|
|
116
|
+
*/
|
|
96
117
|
export type PromisesOptions = {
|
|
118
|
+
/**
|
|
119
|
+
* AbortSignal for aborting the promises; when aborted, the promises will reject with the reason of the signal
|
|
120
|
+
*/
|
|
97
121
|
signal?: AbortSignal;
|
|
122
|
+
/**
|
|
123
|
+
* Strategy for handling the promises; defaults to `complete`
|
|
124
|
+
*/
|
|
98
125
|
strategy?: PromiseStrategy;
|
|
99
126
|
};
|
|
100
127
|
|
|
@@ -126,8 +153,17 @@ export type PromisesValues<Items extends unknown[]> = {
|
|
|
126
153
|
: never;
|
|
127
154
|
};
|
|
128
155
|
|
|
156
|
+
/**
|
|
157
|
+
* A promise that was rejected
|
|
158
|
+
*/
|
|
129
159
|
export type RejectedPromise = {
|
|
160
|
+
/**
|
|
161
|
+
* Status of the promise
|
|
162
|
+
*/
|
|
130
163
|
status: typeof PROMISE_TYPE_REJECTED;
|
|
164
|
+
/**
|
|
165
|
+
* Reason for the rejection
|
|
166
|
+
*/
|
|
131
167
|
reason: unknown;
|
|
132
168
|
};
|
|
133
169
|
|
package/src/queue.ts
CHANGED
|
@@ -5,6 +5,9 @@ import type {GenericAsyncCallback, GenericCallback} from './models';
|
|
|
5
5
|
|
|
6
6
|
type HandleType = 'clear' | 'pause' | 'resume';
|
|
7
7
|
|
|
8
|
+
/**
|
|
9
|
+
* A queue that can be used to manage (a)synchronous tasks with a specific key
|
|
10
|
+
*/
|
|
8
11
|
class KeyedQueue<CallbackParameters extends Parameters<GenericAsyncCallback>, CallbackResult> {
|
|
9
12
|
readonly #callback: GenericAsyncCallback;
|
|
10
13
|
|
|
@@ -240,6 +243,9 @@ class KeyedQueue<CallbackParameters extends Parameters<GenericAsyncCallback>, Ca
|
|
|
240
243
|
}
|
|
241
244
|
}
|
|
242
245
|
|
|
246
|
+
/**
|
|
247
|
+
* A queue that can be used to manage (a)synchronous tasks
|
|
248
|
+
*/
|
|
243
249
|
class Queue<CallbackParameters extends Parameters<GenericAsyncCallback>, CallbackResult> {
|
|
244
250
|
readonly #callback: GenericAsyncCallback;
|
|
245
251
|
|
|
@@ -487,6 +493,9 @@ class Queue<CallbackParameters extends Parameters<GenericAsyncCallback>, Callbac
|
|
|
487
493
|
}
|
|
488
494
|
}
|
|
489
495
|
|
|
496
|
+
/**
|
|
497
|
+
* An error thrown by the Queue when an operation fails
|
|
498
|
+
*/
|
|
490
499
|
class QueueError extends Error {
|
|
491
500
|
constructor(message: string) {
|
|
492
501
|
super(message);
|
|
@@ -510,9 +519,12 @@ type QueueOptions = {
|
|
|
510
519
|
maximum?: number;
|
|
511
520
|
};
|
|
512
521
|
|
|
522
|
+
/**
|
|
523
|
+
* A queued item
|
|
524
|
+
*/
|
|
513
525
|
type Queued<Value> = {
|
|
514
526
|
/**
|
|
515
|
-
* ID of the queued
|
|
527
|
+
* ID of the queued item _(can be used to remove it from the queue)_
|
|
516
528
|
*/
|
|
517
529
|
readonly id: number;
|
|
518
530
|
/**
|
package/src/result/index.ts
CHANGED
|
@@ -1,9 +1,5 @@
|
|
|
1
|
-
import {attemptPromise} from '../promise';
|
|
2
|
-
import {matchResult} from './match';
|
|
3
1
|
import {getError, ok} from './misc';
|
|
4
2
|
import type {ExtendedErr, ExtendedResult, Result} from './models';
|
|
5
|
-
import {attemptFlow} from './work/flow';
|
|
6
|
-
import {attemptPipe} from './work/pipe';
|
|
7
3
|
|
|
8
4
|
// #region Functions
|
|
9
5
|
|
|
@@ -99,9 +95,5 @@ export function attempt<Value, E>(
|
|
|
99
95
|
}
|
|
100
96
|
|
|
101
97
|
attempt.async = asyncAttempt;
|
|
102
|
-
attempt.flow = attemptFlow;
|
|
103
|
-
attempt.match = matchResult;
|
|
104
|
-
attempt.pipe = attemptPipe;
|
|
105
|
-
attempt.promise = attemptPromise;
|
|
106
98
|
|
|
107
99
|
// #endregion
|
package/src/result/match.ts
CHANGED
|
@@ -5,6 +5,8 @@ import type {AnyResult, ExtendedErr, ResultMatch} from './models';
|
|
|
5
5
|
|
|
6
6
|
/**
|
|
7
7
|
* Handles a result with match callbacks
|
|
8
|
+
*
|
|
9
|
+
* Available as `asyncMatchResult` and `matchResult.async`
|
|
8
10
|
* @param result Result to handle
|
|
9
11
|
* @param handler Match callbacks
|
|
10
12
|
*/
|
|
@@ -15,6 +17,8 @@ export async function asyncMatchResult<Value, Returned, E = Error>(
|
|
|
15
17
|
|
|
16
18
|
/**
|
|
17
19
|
* Handles a result with match callbacks
|
|
20
|
+
*
|
|
21
|
+
* Available as `asyncMatchResult` and `matchResult.async`
|
|
18
22
|
* @param result Result to handle
|
|
19
23
|
* @param ok Ok callback
|
|
20
24
|
* @param error Error callback
|
|
@@ -58,8 +62,6 @@ export async function asyncMatchResult<Value, Returned, E = Error>(
|
|
|
58
62
|
|
|
59
63
|
/**
|
|
60
64
|
* Handles a result with match callbacks
|
|
61
|
-
*
|
|
62
|
-
* Available as `matchResult` and `attempt.match`
|
|
63
65
|
* @param result Result to handle
|
|
64
66
|
* @param handler Match callbacks
|
|
65
67
|
*/
|
|
@@ -70,8 +72,6 @@ export function matchResult<Value, Returned, E = Error>(
|
|
|
70
72
|
|
|
71
73
|
/**
|
|
72
74
|
* Handles a result with match callbacks
|
|
73
|
-
*
|
|
74
|
-
* Available as `matchResult` and `attempt.match`
|
|
75
75
|
* @param result Result to handle
|
|
76
76
|
* @param ok Ok callback
|
|
77
77
|
* @param error Error callback
|