@oscarpalmer/atoms 0.179.0 → 0.180.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.
Files changed (100) hide show
  1. package/dist/array/filter.d.mts +36 -28
  2. package/dist/array/filter.mjs +5 -5
  3. package/dist/array/first.d.mts +13 -5
  4. package/dist/array/first.mjs +1 -1
  5. package/dist/array/group-by.d.mts +13 -1
  6. package/dist/array/group-by.mjs +1 -1
  7. package/dist/array/last.d.mts +9 -1
  8. package/dist/array/last.mjs +1 -1
  9. package/dist/array/move.d.mts +9 -1
  10. package/dist/array/move.mjs +3 -1
  11. package/dist/array/reverse.d.mts +5 -0
  12. package/dist/array/reverse.mjs +5 -0
  13. package/dist/array/select.d.mts +2 -2
  14. package/dist/array/sort.d.mts +23 -9
  15. package/dist/array/sort.mjs +22 -22
  16. package/dist/array/swap.d.mts +2 -0
  17. package/dist/array/swap.mjs +2 -0
  18. package/dist/array/to-map.d.mts +13 -1
  19. package/dist/array/to-map.mjs +1 -1
  20. package/dist/array/to-record.d.mts +13 -1
  21. package/dist/array/to-record.mjs +1 -1
  22. package/dist/function/assert.d.mts +9 -1
  23. package/dist/function/assert.mjs +9 -1
  24. package/dist/function/limit.d.mts +5 -1
  25. package/dist/function/limit.mjs +5 -1
  26. package/dist/function/once.d.mts +3 -1
  27. package/dist/function/once.mjs +3 -1
  28. package/dist/function/retry.d.mts +4 -0
  29. package/dist/function/retry.mjs +2 -0
  30. package/dist/function/work.d.mts +49 -1
  31. package/dist/function/work.mjs +1 -1
  32. package/dist/index.d.mts +415 -242
  33. package/dist/index.mjs +224 -167
  34. package/dist/internal/array/index-of.mjs +1 -1
  35. package/dist/internal/function/timer.mjs +3 -1
  36. package/dist/internal/value/compare.d.mts +13 -9
  37. package/dist/internal/value/compare.mjs +13 -9
  38. package/dist/internal/value/equal.d.mts +29 -15
  39. package/dist/internal/value/equal.mjs +41 -35
  40. package/dist/internal/value/handlers.d.mts +4 -4
  41. package/dist/internal/value/handlers.mjs +19 -11
  42. package/dist/internal/value/has.d.mts +9 -8
  43. package/dist/internal/value/has.mjs +3 -3
  44. package/dist/internal/value/misc.d.mts +4 -8
  45. package/dist/internal/value/misc.mjs +6 -17
  46. package/dist/promise/index.d.mts +11 -1
  47. package/dist/promise/index.mjs +1 -1
  48. package/dist/result/index.d.mts +9 -1
  49. package/dist/result/index.mjs +1 -1
  50. package/dist/result/match.d.mts +5 -1
  51. package/dist/result/match.mjs +1 -1
  52. package/dist/result/misc.d.mts +3 -3
  53. package/dist/result/work/flow.d.mts +49 -1
  54. package/dist/result/work/flow.mjs +1 -1
  55. package/dist/result/work/pipe.d.mts +67 -155
  56. package/dist/result/work/pipe.mjs +3 -3
  57. package/dist/string/fuzzy.d.mts +11 -1
  58. package/dist/string/fuzzy.mjs +22 -6
  59. package/dist/string/template.d.mts +3 -1
  60. package/dist/string/template.mjs +3 -1
  61. package/dist/value/clone.d.mts +13 -9
  62. package/dist/value/clone.mjs +21 -17
  63. package/dist/value/merge.d.mts +9 -7
  64. package/dist/value/merge.mjs +7 -5
  65. package/package.json +3 -3
  66. package/plugin/helpers.js +2 -2
  67. package/src/array/filter.ts +44 -36
  68. package/src/array/first.ts +18 -9
  69. package/src/array/group-by.ts +22 -10
  70. package/src/array/last.ts +17 -5
  71. package/src/array/move.ts +18 -5
  72. package/src/array/reverse.ts +5 -0
  73. package/src/array/select.ts +2 -2
  74. package/src/array/sort.ts +110 -86
  75. package/src/array/swap.ts +2 -0
  76. package/src/array/to-map.ts +22 -10
  77. package/src/array/to-record.ts +22 -10
  78. package/src/function/assert.ts +12 -4
  79. package/src/function/limit.ts +6 -2
  80. package/src/function/once.ts +3 -1
  81. package/src/function/retry.ts +8 -2
  82. package/src/function/work.ts +92 -26
  83. package/src/internal/array/index-of.ts +1 -1
  84. package/src/internal/function/timer.ts +4 -2
  85. package/src/internal/string.ts +2 -0
  86. package/src/internal/value/compare.ts +14 -11
  87. package/src/internal/value/equal.ts +79 -67
  88. package/src/internal/value/handlers.ts +19 -11
  89. package/src/internal/value/has.ts +16 -16
  90. package/src/internal/value/misc.ts +10 -8
  91. package/src/promise/index.ts +14 -4
  92. package/src/result/index.ts +15 -5
  93. package/src/result/match.ts +7 -3
  94. package/src/result/misc.ts +3 -3
  95. package/src/result/work/flow.ts +68 -13
  96. package/src/result/work/pipe.ts +97 -392
  97. package/src/string/fuzzy.ts +34 -8
  98. package/src/string/template.ts +3 -1
  99. package/src/value/clone.ts +25 -22
  100. package/src/value/merge.ts +14 -12
