ts-data-forge 14.5.0 → 14.6.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 (71) hide show
  1. package/dist/array/impl/array-utils-element-access.mjs +1 -1
  2. package/dist/array/impl/array-utils-reducing-value.mjs +3 -3
  3. package/dist/array/impl/array-utils-search.mjs +1 -1
  4. package/dist/collections/imap-mapped.mjs +1 -1
  5. package/dist/collections/imap.mjs +1 -1
  6. package/dist/entry-point.mjs +8 -6
  7. package/dist/entry-point.mjs.map +1 -1
  8. package/dist/functional/async-result/async-result.d.mts +16 -0
  9. package/dist/functional/async-result/async-result.d.mts.map +1 -0
  10. package/dist/functional/async-result/async-result.mjs +3 -0
  11. package/dist/functional/async-result/async-result.mjs.map +1 -0
  12. package/dist/functional/async-result/impl/async-result-flat-map.d.mts +39 -0
  13. package/dist/functional/async-result/impl/async-result-flat-map.d.mts.map +1 -0
  14. package/dist/functional/async-result/impl/async-result-flat-map.mjs +25 -0
  15. package/dist/functional/async-result/impl/async-result-flat-map.mjs.map +1 -0
  16. package/dist/functional/async-result/impl/async-result-from-promise.d.mts +47 -0
  17. package/dist/functional/async-result/impl/async-result-from-promise.d.mts.map +1 -0
  18. package/dist/functional/async-result/impl/async-result-from-promise.mjs +11 -0
  19. package/dist/functional/async-result/impl/async-result-from-promise.mjs.map +1 -0
  20. package/dist/functional/async-result/impl/async-result-from-throwable.d.mts +53 -0
  21. package/dist/functional/async-result/impl/async-result-from-throwable.d.mts.map +1 -0
  22. package/dist/functional/async-result/impl/async-result-from-throwable.mjs +17 -0
  23. package/dist/functional/async-result/impl/async-result-from-throwable.mjs.map +1 -0
  24. package/dist/functional/async-result/impl/async-result-map-err.d.mts +35 -0
  25. package/dist/functional/async-result/impl/async-result-map-err.d.mts.map +1 -0
  26. package/dist/functional/async-result/impl/async-result-map-err.mjs +23 -0
  27. package/dist/functional/async-result/impl/async-result-map-err.mjs.map +1 -0
  28. package/dist/functional/async-result/impl/async-result-map.d.mts +45 -0
  29. package/dist/functional/async-result/impl/async-result-map.d.mts.map +1 -0
  30. package/dist/functional/async-result/impl/async-result-map.mjs +23 -0
  31. package/dist/functional/async-result/impl/async-result-map.mjs.map +1 -0
  32. package/dist/functional/async-result/impl/async-result-unwrap-or.d.mts +34 -0
  33. package/dist/functional/async-result/impl/async-result-unwrap-or.d.mts.map +1 -0
  34. package/dist/functional/async-result/impl/async-result-unwrap-or.mjs +10 -0
  35. package/dist/functional/async-result/impl/async-result-unwrap-or.mjs.map +1 -0
  36. package/dist/functional/async-result/impl/index.d.mts +8 -0
  37. package/dist/functional/async-result/impl/index.d.mts.map +1 -0
  38. package/dist/functional/async-result/impl/index.mjs +7 -0
  39. package/dist/functional/async-result/impl/index.mjs.map +1 -0
  40. package/dist/functional/async-result/impl/types.d.mts +45 -0
  41. package/dist/functional/async-result/impl/types.d.mts.map +1 -0
  42. package/dist/functional/async-result/impl/types.mjs +2 -0
  43. package/dist/functional/async-result/impl/types.mjs.map +1 -0
  44. package/dist/functional/async-result/index.d.mts +2 -0
  45. package/dist/functional/async-result/index.d.mts.map +1 -0
  46. package/dist/functional/async-result/index.mjs +3 -0
  47. package/dist/functional/async-result/index.mjs.map +1 -0
  48. package/dist/functional/index.d.mts +1 -0
  49. package/dist/functional/index.d.mts.map +1 -1
  50. package/dist/functional/index.mjs +8 -6
  51. package/dist/functional/index.mjs.map +1 -1
  52. package/dist/functional/result/impl/result-from-promise.d.mts +5 -0
  53. package/dist/functional/result/impl/result-from-promise.d.mts.map +1 -1
  54. package/dist/functional/result/impl/result-from-promise.mjs.map +1 -1
  55. package/dist/json/json.mjs +2 -2
  56. package/dist/number/refined-number-utils.mjs +1 -1
  57. package/dist/object/object.mjs +1 -1
  58. package/package.json +7 -7
  59. package/src/functional/async-result/async-result.mts +17 -0
  60. package/src/functional/async-result/impl/async-result-flat-map.mts +99 -0
  61. package/src/functional/async-result/impl/async-result-from-promise.mts +69 -0
  62. package/src/functional/async-result/impl/async-result-from-throwable.mts +78 -0
  63. package/src/functional/async-result/impl/async-result-map-err.mts +84 -0
  64. package/src/functional/async-result/impl/async-result-map.mts +94 -0
  65. package/src/functional/async-result/impl/async-result-unwrap-or.mts +42 -0
  66. package/src/functional/async-result/impl/index.mts +7 -0
  67. package/src/functional/async-result/impl/types.mts +51 -0
  68. package/src/functional/async-result/index.mts +1 -0
  69. package/src/functional/async-result.test.mts +290 -0
  70. package/src/functional/index.mts +1 -0
  71. package/src/functional/result/impl/result-from-promise.mts +5 -0
