@oscarpalmer/atoms 0.193.0 → 0.194.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/difference.mjs +2 -2
- package/dist/array/intersection.mjs +2 -2
- package/dist/array/match.mjs +13 -13
- package/dist/array/slice.mjs +5 -5
- package/dist/array/sort.d.mts +5 -1
- package/dist/array/sort.mjs +4 -2
- package/dist/array/union.mjs +2 -2
- package/dist/beacon.d.mts +8 -1
- package/dist/beacon.mjs +52 -60
- package/dist/color/constants.d.mts +1 -1
- package/dist/color/constants.mjs +3 -5
- package/dist/color/instance.mjs +34 -41
- package/dist/color/misc/format.d.mts +3 -2
- package/dist/color/misc/format.mjs +4 -1
- package/dist/color/misc/is.d.mts +9 -1
- package/dist/color/misc/is.mjs +11 -1
- package/dist/color/misc/state.d.mts +6 -6
- package/dist/color/misc/state.mjs +11 -14
- package/dist/color/models.d.mts +23 -68
- package/dist/function/assert.mjs +2 -2
- package/dist/function/limit.d.mts +0 -6
- package/dist/function/limit.mjs +2 -2
- package/dist/function/memoize.mjs +19 -32
- package/dist/function/once.d.mts +0 -3
- package/dist/function/once.mjs +16 -13
- package/dist/function/retry.d.mts +1 -24
- package/dist/function/retry.mjs +1 -1
- package/dist/function/work.mjs +13 -11
- package/dist/herald.d.mts +30 -31
- package/dist/herald.mjs +74 -56
- package/dist/index.d.mts +100 -185
- package/dist/index.mjs +680 -702
- package/dist/internal/abort.d.mts +10 -0
- package/dist/internal/abort.mjs +14 -0
- package/dist/internal/array/chunk.mjs +2 -2
- package/dist/internal/array/find.mjs +2 -2
- package/dist/internal/array/sets.d.mts +4 -4
- package/dist/internal/array/sets.mjs +7 -7
- package/dist/internal/function/timer.mjs +19 -15
- package/dist/internal/number.mjs +7 -7
- package/dist/internal/result.mjs +3 -3
- package/dist/internal/sized.mjs +4 -4
- package/dist/internal/string.mjs +4 -4
- package/dist/internal/subscription.d.mts +24 -8
- package/dist/internal/subscription.mjs +49 -36
- package/dist/internal/value/equal.mjs +6 -6
- package/dist/internal/value/misc.mjs +14 -14
- package/dist/internal/value/set.mjs +2 -2
- package/dist/logger.mjs +15 -39
- package/dist/promise/constants.d.mts +17 -0
- package/dist/promise/constants.mjs +14 -0
- package/dist/promise/delay.mjs +7 -10
- package/dist/promise/helpers.mjs +1 -1
- package/dist/promise/index.mjs +14 -17
- package/dist/promise/misc.d.mts +2 -1
- package/dist/promise/misc.mjs +8 -7
- package/dist/promise/models.d.mts +4 -16
- package/dist/promise/models.mjs +4 -13
- package/dist/promise/timed.mjs +9 -10
- package/dist/query.mjs +15 -15
- package/dist/queue.mjs +103 -116
- package/dist/random.mjs +7 -7
- package/dist/result/index.d.mts +0 -3
- package/dist/result/index.mjs +1 -1
- package/dist/result/match.d.mts +0 -3
- package/dist/result/match.mjs +4 -4
- package/dist/result/misc.mjs +2 -2
- package/dist/result/work/flow.d.mts +0 -3
- package/dist/result/work/flow.mjs +5 -5
- package/dist/result/work/pipe.d.mts +0 -3
- package/dist/result/work/pipe.mjs +3 -3
- package/dist/string/case.mjs +20 -20
- package/dist/string/fuzzy.mjs +23 -18
- package/dist/string/index.mjs +7 -7
- package/dist/string/normalize.mjs +14 -14
- package/dist/string/template.mjs +2 -2
- package/dist/value/clone.mjs +10 -10
- package/dist/value/smush.mjs +2 -2
- package/package.json +13 -5
- package/src/array/difference.ts +0 -73
- package/src/array/exists.ts +0 -93
- package/src/array/filter.ts +0 -182
- package/src/array/find.ts +0 -183
- package/src/array/first.ts +0 -196
- package/src/array/flatten.ts +0 -20
- package/src/array/from.ts +0 -129
- package/src/array/get.ts +0 -131
- package/src/array/group-by.ts +0 -369
- package/src/array/index.ts +0 -26
- package/src/array/insert.ts +0 -52
- package/src/array/intersection.ts +0 -72
- package/src/array/last.ts +0 -194
- package/src/array/match.ts +0 -436
- package/src/array/move.ts +0 -315
- package/src/array/partition.ts +0 -91
- package/src/array/push.ts +0 -23
- package/src/array/reverse.ts +0 -33
- package/src/array/select.ts +0 -260
- package/src/array/single.ts +0 -93
- package/src/array/slice.ts +0 -335
- package/src/array/sort.ts +0 -607
- package/src/array/splice.ts +0 -106
- package/src/array/swap.ts +0 -322
- package/src/array/to-map.ts +0 -371
- package/src/array/to-record.ts +0 -351
- package/src/array/to-set.ts +0 -80
- package/src/array/toggle.ts +0 -79
- package/src/array/union.ts +0 -72
- package/src/array/unique.ts +0 -67
- package/src/array/update.ts +0 -75
- package/src/beacon.ts +0 -378
- package/src/color/constants.ts +0 -137
- package/src/color/index.ts +0 -83
- package/src/color/instance.ts +0 -162
- package/src/color/misc/alpha.ts +0 -56
- package/src/color/misc/format.ts +0 -29
- package/src/color/misc/get.ts +0 -210
- package/src/color/misc/is.ts +0 -221
- package/src/color/misc/state.ts +0 -243
- package/src/color/models.ts +0 -389
- package/src/color/space/hex.ts +0 -184
- package/src/color/space/hsl.ts +0 -174
- package/src/color/space/hwb.ts +0 -153
- package/src/color/space/rgb.ts +0 -240
- package/src/function/assert.ts +0 -174
- package/src/function/index.ts +0 -2
- package/src/function/limit.ts +0 -83
- package/src/function/memoize.ts +0 -202
- package/src/function/once.ts +0 -200
- package/src/function/retry.ts +0 -175
- package/src/function/work.ts +0 -1007
- package/src/herald.ts +0 -216
- package/src/index.ts +0 -68
- package/src/internal/array/callbacks.ts +0 -46
- package/src/internal/array/chunk.ts +0 -51
- package/src/internal/array/compact.ts +0 -56
- package/src/internal/array/find.ts +0 -200
- package/src/internal/array/group.ts +0 -42
- package/src/internal/array/index-of.ts +0 -182
- package/src/internal/array/insert.ts +0 -80
- package/src/internal/array/overlap.ts +0 -35
- package/src/internal/array/sets.ts +0 -74
- package/src/internal/array/shuffle.ts +0 -33
- package/src/internal/array/update.ts +0 -52
- package/src/internal/function/misc.ts +0 -8
- package/src/internal/function/timer.ts +0 -168
- package/src/internal/is.ts +0 -244
- package/src/internal/math/aggregate.ts +0 -164
- package/src/internal/math/misc.ts +0 -28
- package/src/internal/number.ts +0 -129
- package/src/internal/random.ts +0 -49
- package/src/internal/result.ts +0 -92
- package/src/internal/sized.ts +0 -25
- package/src/internal/string.ts +0 -128
- package/src/internal/subscription.ts +0 -264
- package/src/internal/value/compare.ts +0 -170
- package/src/internal/value/equal.ts +0 -465
- package/src/internal/value/get.ts +0 -57
- package/src/internal/value/handlers.ts +0 -95
- package/src/internal/value/has.ts +0 -118
- package/src/internal/value/misc.ts +0 -125
- package/src/internal/value/partial.ts +0 -46
- package/src/internal/value/set.ts +0 -132
- package/src/is.ts +0 -182
- package/src/logger.ts +0 -230
- package/src/math.ts +0 -281
- package/src/models.ts +0 -286
- package/src/number.ts +0 -1
- package/src/promise/delay.ts +0 -62
- package/src/promise/helpers.ts +0 -96
- package/src/promise/index.ts +0 -300
- package/src/promise/misc.ts +0 -96
- package/src/promise/models.ts +0 -195
- package/src/promise/timed.ts +0 -91
- package/src/query.ts +0 -147
- package/src/queue.ts +0 -814
- package/src/random.ts +0 -125
- package/src/result/index.ts +0 -105
- package/src/result/match.ts +0 -124
- package/src/result/misc.ts +0 -129
- package/src/result/models.ts +0 -83
- package/src/result/work/flow.ts +0 -488
- package/src/result/work/pipe.ts +0 -496
- package/src/sized/map.ts +0 -110
- package/src/sized/set.ts +0 -107
- package/src/string/case.ts +0 -236
- package/src/string/fuzzy.ts +0 -444
- package/src/string/index.ts +0 -199
- package/src/string/match.ts +0 -80
- package/src/string/normalize.ts +0 -205
- package/src/string/template.ts +0 -161
- package/src/value/clone.ts +0 -319
- package/src/value/collection.ts +0 -63
- package/src/value/diff.ts +0 -232
- package/src/value/freeze.ts +0 -322
- package/src/value/handle.ts +0 -3
- package/src/value/index.ts +0 -5
- package/src/value/merge.ts +0 -278
- package/src/value/omit.ts +0 -20
- package/src/value/pick.ts +0 -20
- package/src/value/shake.ts +0 -45
- package/src/value/smush.ts +0 -89
- package/src/value/transform.ts +0 -153
- package/src/value/unsmush.ts +0 -84
package/src/random.ts
DELETED
|
@@ -1,125 +0,0 @@
|
|
|
1
|
-
import {shuffle} from './internal/array/shuffle';
|
|
2
|
-
import {getRandomInteger} from './internal/random';
|
|
3
|
-
import {join} from './internal/string';
|
|
4
|
-
|
|
5
|
-
// #region Functions
|
|
6
|
-
|
|
7
|
-
/**
|
|
8
|
-
* Get a random boolean
|
|
9
|
-
*
|
|
10
|
-
* @returns Random boolean
|
|
11
|
-
*/
|
|
12
|
-
export function getRandomBoolean(): boolean {
|
|
13
|
-
return Math.random() > BOOLEAN_MODIFIER;
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
/**
|
|
17
|
-
* Get a random string of characters with a specified length
|
|
18
|
-
*
|
|
19
|
-
* @param length Length of random string
|
|
20
|
-
* @param selection String of characters to select from _(defaults to lowercase English alphabet)_
|
|
21
|
-
* @returns Random string of characters
|
|
22
|
-
*/
|
|
23
|
-
export function getRandomCharacters(length: number, selection?: string): string {
|
|
24
|
-
if (typeof length !== 'number' || length <= 0) {
|
|
25
|
-
return '';
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
const actual = typeof selection === 'string' && selection.length > 0 ? selection : ALPHABET;
|
|
29
|
-
|
|
30
|
-
let characters = '';
|
|
31
|
-
|
|
32
|
-
for (let index = 0; index < length; index += 1) {
|
|
33
|
-
characters += actual.charAt(getRandomInteger(0, actual.length - 1));
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
return characters;
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
/**
|
|
40
|
-
* Get a random hexadecimal color
|
|
41
|
-
*
|
|
42
|
-
* @param prefix Prefix the color with `#`? _(defaults to `false`)_
|
|
43
|
-
* @returns Random hexadecimal color string in the format `(#)RRGGBB`
|
|
44
|
-
*/
|
|
45
|
-
export function getRandomColor(prefix?: boolean): string {
|
|
46
|
-
return `${prefix === true ? '#' : ''}${join(Array.from({length: 6}, getRandomHex))}`;
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
/**
|
|
50
|
-
* Get a random hexadecimal character
|
|
51
|
-
*
|
|
52
|
-
* @returns Random hexadecimal character from `0-9` and `A-F`
|
|
53
|
-
*/
|
|
54
|
-
export function getRandomHex(): string {
|
|
55
|
-
return HEX_CHARACTERS[getRandomInteger(0, HEX_MAXIMUM)];
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
/**
|
|
59
|
-
* Get a random item from an array
|
|
60
|
-
*
|
|
61
|
-
* @param array Array to get a random item from
|
|
62
|
-
* @returns Random item from the array, or `undefined` if unable to retrieve one
|
|
63
|
-
*/
|
|
64
|
-
export function getRandomItem<Value>(array: Value[]): Value | undefined {
|
|
65
|
-
if (!Array.isArray(array) || array.length === 0) {
|
|
66
|
-
return;
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
return array.length === 1 ? array[0] : array[getRandomInteger(0, array.length - 1)];
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
/**
|
|
73
|
-
* Get an amount of random items from an array
|
|
74
|
-
*
|
|
75
|
-
* @param array Array to get random items from
|
|
76
|
-
* @param amount Amount of items to return
|
|
77
|
-
* @returns Array of random items
|
|
78
|
-
*/
|
|
79
|
-
export function getRandomItems<Value>(array: Value[], amount: number): Value[];
|
|
80
|
-
|
|
81
|
-
/**
|
|
82
|
-
* Get a shuffled array
|
|
83
|
-
*
|
|
84
|
-
* @param array Array to get random items from
|
|
85
|
-
* @returns Shuffled version of the original array
|
|
86
|
-
*/
|
|
87
|
-
export function getRandomItems<Value>(array: Value[]): Value[];
|
|
88
|
-
|
|
89
|
-
export function getRandomItems<Value>(array: Value[], amount?: number): Value[] {
|
|
90
|
-
if (!Array.isArray(array) || array.length === 0 || amount === 0) {
|
|
91
|
-
return [];
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
if (array.length < 2) {
|
|
95
|
-
return array;
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
if (amount === 1) {
|
|
99
|
-
return [array[getRandomInteger(0, array.length - 1)]];
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
return typeof amount !== 'number' || amount <= 0 || amount >= array.length
|
|
103
|
-
? shuffle(array)
|
|
104
|
-
: shuffle(array).slice(0, amount);
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
// #endregion
|
|
108
|
-
|
|
109
|
-
// #region Variables
|
|
110
|
-
|
|
111
|
-
const ALPHABET = 'abcdefghijklmnopqrstuvwxyz';
|
|
112
|
-
|
|
113
|
-
const BOOLEAN_MODIFIER = 0.5;
|
|
114
|
-
|
|
115
|
-
const HEX_CHARACTERS = '0123456789ABCDEF';
|
|
116
|
-
|
|
117
|
-
const HEX_MAXIMUM = 15;
|
|
118
|
-
|
|
119
|
-
// #endregion
|
|
120
|
-
|
|
121
|
-
// #region Exports
|
|
122
|
-
|
|
123
|
-
export {getRandomFloatingNumber as getRandomFloat, getRandomInteger} from './internal/random';
|
|
124
|
-
|
|
125
|
-
// #endregion
|
package/src/result/index.ts
DELETED
|
@@ -1,105 +0,0 @@
|
|
|
1
|
-
import {getError, ok} from './misc';
|
|
2
|
-
import type {ExtendedErr, ExtendedResult, Result} from './models';
|
|
3
|
-
|
|
4
|
-
// #region Functions
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* Executes a _Promise_, catching any errors, and returns a result
|
|
8
|
-
*
|
|
9
|
-
* _Available as `asyncAttempt` and `attempt.async`_
|
|
10
|
-
*
|
|
11
|
-
* @param promise _Promise_ to execute
|
|
12
|
-
* @param error Error value
|
|
13
|
-
* @returns Callback result
|
|
14
|
-
*/
|
|
15
|
-
export async function asyncAttempt<Value, E>(
|
|
16
|
-
promise: Promise<Value>,
|
|
17
|
-
error: E,
|
|
18
|
-
): Promise<ExtendedResult<Awaited<Value>, E>>;
|
|
19
|
-
|
|
20
|
-
/**
|
|
21
|
-
* Executes a callback asynchronously, catching any errors, and returns a result
|
|
22
|
-
*
|
|
23
|
-
* _Available as `asyncAttempt` and `attempt.async`_
|
|
24
|
-
*
|
|
25
|
-
* @param callback Callback to execute
|
|
26
|
-
* @param error Error value
|
|
27
|
-
* @returns Callback result
|
|
28
|
-
*/
|
|
29
|
-
export async function asyncAttempt<Value, E>(
|
|
30
|
-
callback: () => Promise<Value>,
|
|
31
|
-
error: E,
|
|
32
|
-
): Promise<ExtendedResult<Awaited<Value>, E>>;
|
|
33
|
-
|
|
34
|
-
/**
|
|
35
|
-
* Executes a _Promise_, catching any errors, and returns a result
|
|
36
|
-
*
|
|
37
|
-
* _Available as `asyncAttempt` and `attempt.async`_
|
|
38
|
-
*
|
|
39
|
-
* @param promise _Promise_ to execute
|
|
40
|
-
* @returns Callback result
|
|
41
|
-
*/
|
|
42
|
-
export async function asyncAttempt<Value>(promise: Promise<Value>): Promise<Result<Awaited<Value>>>;
|
|
43
|
-
|
|
44
|
-
/**
|
|
45
|
-
* Executes a callback asynchronously, catching any errors, and returns a result
|
|
46
|
-
*
|
|
47
|
-
* _Available as `asyncAttempt` and `attempt.async`_
|
|
48
|
-
*
|
|
49
|
-
* @param callback Callback to execute
|
|
50
|
-
* @returns Callback result
|
|
51
|
-
*/
|
|
52
|
-
export async function asyncAttempt<Value>(
|
|
53
|
-
callback: () => Promise<Value>,
|
|
54
|
-
): Promise<Result<Awaited<Value>>>;
|
|
55
|
-
|
|
56
|
-
export async function asyncAttempt<Value, E>(
|
|
57
|
-
value: Promise<Value> | (() => Promise<Value>),
|
|
58
|
-
err?: E,
|
|
59
|
-
): Promise<unknown> {
|
|
60
|
-
try {
|
|
61
|
-
let result = typeof value === 'function' ? value() : await value;
|
|
62
|
-
|
|
63
|
-
if (result instanceof Promise) {
|
|
64
|
-
result = await result;
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
return ok(result);
|
|
68
|
-
} catch (thrown) {
|
|
69
|
-
return getError((err ?? thrown) as E, err == null ? undefined : (thrown as Error));
|
|
70
|
-
}
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
/**
|
|
74
|
-
* Executes a callback, catching any errors, and returns a result
|
|
75
|
-
*
|
|
76
|
-
* @param callback Callback to execute
|
|
77
|
-
* @param error Error value
|
|
78
|
-
* @returns Callback result
|
|
79
|
-
*/
|
|
80
|
-
export function attempt<Value, E>(callback: () => Value, error: E): ExtendedResult<Value, E>;
|
|
81
|
-
|
|
82
|
-
/**
|
|
83
|
-
* Executes a callback, catching any errors, and returns a result
|
|
84
|
-
*
|
|
85
|
-
* @param callback Callback to execute
|
|
86
|
-
* @returns Callback result
|
|
87
|
-
*/
|
|
88
|
-
export function attempt<Value>(callback: () => Value): Result<Value, Error>;
|
|
89
|
-
|
|
90
|
-
export function attempt<Value, E>(
|
|
91
|
-
callback: () => Value,
|
|
92
|
-
err?: E,
|
|
93
|
-
): ExtendedErr<E> | Result<Value, E> {
|
|
94
|
-
try {
|
|
95
|
-
const value = callback();
|
|
96
|
-
|
|
97
|
-
return ok(value);
|
|
98
|
-
} catch (thrown) {
|
|
99
|
-
return getError((err ?? thrown) as E, err == null ? undefined : (thrown as Error));
|
|
100
|
-
}
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
attempt.async = asyncAttempt;
|
|
104
|
-
|
|
105
|
-
// #endregion
|
package/src/result/match.ts
DELETED
|
@@ -1,124 +0,0 @@
|
|
|
1
|
-
import {isOk, isResult} from '../internal/result';
|
|
2
|
-
import type {AnyResult, ExtendedErr, ResultMatch} from './models';
|
|
3
|
-
|
|
4
|
-
// #region Functions
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* Handles a _Result_ with match callbacks
|
|
8
|
-
*
|
|
9
|
-
* _Available as `asyncMatchResult` and `matchResult.async`_
|
|
10
|
-
*
|
|
11
|
-
* @param result _Result_ to handle
|
|
12
|
-
* @param handler Match callbacks
|
|
13
|
-
* @returns Matched value-_Promise_
|
|
14
|
-
*/
|
|
15
|
-
export async function asyncMatchResult<Value, Returned, E = Error>(
|
|
16
|
-
result: AnyResult<Value, E> | Promise<AnyResult<Value, E>> | (() => Promise<AnyResult<Value, E>>),
|
|
17
|
-
handler: ResultMatch<Value, Returned, E>,
|
|
18
|
-
): Promise<Returned>;
|
|
19
|
-
|
|
20
|
-
/**
|
|
21
|
-
* Handles a _Result_ with match callbacks
|
|
22
|
-
*
|
|
23
|
-
* _Available as `asyncMatchResult` and `matchResult.async`_
|
|
24
|
-
*
|
|
25
|
-
* @param result _Result_ to handle
|
|
26
|
-
* @param ok Ok callback
|
|
27
|
-
* @param error Error callback
|
|
28
|
-
* @returns Matched value-_Promise_
|
|
29
|
-
*/
|
|
30
|
-
export async function asyncMatchResult<Value, Returned, E = Error>(
|
|
31
|
-
result: AnyResult<Value, E> | Promise<AnyResult<Value, E>> | (() => Promise<AnyResult<Value, E>>),
|
|
32
|
-
ok: ResultMatch<Value, Returned, E>['ok'],
|
|
33
|
-
error: ResultMatch<Value, Returned, E>['error'],
|
|
34
|
-
): Promise<Returned>;
|
|
35
|
-
|
|
36
|
-
export async function asyncMatchResult<Value, Returned, E = Error>(
|
|
37
|
-
result: AnyResult<Value, E> | Promise<AnyResult<Value, E>> | (() => Promise<AnyResult<Value, E>>),
|
|
38
|
-
first: ResultMatch<Value, Returned, E> | ResultMatch<Value, Returned, E>['ok'],
|
|
39
|
-
error?: ResultMatch<Value, Returned, E>['error'],
|
|
40
|
-
): Promise<Returned> {
|
|
41
|
-
let value: AnyResult<Value, E>;
|
|
42
|
-
|
|
43
|
-
if (typeof result === 'function') {
|
|
44
|
-
value = await result();
|
|
45
|
-
} else if (result instanceof Promise) {
|
|
46
|
-
value = await result;
|
|
47
|
-
} else {
|
|
48
|
-
value = result;
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
if (!isResult(value)) {
|
|
52
|
-
throw new Error(MESSAGE_RESULT);
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
const hasObj = typeof first === 'object' && first !== null;
|
|
56
|
-
|
|
57
|
-
const okHandler = hasObj ? first.ok : first;
|
|
58
|
-
const errorHandler = hasObj ? first.error : error;
|
|
59
|
-
|
|
60
|
-
if (isOk(value)) {
|
|
61
|
-
return okHandler(value.value);
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
return errorHandler!(value.error, (value as ExtendedErr<E>).original);
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
/**
|
|
68
|
-
* Handles a _Result_ with match callbacks
|
|
69
|
-
*
|
|
70
|
-
* @param result _Result_ to handle
|
|
71
|
-
* @param handler Match callbacks
|
|
72
|
-
* @returns Matched value
|
|
73
|
-
*/
|
|
74
|
-
export function matchResult<Value, Returned, E = Error>(
|
|
75
|
-
result: AnyResult<Value, E> | (() => AnyResult<Value, E>),
|
|
76
|
-
handler: ResultMatch<Value, Returned, E>,
|
|
77
|
-
): Returned;
|
|
78
|
-
|
|
79
|
-
/**
|
|
80
|
-
* Handles a _Result_ with match callbacks
|
|
81
|
-
*
|
|
82
|
-
* @param result _Result_ to handle
|
|
83
|
-
* @param ok Ok callback
|
|
84
|
-
* @param error Error callback
|
|
85
|
-
* @returns Matched value
|
|
86
|
-
*/
|
|
87
|
-
export function matchResult<Value, Returned, E = Error>(
|
|
88
|
-
result: AnyResult<Value, E> | (() => AnyResult<Value, E>),
|
|
89
|
-
ok: ResultMatch<Value, Returned, E>['ok'],
|
|
90
|
-
error: ResultMatch<Value, Returned, E>['error'],
|
|
91
|
-
): Returned;
|
|
92
|
-
|
|
93
|
-
export function matchResult<Value, Returned, E = Error>(
|
|
94
|
-
result: AnyResult<Value, E> | (() => AnyResult<Value, E>),
|
|
95
|
-
first: ResultMatch<Value, Returned, E> | ResultMatch<Value, Returned, E>['ok'],
|
|
96
|
-
error?: ResultMatch<Value, Returned, E>['error'],
|
|
97
|
-
): Returned {
|
|
98
|
-
const value = typeof result === 'function' ? result() : result;
|
|
99
|
-
|
|
100
|
-
if (!isResult(value)) {
|
|
101
|
-
throw new Error(MESSAGE_RESULT);
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
const hasObj = typeof first === 'object' && first !== null;
|
|
105
|
-
|
|
106
|
-
const okHandler = hasObj ? first.ok : first;
|
|
107
|
-
const errorHandler = hasObj ? first.error : error;
|
|
108
|
-
|
|
109
|
-
if (isOk(value)) {
|
|
110
|
-
return okHandler(value.value);
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
return errorHandler!(value.error, (value as ExtendedErr<E>).original);
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
matchResult.async = asyncMatchResult;
|
|
117
|
-
|
|
118
|
-
// #endregion
|
|
119
|
-
|
|
120
|
-
// #region Variables
|
|
121
|
-
|
|
122
|
-
const MESSAGE_RESULT = '`result.match` expected a Result or a function that returns a Result';
|
|
123
|
-
|
|
124
|
-
// #endregion
|
package/src/result/misc.ts
DELETED
|
@@ -1,129 +0,0 @@
|
|
|
1
|
-
import {isOk, isResult} from '../internal/result';
|
|
2
|
-
import type {AnyResult, Err, ExtendedErr, Ok, Result} from './models';
|
|
3
|
-
|
|
4
|
-
// #region Functions
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* Creates an extended error result
|
|
8
|
-
*
|
|
9
|
-
* @param value Error value
|
|
10
|
-
* @param original Original error
|
|
11
|
-
* @returns Error result
|
|
12
|
-
*/
|
|
13
|
-
export function error<E>(value: E, original: Error): ExtendedErr<E>;
|
|
14
|
-
|
|
15
|
-
/**
|
|
16
|
-
* Creates an error result
|
|
17
|
-
*
|
|
18
|
-
* @param value Error value
|
|
19
|
-
* @returns Error result
|
|
20
|
-
*/
|
|
21
|
-
export function error<E>(value: E): Err<E>;
|
|
22
|
-
|
|
23
|
-
export function error<E>(value: E, original?: Error): Err<E> | ExtendedErr<E> {
|
|
24
|
-
return getError(value, original);
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
export function getError<E>(value: E, original?: Error): Err<E> | ExtendedErr<E> {
|
|
28
|
-
const errorResult: Err<E> | ExtendedErr<E> = {
|
|
29
|
-
error: value,
|
|
30
|
-
ok: false,
|
|
31
|
-
};
|
|
32
|
-
|
|
33
|
-
if (original instanceof Error) {
|
|
34
|
-
(errorResult as ExtendedErr<E>).original = original;
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
return errorResult;
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
/**
|
|
41
|
-
* Creates an ok result
|
|
42
|
-
*
|
|
43
|
-
* @param value Value
|
|
44
|
-
* @returns Ok result
|
|
45
|
-
*/
|
|
46
|
-
export function ok<Value>(value: Value): Ok<Value> {
|
|
47
|
-
return {
|
|
48
|
-
ok: true,
|
|
49
|
-
value,
|
|
50
|
-
};
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
/**
|
|
54
|
-
* Converts a result to a _Promise_
|
|
55
|
-
*
|
|
56
|
-
* Resolves if ok, rejects for error
|
|
57
|
-
*
|
|
58
|
-
* @param callback Callback to get the result
|
|
59
|
-
* @returns Promised result
|
|
60
|
-
*/
|
|
61
|
-
export async function toPromise<Value, E = Error>(
|
|
62
|
-
callback: () => AnyResult<Value, E>,
|
|
63
|
-
): Promise<Value>;
|
|
64
|
-
|
|
65
|
-
/**
|
|
66
|
-
* Converts a result to a _Promise_
|
|
67
|
-
*
|
|
68
|
-
* Resolves if ok, rejects for error
|
|
69
|
-
*
|
|
70
|
-
* @param result _Result_ to convert
|
|
71
|
-
* @returns Promised result
|
|
72
|
-
*/
|
|
73
|
-
export async function toPromise<Value, E = Error>(result: AnyResult<Value, E>): Promise<Value>;
|
|
74
|
-
|
|
75
|
-
/**
|
|
76
|
-
* Converts a _Result_ to a _Promise_
|
|
77
|
-
*
|
|
78
|
-
* Resolves if ok, rejects for error
|
|
79
|
-
*
|
|
80
|
-
* @param result _Result_ to convert
|
|
81
|
-
* @returns Promised result
|
|
82
|
-
*/
|
|
83
|
-
export async function toPromise<Value, E = Error>(
|
|
84
|
-
result: AnyResult<Value, E> | (() => AnyResult<Value, E>),
|
|
85
|
-
): Promise<Value> {
|
|
86
|
-
const actual = typeof result === 'function' ? result() : result;
|
|
87
|
-
|
|
88
|
-
if (!isResult(actual)) {
|
|
89
|
-
return Promise.reject(new Error(MESSAGE_PROMISE_RESULT));
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
return isOk(actual) ? Promise.resolve(actual.value) : Promise.reject(actual.error);
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
/**
|
|
96
|
-
* Gets the value of an ok result _(or a default value)_
|
|
97
|
-
*
|
|
98
|
-
* @param value _Result_ to unwrap
|
|
99
|
-
* @param defaultValue Default value
|
|
100
|
-
* @returns Value of the _Result_ _(or the default value)_
|
|
101
|
-
*/
|
|
102
|
-
export function unwrap<Value, E = Error>(value: Result<Value, E>, defaultValue: Value): Value;
|
|
103
|
-
|
|
104
|
-
/**
|
|
105
|
-
* Gets the value of an ok result _(or a default value)_
|
|
106
|
-
*
|
|
107
|
-
* @param value _Result_ to unwrap
|
|
108
|
-
* @param defaultValue Default value
|
|
109
|
-
* @returns Value of the _Result_ _(or the default value)_
|
|
110
|
-
*/
|
|
111
|
-
export function unwrap(value: unknown, defaultValue: unknown): unknown;
|
|
112
|
-
|
|
113
|
-
export function unwrap(value: unknown, defaultValue: unknown): unknown {
|
|
114
|
-
return isOk(value) ? value.value : defaultValue;
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
// #endregion
|
|
118
|
-
|
|
119
|
-
// #region Variables
|
|
120
|
-
|
|
121
|
-
const MESSAGE_PROMISE_RESULT = 'toPromise expected to receive a Result';
|
|
122
|
-
|
|
123
|
-
// #endregion
|
|
124
|
-
|
|
125
|
-
// #region Exports
|
|
126
|
-
|
|
127
|
-
export {isError, isOk, isResult} from '../internal/result';
|
|
128
|
-
|
|
129
|
-
// #endregion
|
package/src/result/models.ts
DELETED
|
@@ -1,83 +0,0 @@
|
|
|
1
|
-
import type {GenericCallback} from '../models';
|
|
2
|
-
|
|
3
|
-
// #region Types
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* An unknown result
|
|
7
|
-
*/
|
|
8
|
-
export type AnyResult<Value, E> = Err<E> | ExtendedErr<E> | Ok<Value>;
|
|
9
|
-
|
|
10
|
-
/**
|
|
11
|
-
* An error result
|
|
12
|
-
*/
|
|
13
|
-
export type Err<Error> = {
|
|
14
|
-
ok: false;
|
|
15
|
-
error: Error;
|
|
16
|
-
};
|
|
17
|
-
|
|
18
|
-
/**
|
|
19
|
-
* An extended error result
|
|
20
|
-
*/
|
|
21
|
-
export type ExtendedErr<E> = Err<E> & {
|
|
22
|
-
original: Error;
|
|
23
|
-
};
|
|
24
|
-
|
|
25
|
-
/**
|
|
26
|
-
* An extended, unknown result
|
|
27
|
-
*/
|
|
28
|
-
export type ExtendedResult<Value, E> = ExtendedErr<E> | Ok<Value>;
|
|
29
|
-
|
|
30
|
-
/**
|
|
31
|
-
* A successful result
|
|
32
|
-
*/
|
|
33
|
-
export type Ok<Value> = {
|
|
34
|
-
ok: true;
|
|
35
|
-
value: Value;
|
|
36
|
-
};
|
|
37
|
-
|
|
38
|
-
/**
|
|
39
|
-
* An unknown result
|
|
40
|
-
*/
|
|
41
|
-
export type Result<Value, E = Error> = Err<E> | Ok<Value>;
|
|
42
|
-
|
|
43
|
-
/**
|
|
44
|
-
* Match callbacks for a result
|
|
45
|
-
*/
|
|
46
|
-
export type ResultMatch<Value, Returned, E = Error> = {
|
|
47
|
-
/**
|
|
48
|
-
* Callback for error result
|
|
49
|
-
*
|
|
50
|
-
* @param error Error value
|
|
51
|
-
* @param original Original error, if available
|
|
52
|
-
* @returns Value to return
|
|
53
|
-
*/
|
|
54
|
-
error: (error: E, original?: Error) => Returned;
|
|
55
|
-
/**
|
|
56
|
-
* Callback for ok result
|
|
57
|
-
*
|
|
58
|
-
* @param value Ok value
|
|
59
|
-
* @returns Value to return
|
|
60
|
-
*/
|
|
61
|
-
ok: (value: Value) => Returned;
|
|
62
|
-
};
|
|
63
|
-
|
|
64
|
-
/**
|
|
65
|
-
* Unwrap a result value
|
|
66
|
-
*/
|
|
67
|
-
type UnwrapResult<Original extends Result<unknown, unknown>> =
|
|
68
|
-
Original extends Ok<infer Value> ? Value : never;
|
|
69
|
-
|
|
70
|
-
/**
|
|
71
|
-
* Unwrap any value
|
|
72
|
-
*/
|
|
73
|
-
export type UnwrapValue<Original> = Original extends GenericCallback
|
|
74
|
-
? ReturnType<Original> extends Result<unknown, unknown>
|
|
75
|
-
? UnwrapResult<ReturnType<Original>>
|
|
76
|
-
: ReturnType<Original>
|
|
77
|
-
: Original extends Result<unknown, unknown>
|
|
78
|
-
? UnwrapResult<Original>
|
|
79
|
-
: Original extends Promise<infer Value>
|
|
80
|
-
? Awaited<Value>
|
|
81
|
-
: Original;
|
|
82
|
-
|
|
83
|
-
// #endregion
|