@@ -1,6 +1,5 @@
1
- import {isPrimitive} from '../is';
2
1
  import type {ArrayOrPlainObject, Constructor, PlainObject, TypedArray} from '../../models';
3
- import {isPlainObject, isTypedArray} from '../is';
2
+ import {isPlainObject, isPrimitive, isTypedArray} from '../is';
4
3
  import {getCompareHandlers} from './handlers';
5
4
 
6
5
  // #region Types
@@ -24,6 +23,15 @@ export type EqualOptions = {
24
23
  };
25
24
 
26
25
  type Equalizer = {
26
+ /**
27
+ * Are two strings equal?
28
+ * @param first First string
29
+ * @param second Second string
30
+ * @param ignoreCase If `true`, comparison will be case-insensitive
31
+ * @returns `true` if the strings are equal, otherwise `false`
32
+ */
33
+ (first: string, second: string, ignoreCase?: boolean): boolean;
34
+
27
35
  /**
28
36
  * Are two values equal?
29
37
  * @param first First value
@@ -32,21 +40,21 @@ type Equalizer = {
32
40
  */
33
41
  (first: unknown, second: unknown): boolean;
34
42
 
43
+ /**
44
+ * Deregister a equality comparison handler for a specific class
45
+ * @param constructor Class constructor
46
+ */
47
+ deregister: <Instance>(constructor: Constructor<Instance>) => void;
48
+
35
49
  /**
36
50
  * Register a equality comparison function for a specific class
37
51
  * @param constructor Class constructor
38
- * @param fn Comparison function
52
+ * @param handler Comparison function
39
53
  */
40
54
  register: <Instance>(
41
55
  constructor: Constructor<Instance>,
42
56
  handler: (first: Instance, second: Instance) => boolean,
43
57
  ) => void;
44
-
45
- /**
46
- * Unregister a equality comparison handler for a specific class
47
- * @param constructor Class constructor
48
- */
49
- unregister: <Instance>(constructor: Constructor<Instance>) => void;
50
58
  };
51
59
 