@@ -0,0 +1,42 @@
1
+ import { Result } from '../../result/index.mjs';
2
+ import { type AsyncResult } from '../async-result.mjs';
3
+
4
+ /**
5
+ * Unwraps an `AsyncResult`, resolving to the success value or a default value
6
+ * if it resolves to `Err`.
7
+ *
8
+ * @example
9
+ *
10
+ * ```ts
11
+ * const okValue = AsyncResult.fromPromise(
12
+ * Promise.resolve(10),
13
+ * () => 'failed',
14
+ * );
15
+ *
16
+ * const errValue = AsyncResult.fromPromise(
17
+ * Promise.reject(new Error('boom')),
18
+ * () => 'failed',
19
+ * );
20
+ *
21
+ * assert.isTrue((await AsyncResult.unwrapOr(okValue, 0)) === 10);
22
+ *
23
+ * assert.isTrue((await AsyncResult.unwrapOr(errValue, 0)) === 0);
24
+ * ```
25
+ *
26
+ * @template S The type of the success value.
27
+ * @template E The type of the error value.
28
+ * @template D The type of the default value.
29
+ * @param asyncResult The `AsyncResult` to unwrap.
30
+ * @param defaultValue The value to resolve to if `asyncResult` resolves to
31
+ * `Err`.
32
+ * @returns A Promise of the success value if `Ok`, otherwise of
33
+ * `defaultValue`.
34
+ */
35
+ export const unwrapOr = async <S, E, D>(
36
+ asyncResult: AsyncResult<S, E>,
37
+ defaultValue: D,
38
+ ): Promise<D | S> => {
39
+ const result = await asyncResult;
40
+
41
+ return Result.isOk(result) ? result.value : defaultValue;
42
+ };
@@ -0,0 +1,7 @@
1
+ export * from './async-result-flat-map.mjs';
2
+ export * from './async-result-from-promise.mjs';
3
+ export * from './async-result-from-throwable.mjs';
4
+ export * from './async-result-map-err.mjs';
5
+ export * from './async-result-map.mjs';
6
+ export * from './async-result-unwrap-or.mjs';
7
+ export * from './types.mjs';
@@ -0,0 +1,51 @@
1
+ import { type Err, type Ok } from '../../../types.mjs';
2
+ import { type UnknownResult } from '../../result/index.mjs';
3
+ import { type AsyncResult } from '../async-result.mjs';
4
+
5
+ /**
6
+ * Base type for any `AsyncResult`, used for generic constraints. Represents
7
+ * an `AsyncResult` with unknown success and error types.
8
+ */
9
+ export type UnknownAsyncResult = AsyncResult<unknown, unknown>;
10
+
11
+ /**
12
+ * Extracts the success value type `S` from a `Result.Ok<S>`, or from an
13
+ * `AsyncResult` resolving to one. Distributes over unions, so an
14
+ * `Ok<S> | Err<E>` union resolves to `S`.
15
+ *
16
+ * @template R The `Result` or `AsyncResult` type to unwrap.
17
+ */
18
+ export type UnwrapOk<R extends UnknownAsyncResult | UnknownResult> =
19
+ UnwrapOkImpl<SyncResultOf<R>>;
20
+
21
+ /**
22
+ * Extracts the error value type `E` from a `Result.Err<E>`, or from an
23
+ * `AsyncResult` resolving to one. Distributes over unions, so an
24
+ * `Ok<S> | Err<E>` union resolves to `E`.
25
+ *
26
+ * @template R The `Result` or `AsyncResult` type to unwrap.
27
+ */
28
+ export type UnwrapErr<R extends UnknownAsyncResult | UnknownResult> =
29
+ UnwrapErrImpl<SyncResultOf<R>>;
30
+
31
+ /**
32
+ * @template R The `Result` or `AsyncResult` type.
33
+ * @internal
34
+ * Resolves an `AsyncResult` to its `Result`, leaving a plain `Result`
35
+ * unchanged. Distributive.
36
+ */
37
+ type SyncResultOf<R> = R extends Promise<infer P> ? P : R;
38
+
39
+ /**
40
+ * @template R The `Result` type to unwrap.
41
+ * @internal
42
+ * Distributive extraction of the `Ok` value type.
43
+ */
44
+ type UnwrapOkImpl<R> = R extends Ok<infer S> ? S : never;
45
+
46
+ /**
47
+ * @template R The `Result` type to unwrap.
48
+ * @internal
49
+ * Distributive extraction of the `Err` value type.
50
+ */
51
+ type UnwrapErrImpl<R> = R extends Err<infer E> ? E : never;
@@ -0,0 +1 @@
1
+ export * from './async-result.mjs';
@@ -0,0 +1,290 @@
1
+ import { isError } from '@sindresorhus/is';
2
+ import { AsyncResult as AsyncResultFromEntryPoint } from '../entry-point.mjs';
3
+ import { expectType } from '../expect-type.mjs';
4
+ import { AsyncResult } from './async-result/index.mjs';
5
+ import { Result } from './result/index.mjs';
6
+
7
+ // Fixtures are created through functions so that the success/error types stay
8
+ // unions instead of being narrowed to literal types by const inference.
9
+ const okFixture = (value: number): AsyncResult<number, string> =>
10
+ Promise.resolve(Result.ok(value));
11
+
12
+ const errFixture = (message: string): AsyncResult<number, string> =>
13
+ Promise.resolve(Result.err(message));
14
+
15
+ describe('AsyncResult test', () => {
16
+ describe('fromPromise', () => {
17
+ test('resolved promise becomes Ok', async () => {
18
+ const result = await AsyncResult.fromPromise(
19
+ Promise.resolve(42),
20
+ (error) => `mapped: ${String(error)}`,
21
+ );
22
+
23
+ assert.isTrue(Result.isOk(result));
24
+
25
+ assert.deepStrictEqual(result, Result.ok(42));
26
+ });
27
+
28
+ test('rejected promise becomes Err with mapError applied', async () => {
29
+ const result = await AsyncResult.fromPromise(
30
+ Promise.reject(new Error('boom')),
31
+ (error) => (isError(error) ? error.message : 'unknown'),
32
+ );
33
+
34
+ assert.isTrue(Result.isErr(result));
35
+
36
+ assert.deepStrictEqual(result, Result.err('boom'));
37
+ });
38
+
39
+ test('resolved promise becomes Ok when mapError is omitted', async () => {
40
+ const result = await AsyncResult.fromPromise(Promise.resolve(42));
41
+
42
+ assert.deepStrictEqual(result, Result.ok(42));
43
+
44
+ expectType<typeof result, Result<number, unknown>>('=');
45
+ });
46
+
47
+ test('rejection reason is carried as-is when mapError is omitted', async () => {
48
+ const error = new Error('boom');
49
+
50
+ const result = await AsyncResult.fromPromise(
51
+ Promise.reject(error).then(() => 42),
52
+ );
53
+
54
+ assert.isTrue(Result.isErr(result));
55
+
56
+ assert.deepStrictEqual(result, Result.err(error));
57
+ });
58
+ });
59
+
60
+ describe('fromThrowable', () => {
61
+ test('resolved async function becomes Ok', async () => {
62
+ const result = await AsyncResult.fromThrowable(
63
+ () => Promise.resolve('data'),
64
+ (error) => `mapped: ${String(error)}`,
65
+ );
66
+
67
+ assert.isTrue(Result.isOk(result));
68
+
69
+ assert.deepStrictEqual(result, Result.ok('data'));
70
+ });
71
+
72
+ test('synchronous throw becomes Err with mapError applied', async () => {
73
+ const result = await AsyncResult.fromThrowable(
74
+ (): Promise<number> => {
75
+ throw new Error('sync failure');
76
+ },
77
+ (error) => (isError(error) ? error.message : 'unknown'),
78
+ );
79
+
80
+ assert.isTrue(Result.isErr(result));
81
+
82
+ assert.deepStrictEqual(result, Result.err('sync failure'));
83
+ });
84
+
85
+ test('async rejection becomes Err with mapError applied', async () => {
86
+ const result = await AsyncResult.fromThrowable(
87
+ () => Promise.reject(new Error('async failure')),
88
+ (error) => (isError(error) ? error.message : 'unknown'),
89
+ );
90
+
91
+ assert.isTrue(Result.isErr(result));
92
+
93
+ assert.deepStrictEqual(result, Result.err('async failure'));
94
+ });
95
+
96
+ test('synchronous throw is carried as-is when mapError is omitted', async () => {
97
+ const error = new Error('sync failure');
98
+
99
+ const result = await AsyncResult.fromThrowable((): Promise<number> => {
100
+ throw error;
101
+ });
102
+
103
+ assert.deepStrictEqual(result, Result.err(error));
104
+
105
+ expectType<typeof result, Result<number, unknown>>('=');
106
+ });
107
+
108
+ test('async rejection is carried as-is when mapError is omitted', async () => {
109
+ const error = new Error('async failure');
110
+
111
+ const result = await AsyncResult.fromThrowable(() =>
112
+ Promise.reject(error).then(() => 42),
113
+ );
114
+
115
+ assert.deepStrictEqual(result, Result.err(error));
116
+ });
117
+ });
118
+
119
+ describe('map', () => {
120
+ test('maps Ok value with a synchronous function', async () => {
121
+ const result = await AsyncResult.map(okFixture(5), (value) => value * 2);
122
+
123
+ assert.deepStrictEqual(result, Result.ok(10));
124
+ });
125
+
126
+ test('maps Ok value with an asynchronous function', async () => {
127
+ const result = await AsyncResult.map(okFixture(5), (value) =>
128
+ Promise.resolve(value * 3),
129
+ );
130
+
131
+ assert.deepStrictEqual(result, Result.ok(15));
132
+ });
133
+
134
+ test('passes Err through unchanged', async () => {
135
+ let mut_called = false;
136
+
137
+ const result = await AsyncResult.map(errFixture('failure'), (value) => {
138
+ mut_called = true;
139
+
140
+ return value * 2;
141
+ });
142
+
143
+ assert.deepStrictEqual(result, Result.err('failure'));
144
+
145
+ assert.isFalse(mut_called);
146
+ });
147
+
148
+ test('curried version maps Ok value', async () => {
149
+ const double = AsyncResult.map((value: number) => value * 2);
150
+
151
+ const result = await double(okFixture(21));
152
+
153
+ assert.deepStrictEqual(result, Result.ok(42));
154
+ });
155
+
156
+ test('curried version passes Err through unchanged', async () => {
157
+ const double = AsyncResult.map((value: number) => value * 2);
158
+
159
+ const result = await double(errFixture('failure'));
160
+
161
+ assert.deepStrictEqual(result, Result.err('failure'));
162
+ });
163
+ });
164
+
165
+ describe('mapErr', () => {
166
+ test('maps Err value with a synchronous function', async () => {
167
+ const result = await AsyncResult.mapErr(errFixture('failure'), (error) =>
168
+ error.toUpperCase(),
169
+ );
170
+
171
+ assert.deepStrictEqual(result, Result.err('FAILURE'));
172
+ });
173
+
174
+ test('maps Err value with an asynchronous function', async () => {
175
+ const result = await AsyncResult.mapErr(
176
+ errFixture('failure'),
177
+ (message) => Promise.resolve(message.length),
178
+ );
179
+
180
+ assert.deepStrictEqual(result, Result.err(7));
181
+ });
182
+
183
+ test('passes Ok through unchanged', async () => {
184
+ let mut_called = false;
185
+
186
+ const result = await AsyncResult.mapErr(okFixture(1), (error) => {
187
+ mut_called = true;
188
+
189
+ return error.toUpperCase();
190
+ });
191
+
192
+ assert.deepStrictEqual(result, Result.ok(1));
193
+
194
+ assert.isFalse(mut_called);
195
+ });
196
+
197
+ test('curried version maps Err value', async () => {
198
+ const upper = AsyncResult.mapErr((error: string) => error.toUpperCase());
199
+
200
+ const result = await upper(errFixture('failure'));
201
+
202
+ assert.deepStrictEqual(result, Result.err('FAILURE'));
203
+ });
204
+ });
205
+
206
+ describe('flatMap', () => {
207
+ test('chains with a function returning a Result', async () => {
208
+ const result = await AsyncResult.flatMap(okFixture(4), (value) =>
209
+ value > 0 ? Result.ok(value * 10) : Result.err('non-positive'),
210
+ );
211
+
212
+ assert.deepStrictEqual(result, Result.ok(40));
213
+ });
214
+
215
+ test('chains with a function returning an AsyncResult', async () => {
216
+ const result = await AsyncResult.flatMap(okFixture(4), (value) =>
217
+ AsyncResult.fromPromise(Promise.resolve(value + 1), () => 'failed'),
218
+ );
219
+
220
+ assert.deepStrictEqual(result, Result.ok(5));
221
+ });
222
+
223
+ test('chains into Err returned by the function', async () => {
224
+ const result = await AsyncResult.flatMap(
225
+ okFixture(-1),
226
+ (value): Result<number, string> =>
227
+ value > 0 ? Result.ok(value * 10) : Result.err('non-positive'),
228
+ );
229
+
230
+ assert.deepStrictEqual(result, Result.err('non-positive'));
231
+ });
232
+
233
+ test('short-circuits on Err without calling the function', async () => {
234
+ let mut_called = false;
235
+
236
+ const result = await AsyncResult.flatMap(
237
+ errFixture('failure'),
238
+ (value) => {
239
+ mut_called = true;
240
+
241
+ return Result.ok(value * 2);
242
+ },
243
+ );
244
+
245
+ assert.deepStrictEqual(result, Result.err('failure'));
246
+
247
+ assert.isFalse(mut_called);
248
+ });
249
+
250
+ test('curried version chains with a function returning a Result', async () => {
251
+ const validate = AsyncResult.flatMap(
252
+ (value: number): Result<number, string> =>
253
+ value > 0 ? Result.ok(value) : Result.err('non-positive'),
254
+ );
255
+
256
+ const passed = await validate(okFixture(7));
257
+
258
+ const failed = await validate(okFixture(-7));
259
+
260
+ assert.deepStrictEqual(passed, Result.ok(7));
261
+
262
+ assert.deepStrictEqual(failed, Result.err('non-positive'));
263
+ });
264
+ });
265
+
266
+ describe('unwrapOr', () => {
267
+ test('returns the success value for Ok', async () => {
268
+ const value = await AsyncResult.unwrapOr(okFixture(10), 0);
269
+
270
+ expect(value).toBe(10);
271
+ });
272
+
273
+ test('returns the default value for Err', async () => {
274
+ const value = await AsyncResult.unwrapOr(errFixture('failure'), 0);
275
+
276
+ expect(value).toBe(0);
277
+ });
278
+ });
279
+
280
+ describe('entry point', () => {
281
+ test('AsyncResult is exported from the package entry point', async () => {
282
+ const result = await AsyncResultFromEntryPoint.fromPromise(
283
+ Promise.resolve(1),
284
+ () => 'failed',
285
+ );
286
+
287
+ assert.deepStrictEqual(result, Result.ok(1));
288
+ });
289
+ });
290
+ });
@@ -1,3 +1,4 @@
1
+ export * from './async-result/index.mjs';
1
2
  export * from './match.mjs';
2
3
  export * from './optional/index.mjs';
3
4
  export * from './pipe.mjs';
@@ -17,6 +17,11 @@ type UnwrapPromise<P extends Promise<unknown>> =
17
17
  * input Promise rejects, the `Result` will be `Err` with the rejection
18
18
  * reason.
19
19
  *
20
+ * The error channel is `unknown`, since the rejection reason is carried
21
+ * through untouched. For a concrete error type, use `AsyncResult.fromPromise`
22
+ * with a `mapError` function; called without one it behaves exactly as this
23
+ * function does.
24
+ *
20
25
  * @example
21
26
  *
22
27
  * ```ts