52
60
  type Options = {
@@ -65,6 +73,35 @@ type OptionsKeys<Values> = {
65
73
 
66
74
  // #region Functions
67
75
 
76
+ /**
77
+ * Deregister a equality comparison handler for a specific class
78
+ *
79
+ * Available as `deregisterEqualizer` and `equal.deregister`
80
+ * @param constructor Class constructor
81
+ */
82
+ export function deregisterEqualizer<Instance>(constructor: Constructor<Instance>): void {
83
+ equal.handlers.deregister(constructor);
84
+ }
85
+
86
+ function filterKey(key: string | symbol, options: Options): boolean {
87
+ if (typeof key !== 'string') {
88
+ return true;
89
+ }
90
+
91
+ if (
92
+ options.ignoreExpressions.enabled &&
93
+ options.ignoreExpressions.values.some(expression => expression.test(key))
94
+ ) {
95
+ return false;
96
+ }
97
+
98
+ if (options.ignoreKeys.enabled && options.ignoreKeys.values.has(key)) {
99
+ return false;
100
+ }
101
+
102
+ return true;
103
+ }
104
+
68
105
  /**
69
106
  * Are two strings equal?
70
107
  * @param first First string
@@ -91,12 +128,10 @@ equal.handlers = getCompareHandlers<boolean>(equal, {
91
128
  callback: Object.is,
92
129
  });
93
130
 
131
+ equal.deregister = deregisterEqualizer;
94
132
  equal.initialize = initializeEqualizer;
95
-
96
133
  equal.register = registerEqualizer;
97
134
 
98
- equal.unregister = unregisterEqualizer;
99
-
100
135
  function equalArray(first: unknown[], second: unknown[], options: Options): boolean {
101
136
  const {length} = first;
102
137
 
@@ -322,61 +357,6 @@ function equalValue(first: unknown, second: unknown, options: Options): boolean
322
357
  }
323
358
  }
324
359
 
325
- /**
326
- * Create an equalizer with predefined options
327
- * @param options Comparison options
328
- * @returns Equalizer function
329
- */
330
- function initializeEqualizer(options?: EqualOptions): Equalizer {
331
- const actual = getEqualOptions(options);
332
-
333
- const equalizer = (first: unknown, second: unknown): boolean => equalValue(first, second, actual);
334
-
335
- equalizer.register = registerEqualizer;
336
- equalizer.unregister = unregisterEqualizer;
337
-
338
- return equalizer;
339
- }
340
-
341
- /**
342
- * Register a equality comparison function for a specific class
343
- * @param constructor Class constructor
344
- * @param fn Comparison function
345
- */
346
- function registerEqualizer<Instance>(
347
- constructor: Constructor<Instance>,
348
- handler: (first: Instance, second: Instance) => boolean,
349
- ): void {
350
- equal.handlers.register(constructor, handler);
351
- }
352
-
353
- /**
354
- * Unregister a equality comparison handler for a specific class
355
- * @param constructor Class constructor
356
- */
357
- function unregisterEqualizer<Instance>(constructor: Constructor<Instance>): void {
358
- equal.handlers.unregister(constructor);
359
- }
360
-
361
- function filterKey(key: string | symbol, options: Options): boolean {
362
- if (typeof key !== 'string') {
363
- return true;
364
- }
365
-
366
- if (
367
- options.ignoreExpressions.enabled &&
368
- options.ignoreExpressions.values.some(expression => expression.test(key))
369
- ) {
370
- return false;
371
- }
372
-
373
- if (options.ignoreKeys.enabled && options.ignoreKeys.values.has(key)) {
374
- return false;
375
- }
376
-
377
- return true;
378
- }
379
-
380
360
  function getEqualOptions(input?: boolean | EqualOptions): Options {
381
361
  const options: Options = {
382
362
  ignoreCase: false,
@@ -422,6 +402,38 @@ function getEqualOptions(input?: boolean | EqualOptions): Options {
422
402
  return options;
423
403
  }
424
404
 
405
+ /**
406
+ * Create an equalizer with predefined options
407
+ *
408
+ * Available as `initializeEqualizer` and `equal.initialize`
409
+ * @param options Comparison options
410
+ * @returns Equalizer function
411
+ */
412
+ export function initializeEqualizer(options?: EqualOptions): Equalizer {
413
+ const actual = getEqualOptions(options);
414
+
415
+ const equalizer = (first: unknown, second: unknown): boolean => equalValue(first, second, actual);
416
+
417
+ equalizer.deregister = deregisterEqualizer;
418
+ equalizer.register = registerEqualizer;
419
+
420
+ return equalizer;
421
+ }
422
+
423
+ /**
424
+ * Register a equality comparison function for a specific class
425
+ *
426
+ * Available as `registerEqualizer` and `equal.register`
427
+ * @param constructor Class constructor
428
+ * @param handler Comparison function
429
+ */
430
+ export function registerEqualizer<Instance>(
431
+ constructor: Constructor<Instance>,
432
+ handler: (first: Instance, second: Instance) => boolean,
433
+ ): void {
434
+ equal.handlers.register(constructor, handler);
435
+ }
436
+
425
437
  // #endregion
426
438
 
427
439
  // #region Variables
@@ -7,13 +7,14 @@ type Options = {
7
7
  };
8
8
 
9
9
  export function getCompareHandlers<Value>(owner: GenericCallback, options: Options) {
10
- const {get, register, unregister} = getHandlers(owner, options);
10
+ const handlers = getHandlers(owner, options);
11
11
 
12
12
  return {
13
- register,
14
- unregister,
13
+ deregister(constructor: Constructor) {
14
+ handlers.deregister(constructor);
15
+ },
15
16
  handle(first: unknown, second: unknown, ...parameters: unknown[]): Value {
16
- const handler = get(first, second);
17
+ const handler = handlers.get(first, second);
17
18
 
18
19
  if (handler == null) {
19
20
  return options.callback(first, second, ...parameters);
@@ -23,6 +24,9 @@ export function getCompareHandlers<Value>(owner: GenericCallback, options: Optio
23
24
  ? handler(first, second)
24
25
  : (first as any)[handler](second);
25
26
  },
27
+ register(constructor: Constructor, handler?: string | GenericCallback) {
28
+ handlers.register(constructor, handler);
29
+ },
26
30
  };
27
31
  }
28
32
 
@@ -30,6 +34,9 @@ function getHandlers(owner: GenericCallback, options: Options) {
30
34
  const handlers = new WeakMap<Constructor, string | GenericCallback>();
31
35
 
32
36
  return {
37
+ deregister(constructor: Constructor) {
38
+ handlers.delete(constructor);
39
+ },
33
40
  get(first: unknown, second: unknown): string | GenericCallback | undefined {
34
41
  if (
35
42
  isConstructable(first) &&
@@ -58,20 +65,18 @@ function getHandlers(owner: GenericCallback, options: Options) {
58
65
  handlers.set(constructor, actual);
59
66
  }
60
67
  },
61
- unregister(constructor: Constructor) {
62
- handlers.delete(constructor);
63
- },
64
68
  };
65
69
  }
66
70
 
67
71
  export function getSelfHandlers(owner: GenericCallback, options: Options) {
68
- const {get, register, unregister} = getHandlers(owner, options);
72
+ const handlers = getHandlers(owner, options);
69
73
 
70
74
  return {
71
- register,
72
- unregister,
75
+ deregister(constructor: Constructor) {
76
+ handlers.deregister(constructor);
77
+ },
73
78
  handle(value: unknown, ...parameters: unknown[]) {
74
- const handler = get(value, value);
79
+ const handler = handlers.get(value, value);
75
80
 
76
81
  if (handler == null) {
77
82
  return options.callback(value, ...parameters);
@@ -79,6 +84,9 @@ export function getSelfHandlers(owner: GenericCallback, options: Options) {
79
84
 
80
85
  return typeof handler === 'function' ? handler(value) : (value as any)[handler]();
81
86
  },
87
+ register(constructor: Constructor, handler?: string | GenericCallback) {
88
+ handlers.register(constructor, handler);
89
+ },
82
90
  };
83
91
  }
84
92
 
@@ -1,15 +1,7 @@
1
1
  import type {NestedKeys, NestedValue, PlainObject, ToString} from '../../models';
2
+ import type {Result} from '../../result/models';
2
3
  import {getNestedValue} from './misc';
3
4
 
4
- // #region Types
5
-
6
- export type HasValue<Value> = {
7
- exists: boolean;
8
- value: Value;
9
- };
10
-
11
- // #endregion
12
-
13
5
  // #region Functions
14
6
 
15
7
  /**
@@ -37,38 +29,46 @@ export function hasValue<Data extends PlainObject>(
37
29
  ): boolean;
38
30
 
39
31
  export function hasValue(data: PlainObject, path: string, ignoreCase?: boolean): boolean {
40
- return getNestedValue(data, path, ignoreCase === true).exists;
32
+ return getNestedValue(data, path, ignoreCase === true).ok;
41
33
  }
42
34
 
43
- hasValue.get = getWithHasValue;
35
+ hasValue.get = hasValueResult;
44
36
 
45
37
  /**
46
38
  * Check if a nested property is defined in an object, and get its value if it is
39
+ *
40
+ * Available as `hasValueResult` and `hasValue.get`
47
41
  * @param data Object to check in
48
42
  * @param path Path for property
49
43
  * @param ignoreCase If `true`, the path matching is case-insensitive
50
44
  * @return Result object
51
45
  */
52
- function getWithHasValue<Data extends PlainObject, Path extends NestedKeys<Data>>(
46
+ function hasValueResult<Data extends PlainObject, Path extends NestedKeys<Data>>(
53
47
  data: Data,
54
48
  path: Path,
55
49
  ignoreCase?: boolean,
56
- ): HasValue<NestedValue<Data, ToString<Path>>>;
50
+ ): Result<NestedValue<Data, ToString<Path>>, undefined>;
57
51
 
58
52
  /**
59
53
  * Check if a nested property is defined in an object, and get its value if it is
54
+ *
55
+ * Available as `hasValueResult` and `hasValue.get`
60
56
  * @param data Object to check in
61
57
  * @param path Path for property
62
58
  * @param ignoreCase If `true`, the path matching is case-insensitive
63
59
  * @return Result object
64
60
  */
65
- function getWithHasValue<Data extends PlainObject>(
61
+ function hasValueResult<Data extends PlainObject>(
66
62
  data: Data,
67
63
  path: string,
68
64
  ignoreCase?: boolean,
69
- ): HasValue<unknown>;
65
+ ): Result<unknown, undefined>;
70
66
 
71
- function getWithHasValue(data: PlainObject, path: string, ignoreCase?: boolean): HasValue<unknown> {
67
+ function hasValueResult(
68
+ data: PlainObject,
69
+ path: string,
70
+ ignoreCase?: boolean,
71
+ ): Result<unknown, undefined> {
72
72
  return getNestedValue(data, path, ignoreCase === true);
73
73
  }
74
74
 
@@ -1,4 +1,6 @@
1
1
  import type {PlainObject} from '../../models';
2
+ import {error, ok} from '../../result/misc';
3
+ import type {Result} from '../../result/models';
2
4
  import {ignoreKey} from '../string';
3
5
 
4
6
  // #region Functions
@@ -15,14 +17,14 @@ export function getNestedValue(
15
17
  data: object,
16
18
  path: string,
17
19
  ignoreCase: boolean,
18
- ): {exists: boolean; value: unknown} {
20
+ ): Result<unknown, undefined> {
19
21
  if (
20
22
  typeof data !== 'object' ||
21
23
  data === null ||
22
24
  typeof path !== 'string' ||
23
25
  path.trim().length === 0
24
26
  ) {
25
- return {exists: false, value: undefined};
27
+ return error(undefined);
26
28
  }
27
29
 
28
30
  const shouldIgnoreCase = ignoreCase === true;
@@ -41,14 +43,14 @@ export function getNestedValue(
41
43
 
42
44
  const handled = handleValue(current, part, null, true, shouldIgnoreCase);
43
45
 
44
- if (!handled.exists) {
46
+ if (!handled.ok) {
45
47
  return handled;
46
48
  }
47
49
 
48
50
  current = handled.value as object;
49
51
  }
50
52
 
51
- return {exists: true, value: current};
53
+ return ok(current);
52
54
  }
53
55
 
54
56
  export function getPaths(path: string, lowercase: boolean): string | string[] {
@@ -67,7 +69,7 @@ export function handleValue(
67
69
  value: unknown,
68
70
  get: true,
69
71
  ignoreCase: boolean,
70
- ): {exists: boolean; value: unknown};
72
+ ): Result<unknown, undefined>;
71
73
 
72
74
  export function handleValue(
73
75
  data: object,
@@ -83,19 +85,19 @@ export function handleValue(
83
85
  value: unknown,
84
86
  get: boolean,
85
87
  ignoreCase: boolean,
86
- ): {exists: boolean; value: unknown} | void {
88
+ ): Result<unknown, undefined> | void {
87
89
  if (typeof data === 'object' && data !== null && !ignoreKey(path)) {
88
90
  const key = ignoreCase ? findKey(path, data) : path;
89
91
 
90
92
  if (get) {
91
- return {exists: key in data, value: data[key as never]};
93
+ return key in data ? ok(data[key as never]) : error(undefined);
92
94
  }
93
95
 
94
96
  (data as PlainObject)[key] = typeof value === 'function' ? value(data[key as never]) : value;
95
97
  }
96
98
 
97
99
  if (get) {
98
- return {exists: false, value: undefined};
100
+ return error(undefined);
99
101
  }
100
102
  }
101
103
 
@@ -24,6 +24,8 @@ 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`
27
29
  * @param promise Promise to wrap
28
30
  * @param options Options for the promise
29
31
  * @returns Wrapped promise
@@ -35,6 +37,8 @@ export async function attemptPromise<Value>(
35
37
 
36
38
  /**
37
39
  * Wrap a promise-returning callback with safety handlers, with optional abort capabilities and timeout
40
+ *
41
+ * Available as `attemptPromise` and `attempt.promise`
38
42
  * @param callback Callback to wrap
39
43
  * @param options Options for the promise
40
44
  * @returns Promise-wrapped callback
@@ -46,6 +50,8 @@ export async function attemptPromise<Value>(
46
50
 
47
51
  /**
48
52
  * Wrap a callback with a promise and safety handlers, with optional abort capabilities and timeout
53
+ *
54
+ * Available as `attemptPromise` and `attempt.promise`
49
55
  * @param callback Callback to wrap
50
56
  * @param options Options for the promise
51
57
  * @returns Promise-wrapped callback
@@ -99,7 +105,7 @@ export async function attemptPromise<Value>(
99
105
  const promise = new Promise<Value>((resolve, reject) => {
100
106
  rejector = reject;
101
107
 
102
- handler(resolve, reject);
108
+ void handler(resolve, reject);
103
109
  });
104
110
 
105
111
  return time > 0 ? getTimedPromise(promise, time, signal) : promise;
@@ -253,11 +259,13 @@ promises.result = resultPromises;
253
259
  * Handle a list of promises, returning their results in an ordered array of results _({@link Result})_.
254
260
  *
255
261
  * Depending on the strategy, the function will either reject on the first error encountered or return an array of rejected and resolved results
262
+ *
263
+ * Available as `resultPromises` and `promises.result`
256
264
  * @param items List of promises
257
265
  * @param signal AbortSignal for aborting the operation _(when aborted, the promise will reject with the reason of the signal)_
258
266
  * @returns List of results
259
267
  */
260
- async function resultPromises<Items extends unknown[]>(
268
+ export async function resultPromises<Items extends unknown[]>(
261
269
  items: [...Items],
262
270
  signal?: AbortSignal,
263
271
  ): Promise<PromisesResult<PromisesItems<Items>>>;
@@ -266,16 +274,18 @@ async function resultPromises<Items extends unknown[]>(
266
274
  * Handle a list of promises, returning their results in an ordered array of results _({@link Result})_.
267
275
  *
268
276
  * Depending on the strategy, the function will either reject on the first error encountered or return an array of rejected and resolved results
277
+ *
278
+ * Available as `resultPromises` and `promises.result`
269
279
  * @param items List of promises
270
280
  * @param signal AbortSignal for aborting the operation _(when aborted, the promise will reject with the reason of the signal)_
271
281
  * @returns List of results
272
282
  */
273
- async function resultPromises<Value>(
283
+ export async function resultPromises<Value>(
274
284
  items: Promise<Value>[],
275
285
  signal?: AbortSignal,
276
286
  ): Promise<Result<Awaited<Value>>[]>;
277
287
 
278
- async function resultPromises(
288
+ export async function resultPromises(
279
289
  items: Promise<unknown>[],
280
290
  signal?: AbortSignal,
281
291
  ): Promise<Result<unknown>[]> {
@@ -9,41 +9,51 @@ import {attemptPipe} from './work/pipe';
9
9
 
10
10
  /**
11
11
  * Executes a promise, catching any errors, and returns a result
12
+ *
13
+ * Available as `asyncAttempt` and `attempt.async`
12
14
  * @param promise Promise to execute
13
15
  * @param error Error value
14
16
  * @returns Callback result
15
17
  */
16
- async function asyncAttempt<Value, E>(
18
+ export async function asyncAttempt<Value, E>(
17
19
  promise: Promise<Value>,
18
20
  error: E,
19
21
  ): Promise<ExtendedResult<Awaited<Value>, E>>;
20
22
 
21
23
  /**
22
24
  * Executes a callback asynchronously, catching any errors, and returns a result
25
+ *
26
+ * Available as `asyncAttempt` and `attempt.async`
23
27
  * @param callback Callback to execute
24
28
  * @param error Error value
25
29
  * @returns Callback result
26
30
  */
27
- async function asyncAttempt<Value, E>(
31
+ export async function asyncAttempt<Value, E>(
28
32
  callback: () => Promise<Value>,
29
33
  error: E,
30
34
  ): Promise<ExtendedResult<Awaited<Value>, E>>;
31
35
 
32
36
  /**
33
37
  * Executes a promise, catching any errors, and returns a result
38
+ *
39
+ * Available as `asyncAttempt` and `attempt.async`
34
40
  * @param promise Promise to execute
35
41
  * @returns Callback result
36
42
  */
37
- async function asyncAttempt<Value>(promise: Promise<Value>): Promise<Result<Awaited<Value>>>;
43
+ export async function asyncAttempt<Value>(promise: Promise<Value>): Promise<Result<Awaited<Value>>>;
38
44
 
39
45
  /**
40
46
  * Executes a callback asynchronously, catching any errors, and returns a result
47
+ *
48
+ * Available as `asyncAttempt` and `attempt.async`
41
49
  * @param callback Callback to execute
42
50
  * @returns Callback result
43
51
  */
44
- async function asyncAttempt<Value>(callback: () => Promise<Value>): Promise<Result<Awaited<Value>>>;
52
+ export async function asyncAttempt<Value>(
53
+ callback: () => Promise<Value>,
54
+ ): Promise<Result<Awaited<Value>>>;
45
55
 
46
- async function asyncAttempt<Value, E>(
56
+ export async function asyncAttempt<Value, E>(
47
57
  value: Promise<Value> | (() => Promise<Value>),
48
58
  err?: E,
49
59
  ): Promise<unknown> {
@@ -8,7 +8,7 @@ import type {AnyResult, ExtendedErr, ResultMatch} from './models';
8
8
  * @param result Result to handle
9
9
  * @param handler Match callbacks
10
10
  */
11
- async function asyncMatchResult<Value, Returned, E = Error>(
11
+ export async function asyncMatchResult<Value, Returned, E = Error>(
12
12
  result: AnyResult<Value, E> | Promise<AnyResult<Value, E>> | (() => Promise<AnyResult<Value, E>>),
13
13
  handler: ResultMatch<Value, Returned, E>,
14
14
  ): Promise<Returned>;
@@ -19,13 +19,13 @@ async function asyncMatchResult<Value, Returned, E = Error>(
19
19
  * @param ok Ok callback
20
20
  * @param error Error callback
21
21
  */
22
- async function asyncMatchResult<Value, Returned, E = Error>(
22
+ export async function asyncMatchResult<Value, Returned, E = Error>(
23
23
  result: AnyResult<Value, E> | Promise<AnyResult<Value, E>> | (() => Promise<AnyResult<Value, E>>),
24
24
  ok: ResultMatch<Value, Returned, E>['ok'],
25
25
  error: ResultMatch<Value, Returned, E>['error'],
26
26
  ): Promise<Returned>;
27
27
 
28
- async function asyncMatchResult<Value, Returned, E = Error>(
28
+ export async function asyncMatchResult<Value, Returned, E = Error>(
29
29
  result: AnyResult<Value, E> | Promise<AnyResult<Value, E>> | (() => Promise<AnyResult<Value, E>>),
30
30
  first: ResultMatch<Value, Returned, E> | ResultMatch<Value, Returned, E>['ok'],
31
31
  error?: ResultMatch<Value, Returned, E>['error'],
@@ -58,6 +58,8 @@ async function asyncMatchResult<Value, Returned, E = Error>(
58
58
 
59
59
  /**
60
60
  * Handles a result with match callbacks
61
+ *
62
+ * Available as `matchResult` and `attempt.match`
61
63
  * @param result Result to handle
62
64
  * @param handler Match callbacks
63
65
  */
@@ -68,6 +70,8 @@ export function matchResult<Value, Returned, E = Error>(
68
70
 
69
71
  /**
70
72
  * Handles a result with match callbacks
73
+ *
74
+ * Available as `matchResult` and `attempt.match`
71
75
  * @param result Result to handle
72
76
  * @param ok Ok callback
73
77
  * @param error Error callback
@@ -5,7 +5,7 @@ import type {AnyResult, Err, ExtendedErr, Ok, Result} from './models';
5
5
 
6
6
  /**
7
7
  * Creates an extended error result
8
- * @param error Error value
8
+ * @param value Error value
9
9
  * @param original Original error
10
10
  * @returns Error result
11
11
  */
@@ -13,7 +13,7 @@ export function error<E>(value: E, original: Error): ExtendedErr<E>;
13
13
 
14
14
  /**
15
15
  * Creates an error result
16
- * @param error Error value
16
+ * @param value Error value
17
17
  * @returns Error result
18
18
  */
19
19
  export function error<E>(value: E): Err<E>;
@@ -51,7 +51,7 @@ export function ok<Value>(value: Value): Ok<Value> {
51
51
  * Converts a result to a promise
52
52
  *
53
53
  * Resolves if ok, rejects for error
54
- * @param result Result to convert
54
+ * @param callback Callback to get the result
55
55
  * @returns Promised result
56
56
  */
57
57
  export async function toPromise<Value, E = Error>(