ts-data-forge 14.4.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 (123) hide show
  1. package/README.md +6 -0
  2. package/dist/array/impl/array-utils-element-access.mjs +1 -1
  3. package/dist/array/impl/array-utils-reducing-value.mjs +3 -3
  4. package/dist/array/impl/array-utils-search.d.mts.map +1 -1
  5. package/dist/array/impl/array-utils-search.mjs +1 -1
  6. package/dist/array/impl/array-utils-search.mjs.map +1 -1
  7. package/dist/collections/imap-mapped.mjs +1 -1
  8. package/dist/collections/imap.mjs +1 -1
  9. package/dist/entry-point.mjs +8 -6
  10. package/dist/entry-point.mjs.map +1 -1
  11. package/dist/functional/async-result/async-result.d.mts +16 -0
  12. package/dist/functional/async-result/async-result.d.mts.map +1 -0
  13. package/dist/functional/async-result/async-result.mjs +3 -0
  14. package/dist/functional/async-result/async-result.mjs.map +1 -0
  15. package/dist/functional/async-result/impl/async-result-flat-map.d.mts +39 -0
  16. package/dist/functional/async-result/impl/async-result-flat-map.d.mts.map +1 -0
  17. package/dist/functional/async-result/impl/async-result-flat-map.mjs +25 -0
  18. package/dist/functional/async-result/impl/async-result-flat-map.mjs.map +1 -0
  19. package/dist/functional/async-result/impl/async-result-from-promise.d.mts +47 -0
  20. package/dist/functional/async-result/impl/async-result-from-promise.d.mts.map +1 -0
  21. package/dist/functional/async-result/impl/async-result-from-promise.mjs +11 -0
  22. package/dist/functional/async-result/impl/async-result-from-promise.mjs.map +1 -0
  23. package/dist/functional/async-result/impl/async-result-from-throwable.d.mts +53 -0
  24. package/dist/functional/async-result/impl/async-result-from-throwable.d.mts.map +1 -0
  25. package/dist/functional/async-result/impl/async-result-from-throwable.mjs +17 -0
  26. package/dist/functional/async-result/impl/async-result-from-throwable.mjs.map +1 -0
  27. package/dist/functional/async-result/impl/async-result-map-err.d.mts +35 -0
  28. package/dist/functional/async-result/impl/async-result-map-err.d.mts.map +1 -0
  29. package/dist/functional/async-result/impl/async-result-map-err.mjs +23 -0
  30. package/dist/functional/async-result/impl/async-result-map-err.mjs.map +1 -0
  31. package/dist/functional/async-result/impl/async-result-map.d.mts +45 -0
  32. package/dist/functional/async-result/impl/async-result-map.d.mts.map +1 -0
  33. package/dist/functional/async-result/impl/async-result-map.mjs +23 -0
  34. package/dist/functional/async-result/impl/async-result-map.mjs.map +1 -0
  35. package/dist/functional/async-result/impl/async-result-unwrap-or.d.mts +34 -0
  36. package/dist/functional/async-result/impl/async-result-unwrap-or.d.mts.map +1 -0
  37. package/dist/functional/async-result/impl/async-result-unwrap-or.mjs +10 -0
  38. package/dist/functional/async-result/impl/async-result-unwrap-or.mjs.map +1 -0
  39. package/dist/functional/async-result/impl/index.d.mts +8 -0
  40. package/dist/functional/async-result/impl/index.d.mts.map +1 -0
  41. package/dist/functional/async-result/impl/index.mjs +7 -0
  42. package/dist/functional/async-result/impl/index.mjs.map +1 -0
  43. package/dist/functional/async-result/impl/types.d.mts +45 -0
  44. package/dist/functional/async-result/impl/types.d.mts.map +1 -0
  45. package/dist/functional/async-result/impl/types.mjs +2 -0
  46. package/dist/functional/async-result/impl/types.mjs.map +1 -0
  47. package/dist/functional/async-result/index.d.mts +2 -0
  48. package/dist/functional/async-result/index.d.mts.map +1 -0
  49. package/dist/functional/async-result/index.mjs +3 -0
  50. package/dist/functional/async-result/index.mjs.map +1 -0
  51. package/dist/functional/index.d.mts +1 -0
  52. package/dist/functional/index.d.mts.map +1 -1
  53. package/dist/functional/index.mjs +8 -6
  54. package/dist/functional/index.mjs.map +1 -1
  55. package/dist/functional/optional/impl/index.d.mts +1 -0
  56. package/dist/functional/optional/impl/index.d.mts.map +1 -1
  57. package/dist/functional/optional/impl/index.mjs +1 -0
  58. package/dist/functional/optional/impl/index.mjs.map +1 -1
  59. package/dist/functional/optional/impl/optional-match.d.mts +48 -0
  60. package/dist/functional/optional/impl/optional-match.d.mts.map +1 -0
  61. package/dist/functional/optional/impl/optional-match.mjs +22 -0
  62. package/dist/functional/optional/impl/optional-match.mjs.map +1 -0
  63. package/dist/functional/result/impl/index.d.mts +4 -0
  64. package/dist/functional/result/impl/index.d.mts.map +1 -1
  65. package/dist/functional/result/impl/index.mjs +4 -0
  66. package/dist/functional/result/impl/index.mjs.map +1 -1
  67. package/dist/functional/result/impl/result-from-optional.d.mts +41 -0
  68. package/dist/functional/result/impl/result-from-optional.d.mts.map +1 -0
  69. package/dist/functional/result/impl/result-from-optional.mjs +24 -0
  70. package/dist/functional/result/impl/result-from-optional.mjs.map +1 -0
  71. package/dist/functional/result/impl/result-from-promise.d.mts +5 -0
  72. package/dist/functional/result/impl/result-from-promise.d.mts.map +1 -1
  73. package/dist/functional/result/impl/result-from-promise.mjs.map +1 -1
  74. package/dist/functional/result/impl/result-match.d.mts +47 -0
  75. package/dist/functional/result/impl/result-match.d.mts.map +1 -0
  76. package/dist/functional/result/impl/result-match.mjs +24 -0
  77. package/dist/functional/result/impl/result-match.mjs.map +1 -0
  78. package/dist/functional/result/impl/result-safe-try.d.mts +51 -0
  79. package/dist/functional/result/impl/result-safe-try.d.mts.map +1 -0
  80. package/dist/functional/result/impl/result-safe-try.mjs +24 -0
  81. package/dist/functional/result/impl/result-safe-try.mjs.map +1 -0
  82. package/dist/functional/result/impl/result-safe-unwrap.d.mts +36 -0
  83. package/dist/functional/result/impl/result-safe-unwrap.d.mts.map +1 -0
  84. package/dist/functional/result/impl/result-safe-unwrap.mjs +14 -0
  85. package/dist/functional/result/impl/result-safe-unwrap.mjs.map +1 -0
  86. package/dist/json/json.mjs +2 -2
  87. package/dist/number/refined-number-utils.mjs +1 -1
  88. package/dist/object/object.mjs +1 -1
  89. package/dist/others/debounce-function.d.mts.map +1 -1
  90. package/dist/others/debounce-function.mjs.map +1 -1
  91. package/dist/others/fast-deep-equal.d.mts.map +1 -1
  92. package/dist/others/fast-deep-equal.mjs +1 -1
  93. package/dist/others/fast-deep-equal.mjs.map +1 -1
  94. package/package.json +10 -10
  95. package/src/array/impl/array-utils-modification.test.mts +3 -3
  96. package/src/array/impl/array-utils-search.mts +22 -8
  97. package/src/array/impl/array-utils-shape-invariants.test.mts +3 -3
  98. package/src/array/impl/array-utils-transformation.test.mts +1 -1
  99. package/src/functional/async-result/async-result.mts +17 -0
  100. package/src/functional/async-result/impl/async-result-flat-map.mts +99 -0
  101. package/src/functional/async-result/impl/async-result-from-promise.mts +69 -0
  102. package/src/functional/async-result/impl/async-result-from-throwable.mts +78 -0
  103. package/src/functional/async-result/impl/async-result-map-err.mts +84 -0
  104. package/src/functional/async-result/impl/async-result-map.mts +94 -0
  105. package/src/functional/async-result/impl/async-result-unwrap-or.mts +42 -0
  106. package/src/functional/async-result/impl/index.mts +7 -0
  107. package/src/functional/async-result/impl/types.mts +51 -0
  108. package/src/functional/async-result/index.mts +1 -0
  109. package/src/functional/async-result.test.mts +290 -0
  110. package/src/functional/index.mts +1 -0
  111. package/src/functional/optional/impl/index.mts +1 -0
  112. package/src/functional/optional/impl/optional-match.mts +108 -0
  113. package/src/functional/optional.test.mts +79 -0
  114. package/src/functional/result/impl/index.mts +4 -0
  115. package/src/functional/result/impl/result-from-optional.mts +80 -0
  116. package/src/functional/result/impl/result-from-promise.mts +5 -0
  117. package/src/functional/result/impl/result-match.mts +108 -0
  118. package/src/functional/result/impl/result-safe-try.mts +110 -0
  119. package/src/functional/result/impl/result-safe-unwrap.mts +50 -0
  120. package/src/functional/result.test.mts +331 -0
  121. package/src/guard/is-error.test.mts +7 -7
  122. package/src/others/debounce-function.mts +5 -1
  123. package/src/others/fast-deep-equal.mts +9 -2
@@ -0,0 +1,45 @@
1
+ import { type AsyncResult } from '../async-result.mjs';
2
+ /**
3
+ * Maps an `AsyncResult<S, E>` to `AsyncResult<S2, E>` by applying a function
4
+ * to the success value. The mapping function may be synchronous or return a
5
+ * `Promise`; both are handled transparently. If the `AsyncResult` resolves to
6
+ * `Err`, the original `Err` is passed through and the function is not called.
7
+ *
8
+ * If `mapFn` throws or returns a rejecting Promise, the rejection propagates
9
+ * as-is — it is **not** caught and converted into an `Err`. `map` performs no
10
+ * implicit error conversion; write a fallible step as
11
+ * `AsyncResult.flatMap` combined with `AsyncResult.fromThrowable` instead, so
12
+ * that the error type stays explicit.
13
+ *
14
+ * @example
15
+ *
16
+ * ```ts
17
+ * const okValue = AsyncResult.fromPromise(
18
+ * Promise.resolve(5),
19
+ * () => 'failed',
20
+ * );
21
+ *
22
+ * const doubled = await AsyncResult.map(okValue, (value) => value * 2);
23
+ *
24
+ * assert.deepStrictEqual(doubled, Result.ok(10));
25
+ *
26
+ * const double = AsyncResult.map((value: number) => value * 2);
27
+ *
28
+ * const curried = await double(
29
+ * AsyncResult.fromPromise(Promise.resolve(21), () => 'failed'),
30
+ * );
31
+ *
32
+ * assert.deepStrictEqual(curried, Result.ok(42));
33
+ * ```
34
+ *
35
+ * @template S The type of the success value of the input.
36
+ * @template S2 The type of the success value returned by the mapping
37
+ * function.
38
+ * @template E The type of the error value.
39
+ * @param asyncResult The `AsyncResult` to map.
40
+ * @param mapFn The function to apply to the success value if present.
41
+ * @returns A new `AsyncResult<S2, E>`.
42
+ */
43
+ export declare function map<S, S2, E>(asyncResult: AsyncResult<S, E>, mapFn: (value: S) => Promise<S2> | S2): AsyncResult<S2, E>;
44
+ export declare function map<S, S2>(mapFn: (value: S) => Promise<S2> | S2): <E>(asyncResult: AsyncResult<S, E>) => AsyncResult<S2, E>;
45
+ //# sourceMappingURL=async-result-map.d.mts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"async-result-map.d.mts","sourceRoot":"","sources":["../../../../src/functional/async-result/impl/async-result-map.mts"],"names":[],"mappings":"AACA,OAAO,EAAE,KAAK,WAAW,EAAE,MAAM,qBAAqB,CAAC;AAEvD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAwCG;AACH,wBAAgB,GAAG,CAAC,CAAC,EAAE,EAAE,EAAE,CAAC,EAC1B,WAAW,EAAE,WAAW,CAAC,CAAC,EAAE,CAAC,CAAC,EAC9B,KAAK,EAAE,CAAC,KAAK,EAAE,CAAC,KAAK,OAAO,CAAC,EAAE,CAAC,GAAG,EAAE,GACpC,WAAW,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC;AAQtB,wBAAgB,GAAG,CAAC,CAAC,EAAE,EAAE,EACvB,KAAK,EAAE,CAAC,KAAK,EAAE,CAAC,KAAK,OAAO,CAAC,EAAE,CAAC,GAAG,EAAE,GACpC,CAAC,CAAC,EAAE,WAAW,EAAE,WAAW,CAAC,CAAC,EAAE,CAAC,CAAC,KAAK,WAAW,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC"}
@@ -0,0 +1,23 @@
1
+ import { isErr } from '../../result/impl/result-is-err.mjs';
2
+ import { ok } from '../../result/impl/result-ok.mjs';
3
+ import '@sindresorhus/is';
4
+
5
+ function map(...args) {
6
+ switch (args.length) {
7
+ case 2: {
8
+ const [asyncResult, mapFn] = args;
9
+ return mapImpl(asyncResult, mapFn);
10
+ }
11
+ case 1: {
12
+ const [mapFn] = args;
13
+ return (asyncResult) => mapImpl(asyncResult, mapFn);
14
+ }
15
+ }
16
+ }
17
+ const mapImpl = async (asyncResult, mapFn) => {
18
+ const result = await asyncResult;
19
+ return isErr(result) ? result : ok(await mapFn(result.value));
20
+ };
21
+
22
+ export { map };
23
+ //# sourceMappingURL=async-result-map.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"async-result-map.mjs","sources":["../../../../src/functional/async-result/impl/async-result-map.mts"],"sourcesContent":["import { Result } from '../../result/index.mjs';\nimport { type AsyncResult } from '../async-result.mjs';\n\n/**\n * Maps an `AsyncResult<S, E>` to `AsyncResult<S2, E>` by applying a function\n * to the success value. The mapping function may be synchronous or return a\n * `Promise`; both are handled transparently. If the `AsyncResult` resolves to\n * `Err`, the original `Err` is passed through and the function is not called.\n *\n * If `mapFn` throws or returns a rejecting Promise, the rejection propagates\n * as-is — it is **not** caught and converted into an `Err`. `map` performs no\n * implicit error conversion; write a fallible step as\n * `AsyncResult.flatMap` combined with `AsyncResult.fromThrowable` instead, so\n * that the error type stays explicit.\n *\n * @example\n *\n * ```ts\n * const okValue = AsyncResult.fromPromise(\n * Promise.resolve(5),\n * () => 'failed',\n * );\n *\n * const doubled = await AsyncResult.map(okValue, (value) => value * 2);\n *\n * assert.deepStrictEqual(doubled, Result.ok(10));\n *\n * const double = AsyncResult.map((value: number) => value * 2);\n *\n * const curried = await double(\n * AsyncResult.fromPromise(Promise.resolve(21), () => 'failed'),\n * );\n *\n * assert.deepStrictEqual(curried, Result.ok(42));\n * ```\n *\n * @template S The type of the success value of the input.\n * @template S2 The type of the success value returned by the mapping\n * function.\n * @template E The type of the error value.\n * @param asyncResult The `AsyncResult` to map.\n * @param mapFn The function to apply to the success value if present.\n * @returns A new `AsyncResult<S2, E>`.\n */\nexport function map<S, S2, E>(\n asyncResult: AsyncResult<S, E>,\n mapFn: (value: S) => Promise<S2> | S2,\n): AsyncResult<S2, E>;\n\n// Curried version\n//\n// Unlike `Result.map`, the returned function can simply be generic over `E`:\n// an `AsyncResult` is a `Promise`, not a discriminated union, so there is no\n// literal-narrowing problem that would require the whole-result generic\n// trick used by the synchronous combinators.\nexport function map<S, S2>(\n mapFn: (value: S) => Promise<S2> | S2,\n): <E>(asyncResult: AsyncResult<S, E>) => AsyncResult<S2, E>;\n\nexport function map<S, S2, E>(\n ...args:\n | readonly [\n asyncResult: AsyncResult<S, E>,\n mapFn: (value: S) => Promise<S2> | S2,\n ]\n | readonly [mapFn: (value: S) => Promise<S2> | S2]\n):\n | AsyncResult<S2, E>\n | ((asyncResult: AsyncResult<S, E>) => AsyncResult<S2, E>) {\n switch (args.length) {\n case 2: {\n // Direct version: first argument is asyncResult\n const [asyncResult, mapFn] = args;\n\n return mapImpl(asyncResult, mapFn);\n }\n\n case 1: {\n // Curried version\n const [mapFn] = args;\n\n return (asyncResult: AsyncResult<S, E>) => mapImpl(asyncResult, mapFn);\n }\n }\n}\n\nconst mapImpl = async <S, S2, E>(\n asyncResult: AsyncResult<S, E>,\n mapFn: (value: S) => Promise<S2> | S2,\n): AsyncResult<S2, E> => {\n const result = await asyncResult;\n\n return Result.isErr(result) ? result : Result.ok(await mapFn(result.value));\n};\n"],"names":["Result.isErr","Result.ok"],"mappings":";;;;AA2DO,SAAS,OACX,IAAA,EAQwD;AAC3D,EAAA,QAAQ,KAAK,MAAA;AAAQ,IACnB,KAAK,CAAA,EAAG;AAEN,MAAA,MAAM,CAAC,WAAA,EAAa,KAAK,CAAA,GAAI,IAAA;AAE7B,MAAA,OAAO,OAAA,CAAQ,aAAa,KAAK,CAAA;AAAA,IACnC;AAAA,IAEA,KAAK,CAAA,EAAG;AAEN,MAAA,MAAM,CAAC,KAAK,CAAA,GAAI,IAAA;AAEhB,MAAA,OAAO,CAAC,WAAA,KAAmC,OAAA,CAAQ,WAAA,EAAa,KAAK,CAAA;AAAA,IACvE;AAAA;AAEJ;AAEA,MAAM,OAAA,GAAU,OACd,WAAA,EACA,KAAA,KACuB;AACvB,EAAA,MAAM,SAAS,MAAM,WAAA;AAErB,EAAA,OAAOA,KAAO,CAAM,MAAM,CAAA,GAAI,MAAA,GAASC,EAAO,CAAG,MAAM,KAAA,CAAM,MAAA,CAAO,KAAK,CAAC,CAAA;AAC5E,CAAA;;;;"}
@@ -0,0 +1,34 @@
1
+ import { type AsyncResult } from '../async-result.mjs';
2
+ /**
3
+ * Unwraps an `AsyncResult`, resolving to the success value or a default value
4
+ * if it resolves to `Err`.
5
+ *
6
+ * @example
7
+ *
8
+ * ```ts
9
+ * const okValue = AsyncResult.fromPromise(
10
+ * Promise.resolve(10),
11
+ * () => 'failed',
12
+ * );
13
+ *
14
+ * const errValue = AsyncResult.fromPromise(
15
+ * Promise.reject(new Error('boom')),
16
+ * () => 'failed',
17
+ * );
18
+ *
19
+ * assert.isTrue((await AsyncResult.unwrapOr(okValue, 0)) === 10);
20
+ *
21
+ * assert.isTrue((await AsyncResult.unwrapOr(errValue, 0)) === 0);
22
+ * ```
23
+ *
24
+ * @template S The type of the success value.
25
+ * @template E The type of the error value.
26
+ * @template D The type of the default value.
27
+ * @param asyncResult The `AsyncResult` to unwrap.
28
+ * @param defaultValue The value to resolve to if `asyncResult` resolves to
29
+ * `Err`.
30
+ * @returns A Promise of the success value if `Ok`, otherwise of
31
+ * `defaultValue`.
32
+ */
33
+ export declare const unwrapOr: <S, E, D>(asyncResult: AsyncResult<S, E>, defaultValue: D) => Promise<D | S>;
34
+ //# sourceMappingURL=async-result-unwrap-or.d.mts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"async-result-unwrap-or.d.mts","sourceRoot":"","sources":["../../../../src/functional/async-result/impl/async-result-unwrap-or.mts"],"names":[],"mappings":"AACA,OAAO,EAAE,KAAK,WAAW,EAAE,MAAM,qBAAqB,CAAC;AAEvD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA8BG;AACH,eAAO,MAAM,QAAQ,GAAU,CAAC,EAAE,CAAC,EAAE,CAAC,eACvB,WAAW,CAAC,CAAC,EAAE,CAAC,CAAC,gBAChB,CAAC,KACd,OAAO,CAAC,CAAC,GAAG,CAAC,CAIf,CAAC"}
@@ -0,0 +1,10 @@
1
+ import { isOk } from '../../result/impl/result-is-ok.mjs';
2
+ import '@sindresorhus/is';
3
+
4
+ const unwrapOr = async (asyncResult, defaultValue) => {
5
+ const result = await asyncResult;
6
+ return isOk(result) ? result.value : defaultValue;
7
+ };
8
+
9
+ export { unwrapOr };
10
+ //# sourceMappingURL=async-result-unwrap-or.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"async-result-unwrap-or.mjs","sources":["../../../../src/functional/async-result/impl/async-result-unwrap-or.mts"],"sourcesContent":["import { Result } from '../../result/index.mjs';\nimport { type AsyncResult } from '../async-result.mjs';\n\n/**\n * Unwraps an `AsyncResult`, resolving to the success value or a default value\n * if it resolves to `Err`.\n *\n * @example\n *\n * ```ts\n * const okValue = AsyncResult.fromPromise(\n * Promise.resolve(10),\n * () => 'failed',\n * );\n *\n * const errValue = AsyncResult.fromPromise(\n * Promise.reject(new Error('boom')),\n * () => 'failed',\n * );\n *\n * assert.isTrue((await AsyncResult.unwrapOr(okValue, 0)) === 10);\n *\n * assert.isTrue((await AsyncResult.unwrapOr(errValue, 0)) === 0);\n * ```\n *\n * @template S The type of the success value.\n * @template E The type of the error value.\n * @template D The type of the default value.\n * @param asyncResult The `AsyncResult` to unwrap.\n * @param defaultValue The value to resolve to if `asyncResult` resolves to\n * `Err`.\n * @returns A Promise of the success value if `Ok`, otherwise of\n * `defaultValue`.\n */\nexport const unwrapOr = async <S, E, D>(\n asyncResult: AsyncResult<S, E>,\n defaultValue: D,\n): Promise<D | S> => {\n const result = await asyncResult;\n\n return Result.isOk(result) ? result.value : defaultValue;\n};\n"],"names":["Result.isOk"],"mappings":";;;AAkCO,MAAM,QAAA,GAAW,OACtB,WAAA,EACA,YAAA,KACmB;AACnB,EAAA,MAAM,SAAS,MAAM,WAAA;AAErB,EAAA,OAAOA,IAAO,CAAK,MAAM,CAAA,GAAI,OAAO,KAAA,GAAQ,YAAA;AAC9C;;;;"}
@@ -0,0 +1,8 @@
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';
8
+ //# sourceMappingURL=index.d.mts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.mts","sourceRoot":"","sources":["../../../../src/functional/async-result/impl/index.mts"],"names":[],"mappings":"AAAA,cAAc,6BAA6B,CAAC;AAC5C,cAAc,iCAAiC,CAAC;AAChD,cAAc,mCAAmC,CAAC;AAClD,cAAc,4BAA4B,CAAC;AAC3C,cAAc,wBAAwB,CAAC;AACvC,cAAc,8BAA8B,CAAC;AAC7C,cAAc,aAAa,CAAC"}
@@ -0,0 +1,7 @@
1
+ export { flatMap } from './async-result-flat-map.mjs';
2
+ export { fromPromise } from './async-result-from-promise.mjs';
3
+ export { fromThrowable } from './async-result-from-throwable.mjs';
4
+ export { mapErr } from './async-result-map-err.mjs';
5
+ export { map } from './async-result-map.mjs';
6
+ export { unwrapOr } from './async-result-unwrap-or.mjs';
7
+ //# sourceMappingURL=index.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.mjs","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;"}
@@ -0,0 +1,45 @@
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
+ * Base type for any `AsyncResult`, used for generic constraints. Represents
6
+ * an `AsyncResult` with unknown success and error types.
7
+ */
8
+ export type UnknownAsyncResult = AsyncResult<unknown, unknown>;
9
+ /**
10
+ * Extracts the success value type `S` from a `Result.Ok<S>`, or from an
11
+ * `AsyncResult` resolving to one. Distributes over unions, so an
12
+ * `Ok<S> | Err<E>` union resolves to `S`.
13
+ *
14
+ * @template R The `Result` or `AsyncResult` type to unwrap.
15
+ */
16
+ export type UnwrapOk<R extends UnknownAsyncResult | UnknownResult> = UnwrapOkImpl<SyncResultOf<R>>;
17
+ /**
18
+ * Extracts the error value type `E` from a `Result.Err<E>`, or from an
19
+ * `AsyncResult` resolving to one. Distributes over unions, so an
20
+ * `Ok<S> | Err<E>` union resolves to `E`.
21
+ *
22
+ * @template R The `Result` or `AsyncResult` type to unwrap.
23
+ */
24
+ export type UnwrapErr<R extends UnknownAsyncResult | UnknownResult> = UnwrapErrImpl<SyncResultOf<R>>;
25
+ /**
26
+ * @template R The `Result` or `AsyncResult` type.
27
+ * @internal
28
+ * Resolves an `AsyncResult` to its `Result`, leaving a plain `Result`
29
+ * unchanged. Distributive.
30
+ */
31
+ type SyncResultOf<R> = R extends Promise<infer P> ? P : R;
32
+ /**
33
+ * @template R The `Result` type to unwrap.
34
+ * @internal
35
+ * Distributive extraction of the `Ok` value type.
36
+ */
37
+ type UnwrapOkImpl<R> = R extends Ok<infer S> ? S : never;
38
+ /**
39
+ * @template R The `Result` type to unwrap.
40
+ * @internal
41
+ * Distributive extraction of the `Err` value type.
42
+ */
43
+ type UnwrapErrImpl<R> = R extends Err<infer E> ? E : never;
44
+ export {};
45
+ //# sourceMappingURL=types.d.mts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.d.mts","sourceRoot":"","sources":["../../../../src/functional/async-result/impl/types.mts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,GAAG,EAAE,KAAK,EAAE,EAAE,MAAM,oBAAoB,CAAC;AACvD,OAAO,EAAE,KAAK,aAAa,EAAE,MAAM,wBAAwB,CAAC;AAC5D,OAAO,EAAE,KAAK,WAAW,EAAE,MAAM,qBAAqB,CAAC;AAEvD;;;GAGG;AACH,MAAM,MAAM,kBAAkB,GAAG,WAAW,CAAC,OAAO,EAAE,OAAO,CAAC,CAAC;AAE/D;;;;;;GAMG;AACH,MAAM,MAAM,QAAQ,CAAC,CAAC,SAAS,kBAAkB,GAAG,aAAa,IAC/D,YAAY,CAAC,YAAY,CAAC,CAAC,CAAC,CAAC,CAAC;AAEhC;;;;;;GAMG;AACH,MAAM,MAAM,SAAS,CAAC,CAAC,SAAS,kBAAkB,GAAG,aAAa,IAChE,aAAa,CAAC,YAAY,CAAC,CAAC,CAAC,CAAC,CAAC;AAEjC;;;;;GAKG;AACH,KAAK,YAAY,CAAC,CAAC,IAAI,CAAC,SAAS,OAAO,CAAC,MAAM,CAAC,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;AAE1D;;;;GAIG;AACH,KAAK,YAAY,CAAC,CAAC,IAAI,CAAC,SAAS,EAAE,CAAC,MAAM,CAAC,CAAC,GAAG,CAAC,GAAG,KAAK,CAAC;AAEzD;;;;GAIG;AACH,KAAK,aAAa,CAAC,CAAC,IAAI,CAAC,SAAS,GAAG,CAAC,MAAM,CAAC,CAAC,GAAG,CAAC,GAAG,KAAK,CAAC"}
@@ -0,0 +1,2 @@
1
+
2
+ //# sourceMappingURL=types.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.mjs","sources":[],"sourcesContent":[],"names":[],"mappings":""}
@@ -0,0 +1,2 @@
1
+ export * from './async-result.mjs';
2
+ //# sourceMappingURL=index.d.mts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.mts","sourceRoot":"","sources":["../../../src/functional/async-result/index.mts"],"names":[],"mappings":"AAAA,cAAc,oBAAoB,CAAC"}
@@ -0,0 +1,3 @@
1
+ import * as index from './impl/index.mjs';
2
+ export { index as AsyncResult };
3
+ //# sourceMappingURL=index.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.mjs","sources":[],"sourcesContent":[],"names":[],"mappings":";"}
@@ -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';
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.mts","sourceRoot":"","sources":["../../src/functional/index.mts"],"names":[],"mappings":"AAAA,cAAc,aAAa,CAAC;AAC5B,cAAc,sBAAsB,CAAC;AACrC,cAAc,YAAY,CAAC;AAC3B,cAAc,oBAAoB,CAAC;AACnC,cAAc,4BAA4B,CAAC"}
1
+ {"version":3,"file":"index.d.mts","sourceRoot":"","sources":["../../src/functional/index.mts"],"names":[],"mappings":"AAAA,cAAc,0BAA0B,CAAC;AACzC,cAAc,aAAa,CAAC;AAC5B,cAAc,sBAAsB,CAAC;AACrC,cAAc,YAAY,CAAC;AAC3B,cAAc,oBAAoB,CAAC;AACnC,cAAc,4BAA4B,CAAC"}
@@ -1,9 +1,11 @@
1
+ import * as index from './async-result/impl/index.mjs';
2
+ export { index as AsyncResult };
1
3
  export { match } from './match.mjs';
2
- import * as index from './optional/impl/index.mjs';
3
- export { index as Optional };
4
+ import * as index$1 from './optional/impl/index.mjs';
5
+ export { index$1 as Optional };
4
6
  export { pipe } from './pipe.mjs';
5
- import * as index$1 from './result/impl/index.mjs';
6
- export { index$1 as Result };
7
- import * as index$2 from './ternary-result/impl/index.mjs';
8
- export { index$2 as TernaryResult };
7
+ import * as index$2 from './result/impl/index.mjs';
8
+ export { index$2 as Result };
9
+ import * as index$3 from './ternary-result/impl/index.mjs';
10
+ export { index$3 as TernaryResult };
9
11
  //# sourceMappingURL=index.mjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.mjs","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;"}
1
+ {"version":3,"file":"index.mjs","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;"}
@@ -6,6 +6,7 @@ export * from './optional-is-none.mjs';
6
6
  export * from './optional-is-optional.mjs';
7
7
  export * from './optional-is-some.mjs';
8
8
  export * from './optional-map.mjs';
9
+ export * from './optional-match.mjs';
9
10
  export * from './optional-none.mjs';
10
11
  export * from './optional-or-else.mjs';
11
12
  export * from './optional-some.mjs';
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.mts","sourceRoot":"","sources":["../../../../src/functional/optional/impl/index.mts"],"names":[],"mappings":"AAAA,cAAc,6BAA6B,CAAC;AAC5C,cAAc,uBAAuB,CAAC;AACtC,cAAc,yBAAyB,CAAC;AACxC,cAAc,8BAA8B,CAAC;AAC7C,cAAc,wBAAwB,CAAC;AACvC,cAAc,4BAA4B,CAAC;AAC3C,cAAc,wBAAwB,CAAC;AACvC,cAAc,oBAAoB,CAAC;AACnC,cAAc,qBAAqB,CAAC;AACpC,cAAc,wBAAwB,CAAC;AACvC,cAAc,qBAAqB,CAAC;AACpC,cAAc,4BAA4B,CAAC;AAC3C,cAAc,0BAA0B,CAAC;AACzC,cAAc,6BAA6B,CAAC;AAC5C,cAAc,uBAAuB,CAAC;AACtC,cAAc,oBAAoB,CAAC;AACnC,cAAc,aAAa,CAAC"}
1
+ {"version":3,"file":"index.d.mts","sourceRoot":"","sources":["../../../../src/functional/optional/impl/index.mts"],"names":[],"mappings":"AAAA,cAAc,6BAA6B,CAAC;AAC5C,cAAc,uBAAuB,CAAC;AACtC,cAAc,yBAAyB,CAAC;AACxC,cAAc,8BAA8B,CAAC;AAC7C,cAAc,wBAAwB,CAAC;AACvC,cAAc,4BAA4B,CAAC;AAC3C,cAAc,wBAAwB,CAAC;AACvC,cAAc,oBAAoB,CAAC;AACnC,cAAc,sBAAsB,CAAC;AACrC,cAAc,qBAAqB,CAAC;AACpC,cAAc,wBAAwB,CAAC;AACvC,cAAc,qBAAqB,CAAC;AACpC,cAAc,4BAA4B,CAAC;AAC3C,cAAc,0BAA0B,CAAC;AACzC,cAAc,6BAA6B,CAAC;AAC5C,cAAc,uBAAuB,CAAC;AACtC,cAAc,oBAAoB,CAAC;AACnC,cAAc,aAAa,CAAC"}
@@ -6,6 +6,7 @@ export { isNone } from './optional-is-none.mjs';
6
6
  export { isOptional } from './optional-is-optional.mjs';
7
7
  export { isSome } from './optional-is-some.mjs';
8
8
  export { map } from './optional-map.mjs';
9
+ export { match } from './optional-match.mjs';
9
10
  export { none } from './optional-none.mjs';
10
11
  export { orElse } from './optional-or-else.mjs';
11
12
  export { some } from './optional-some.mjs';
@@ -1 +1 @@
1
- {"version":3,"file":"index.mjs","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;"}
1
+ {"version":3,"file":"index.mjs","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;"}
@@ -0,0 +1,48 @@
1
+ import { type UnknownOptional } from '../optional.mjs';
2
+ import { type Unwrap } from './types.mjs';
3
+ /**
4
+ * Folds an {@link Optional} into a plain value by applying one of two case
5
+ * handlers: `some` if the {@link Optional} contains a value, `none`
6
+ * otherwise. This is the function form of pattern matching on an
7
+ * {@link Optional}.
8
+ *
9
+ * Unlike {@link Optional.map}, the result is not wrapped in an
10
+ * {@link Optional} — both branches produce the final value directly.
11
+ *
12
+ * @example
13
+ *
14
+ * ```ts
15
+ * const doubledOrZero = Optional.match(Optional.some(21), {
16
+ * some: (value) => value * 2,
17
+ * none: () => 0,
18
+ * });
19
+ *
20
+ * assert.deepStrictEqual(doubledOrZero, 42);
21
+ *
22
+ * const matcher = Optional.match({
23
+ * some: (value: number) => `value: ${value}`,
24
+ * none: () => 'none',
25
+ * });
26
+ *
27
+ * assert.deepStrictEqual(matcher(Optional.some(3)), 'value: 3');
28
+ *
29
+ * assert.deepStrictEqual(matcher(Optional.none), 'none');
30
+ * ```
31
+ *
32
+ * @template O The input `UnknownOptional` type.
33
+ * @template S2 The type returned by the `some` case.
34
+ * @template N2 The type returned by the `none` case.
35
+ * @param optional The `Optional` to match on.
36
+ * @param cases An object with a `some` handler receiving the contained value
37
+ * and a `none` handler receiving nothing.
38
+ * @returns The value produced by whichever case handler was applied.
39
+ */
40
+ export declare function match<O extends UnknownOptional, S2, N2>(optional: O, cases: Readonly<{
41
+ some: (value: Unwrap<O>) => S2;
42
+ none: () => N2;
43
+ }>): N2 | S2;
44
+ export declare function match<S, S2, N2>(cases: Readonly<{
45
+ some: (value: S) => S2;
46
+ none: () => N2;
47
+ }>): <O extends UnknownOptional>(optional: Unwrap<O> extends S ? O : never) => N2 | S2;
48
+ //# sourceMappingURL=optional-match.d.mts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"optional-match.d.mts","sourceRoot":"","sources":["../../../../src/functional/optional/impl/optional-match.mts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,eAAe,EAAE,MAAM,iBAAiB,CAAC;AAGvD,OAAO,EAAE,KAAK,MAAM,EAAE,MAAM,aAAa,CAAC;AAE1C;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAoCG;AACH,wBAAgB,KAAK,CAAC,CAAC,SAAS,eAAe,EAAE,EAAE,EAAE,EAAE,EACrD,QAAQ,EAAE,CAAC,EACX,KAAK,EAAE,QAAQ,CAAC;IACd,IAAI,EAAE,CAAC,KAAK,EAAE,MAAM,CAAC,CAAC,CAAC,KAAK,EAAE,CAAC;IAC/B,IAAI,EAAE,MAAM,EAAE,CAAC;CAChB,CAAC,GACD,EAAE,GAAG,EAAE,CAAC;AAQX,wBAAgB,KAAK,CAAC,CAAC,EAAE,EAAE,EAAE,EAAE,EAC7B,KAAK,EAAE,QAAQ,CAAC;IACd,IAAI,EAAE,CAAC,KAAK,EAAE,CAAC,KAAK,EAAE,CAAC;IACvB,IAAI,EAAE,MAAM,EAAE,CAAC;CAChB,CAAC,GACD,CAAC,CAAC,SAAS,eAAe,EAC3B,QAAQ,EAAE,MAAM,CAAC,CAAC,CAAC,SAAS,CAAC,GAAG,CAAC,GAAG,KAAK,KACtC,EAAE,GAAG,EAAE,CAAC"}
@@ -0,0 +1,22 @@
1
+ import { isSome } from './optional-is-some.mjs';
2
+ import { unwrap } from './optional-unwrap.mjs';
3
+
4
+ function match(...args) {
5
+ switch (args.length) {
6
+ case 2: {
7
+ const [optional, cases] = args;
8
+ return matchImpl(optional, cases);
9
+ }
10
+ case 1: {
11
+ const [cases] = args;
12
+ return (optional) => matchImpl(optional, cases);
13
+ }
14
+ }
15
+ }
16
+ const matchImpl = (optional, cases) => isSome(optional) ? (
17
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
18
+ cases.some(unwrap(optional))
19
+ ) : cases.none();
20
+
21
+ export { match };
22
+ //# sourceMappingURL=optional-match.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"optional-match.mjs","sources":["../../../../src/functional/optional/impl/optional-match.mts"],"sourcesContent":["import { type UnknownOptional } from '../optional.mjs';\nimport { isSome } from './optional-is-some.mjs';\nimport { unwrap } from './optional-unwrap.mjs';\nimport { type Unwrap } from './types.mjs';\n\n/**\n * Folds an {@link Optional} into a plain value by applying one of two case\n * handlers: `some` if the {@link Optional} contains a value, `none`\n * otherwise. This is the function form of pattern matching on an\n * {@link Optional}.\n *\n * Unlike {@link Optional.map}, the result is not wrapped in an\n * {@link Optional} — both branches produce the final value directly.\n *\n * @example\n *\n * ```ts\n * const doubledOrZero = Optional.match(Optional.some(21), {\n * some: (value) => value * 2,\n * none: () => 0,\n * });\n *\n * assert.deepStrictEqual(doubledOrZero, 42);\n *\n * const matcher = Optional.match({\n * some: (value: number) => `value: ${value}`,\n * none: () => 'none',\n * });\n *\n * assert.deepStrictEqual(matcher(Optional.some(3)), 'value: 3');\n *\n * assert.deepStrictEqual(matcher(Optional.none), 'none');\n * ```\n *\n * @template O The input `UnknownOptional` type.\n * @template S2 The type returned by the `some` case.\n * @template N2 The type returned by the `none` case.\n * @param optional The `Optional` to match on.\n * @param cases An object with a `some` handler receiving the contained value\n * and a `none` handler receiving nothing.\n * @returns The value produced by whichever case handler was applied.\n */\nexport function match<O extends UnknownOptional, S2, N2>(\n optional: O,\n cases: Readonly<{\n some: (value: Unwrap<O>) => S2;\n none: () => N2;\n }>,\n): N2 | S2;\n\n// Curried version\n//\n// The returned function is generic over the *whole* optional, mirroring the\n// direct overload above; see the comment on the curried overload of\n// `Optional.map` for why the parameter is a conditional type instead of a\n// plain `Optional<S>`.\nexport function match<S, S2, N2>(\n cases: Readonly<{\n some: (value: S) => S2;\n none: () => N2;\n }>,\n): <O extends UnknownOptional>(\n optional: Unwrap<O> extends S ? O : never,\n) => N2 | S2;\n\nexport function match<O extends UnknownOptional, S2, N2>(\n ...args:\n | readonly [\n optional: O,\n cases: Readonly<{\n some: (value: Unwrap<O>) => S2;\n none: () => N2;\n }>,\n ]\n | readonly [\n cases: Readonly<{\n some: (value: Unwrap<O>) => S2;\n none: () => N2;\n }>,\n ]\n): N2 | S2 | ((optional: O) => N2 | S2) {\n switch (args.length) {\n case 2: {\n const [optional, cases] = args;\n\n return matchImpl(optional, cases);\n }\n\n case 1: {\n // Curried version: the only argument is the case handlers\n const [cases] = args;\n\n return (optional: O) => matchImpl(optional, cases);\n }\n }\n}\n\nconst matchImpl = <O extends UnknownOptional, S2, N2>(\n optional: O,\n cases: Readonly<{\n some: (value: Unwrap<O>) => S2;\n none: () => N2;\n }>,\n): N2 | S2 =>\n isSome(optional)\n ? // eslint-disable-next-line @typescript-eslint/no-non-null-assertion\n cases.some(unwrap(optional)!)\n : cases.none();\n"],"names":[],"mappings":";;;AAiEO,SAAS,SACX,IAAA,EAcmC;AACtC,EAAA,QAAQ,KAAK,MAAA;AAAQ,IACnB,KAAK,CAAA,EAAG;AACN,MAAA,MAAM,CAAC,QAAA,EAAU,KAAK,CAAA,GAAI,IAAA;AAE1B,MAAA,OAAO,SAAA,CAAU,UAAU,KAAK,CAAA;AAAA,IAClC;AAAA,IAEA,KAAK,CAAA,EAAG;AAEN,MAAA,MAAM,CAAC,KAAK,CAAA,GAAI,IAAA;AAEhB,MAAA,OAAO,CAAC,QAAA,KAAgB,SAAA,CAAU,QAAA,EAAU,KAAK,CAAA;AAAA,IACnD;AAAA;AAEJ;AAEA,MAAM,SAAA,GAAY,CAChB,QAAA,EACA,KAAA,KAKA,OAAO,QAAQ,CAAA;AAAA;AAAA,EAEX,KAAA,CAAM,IAAA,CAAK,MAAA,CAAO,QAAQ,CAAE;AAAA,IAC5B,MAAM,IAAA,EAAK;;;;"}
@@ -2,6 +2,7 @@ export * from './result-err.mjs';
2
2
  export * from './result-expect-to-be.mjs';
3
3
  export * from './result-flat-map.mjs';
4
4
  export * from './result-fold.mjs';
5
+ export * from './result-from-optional.mjs';
5
6
  export * from './result-from-promise.mjs';
6
7
  export * from './result-from-throwable.mjs';
7
8
  export * from './result-is-err.mjs';
@@ -9,8 +10,11 @@ export * from './result-is-ok.mjs';
9
10
  export * from './result-is-result.mjs';
10
11
  export * from './result-map-err.mjs';
11
12
  export * from './result-map.mjs';
13
+ export * from './result-match.mjs';
12
14
  export * from './result-ok.mjs';
13
15
  export * from './result-or-else.mjs';
16
+ export * from './result-safe-try.mjs';
17
+ export * from './result-safe-unwrap.mjs';
14
18
  export * from './result-swap.mjs';
15
19
  export * from './result-to-optional.mjs';
16
20
  export * from './result-unwrap-err-or.mjs';
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.mts","sourceRoot":"","sources":["../../../../src/functional/result/impl/index.mts"],"names":[],"mappings":"AAAA,cAAc,kBAAkB,CAAC;AACjC,cAAc,2BAA2B,CAAC;AAC1C,cAAc,uBAAuB,CAAC;AACtC,cAAc,mBAAmB,CAAC;AAClC,cAAc,2BAA2B,CAAC;AAC1C,cAAc,6BAA6B,CAAC;AAC5C,cAAc,qBAAqB,CAAC;AACpC,cAAc,oBAAoB,CAAC;AACnC,cAAc,wBAAwB,CAAC;AACvC,cAAc,sBAAsB,CAAC;AACrC,cAAc,kBAAkB,CAAC;AACjC,cAAc,iBAAiB,CAAC;AAChC,cAAc,sBAAsB,CAAC;AACrC,cAAc,mBAAmB,CAAC;AAClC,cAAc,0BAA0B,CAAC;AACzC,cAAc,4BAA4B,CAAC;AAC3C,cAAc,+BAA+B,CAAC;AAC9C,cAAc,yBAAyB,CAAC;AACxC,cAAc,2BAA2B,CAAC;AAC1C,cAAc,wBAAwB,CAAC;AACvC,cAAc,2BAA2B,CAAC;AAC1C,cAAc,kBAAkB,CAAC;AACjC,cAAc,aAAa,CAAC"}
1
+ {"version":3,"file":"index.d.mts","sourceRoot":"","sources":["../../../../src/functional/result/impl/index.mts"],"names":[],"mappings":"AAAA,cAAc,kBAAkB,CAAC;AACjC,cAAc,2BAA2B,CAAC;AAC1C,cAAc,uBAAuB,CAAC;AACtC,cAAc,mBAAmB,CAAC;AAClC,cAAc,4BAA4B,CAAC;AAC3C,cAAc,2BAA2B,CAAC;AAC1C,cAAc,6BAA6B,CAAC;AAC5C,cAAc,qBAAqB,CAAC;AACpC,cAAc,oBAAoB,CAAC;AACnC,cAAc,wBAAwB,CAAC;AACvC,cAAc,sBAAsB,CAAC;AACrC,cAAc,kBAAkB,CAAC;AACjC,cAAc,oBAAoB,CAAC;AACnC,cAAc,iBAAiB,CAAC;AAChC,cAAc,sBAAsB,CAAC;AACrC,cAAc,uBAAuB,CAAC;AACtC,cAAc,0BAA0B,CAAC;AACzC,cAAc,mBAAmB,CAAC;AAClC,cAAc,0BAA0B,CAAC;AACzC,cAAc,4BAA4B,CAAC;AAC3C,cAAc,+BAA+B,CAAC;AAC9C,cAAc,yBAAyB,CAAC;AACxC,cAAc,2BAA2B,CAAC;AAC1C,cAAc,wBAAwB,CAAC;AACvC,cAAc,2BAA2B,CAAC;AAC1C,cAAc,kBAAkB,CAAC;AACjC,cAAc,aAAa,CAAC"}
@@ -2,6 +2,7 @@ export { err } from './result-err.mjs';
2
2
  export { expectToBe } from './result-expect-to-be.mjs';
3
3
  export { flatMap } from './result-flat-map.mjs';
4
4
  export { fold } from './result-fold.mjs';
5
+ export { fromOptional } from './result-from-optional.mjs';
5
6
  export { fromPromise } from './result-from-promise.mjs';
6
7
  export { fromThrowable } from './result-from-throwable.mjs';
7
8
  export { isErr } from './result-is-err.mjs';
@@ -9,8 +10,11 @@ export { isOk } from './result-is-ok.mjs';
9
10
  export { isResult } from './result-is-result.mjs';
10
11
  export { mapErr } from './result-map-err.mjs';
11
12
  export { map } from './result-map.mjs';
13
+ export { match } from './result-match.mjs';
12
14
  export { ok } from './result-ok.mjs';
13
15
  export { orElse } from './result-or-else.mjs';
16
+ export { safeTry } from './result-safe-try.mjs';
17
+ export { safeUnwrap } from './result-safe-unwrap.mjs';
14
18
  export { swap } from './result-swap.mjs';
15
19
  export { toOptional } from './result-to-optional.mjs';
16
20
  export { unwrapErrOr } from './result-unwrap-err-or.mjs';
@@ -1 +1 @@
1
- {"version":3,"file":"index.mjs","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;"}
1
+ {"version":3,"file":"index.mjs","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;"}
@@ -0,0 +1,41 @@
1
+ import { Optional, type UnknownOptional } from '../../optional/index.mjs';
2
+ import { type Result } from '../result.mjs';
3
+ /**
4
+ * Converts an {@link Optional} to a `Result` (the inverse of
5
+ * {@link Result.toOptional}, and the equivalent of Rust's `Option::ok_or`).
6
+ *
7
+ * If the {@link Optional} is `Some`, returns `Ok` with the contained value.
8
+ * If it is `None`, returns `Err` with the provided error value.
9
+ *
10
+ * @example
11
+ *
12
+ * ```ts
13
+ * assert.deepStrictEqual(
14
+ * Result.fromOptional(Optional.some(42), 'missing'),
15
+ * Result.ok(42),
16
+ * );
17
+ *
18
+ * assert.deepStrictEqual(
19
+ * Result.fromOptional(Optional.none, 'missing'),
20
+ * Result.err('missing'),
21
+ * );
22
+ *
23
+ * const withMissingError = Result.fromOptional('missing');
24
+ *
25
+ * assert.deepStrictEqual(withMissingError(Optional.some(1)), Result.ok(1));
26
+ *
27
+ * assert.deepStrictEqual(
28
+ * withMissingError(Optional.none),
29
+ * Result.err('missing'),
30
+ * );
31
+ * ```
32
+ *
33
+ * @template O The input `UnknownOptional` type.
34
+ * @template E The type of the error value used for the `None` case.
35
+ * @param optional The `Optional` to convert.
36
+ * @param error The error value to use if the `Optional` is `None`.
37
+ * @returns `Ok` with the contained value, or `Err` with `error`.
38
+ */
39
+ export declare function fromOptional<O extends UnknownOptional, const E>(optional: O, error: E): Result<Optional.Unwrap<O>, E>;
40
+ export declare function fromOptional<const E>(error: E): <O extends UnknownOptional>(optional: O) => Result<Optional.Unwrap<O>, E>;
41
+ //# sourceMappingURL=result-from-optional.d.mts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"result-from-optional.d.mts","sourceRoot":"","sources":["../../../../src/functional/result/impl/result-from-optional.mts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,KAAK,eAAe,EAAE,MAAM,0BAA0B,CAAC;AAC1E,OAAO,EAAE,KAAK,MAAM,EAAE,MAAM,eAAe,CAAC;AAI5C;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAmCG;AACH,wBAAgB,YAAY,CAAC,CAAC,SAAS,eAAe,EAAE,KAAK,CAAC,CAAC,EAC7D,QAAQ,EAAE,CAAC,EACX,KAAK,EAAE,CAAC,GACP,MAAM,CAAC,QAAQ,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC;AAGjC,wBAAgB,YAAY,CAAC,KAAK,CAAC,CAAC,EAClC,KAAK,EAAE,CAAC,GACP,CAAC,CAAC,SAAS,eAAe,EAAE,QAAQ,EAAE,CAAC,KAAK,MAAM,CAAC,QAAQ,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC"}
@@ -0,0 +1,24 @@
1
+ import { isSome } from '../../optional/impl/optional-is-some.mjs';
2
+ import { unwrap } from '../../optional/impl/optional-unwrap.mjs';
3
+ import { err } from './result-err.mjs';
4
+ import { ok } from './result-ok.mjs';
5
+
6
+ function fromOptional(...args) {
7
+ switch (args.length) {
8
+ case 2: {
9
+ const [optional, error] = args;
10
+ return fromOptionalImpl(optional, error);
11
+ }
12
+ case 1: {
13
+ const [error] = args;
14
+ return (optional) => fromOptionalImpl(optional, error);
15
+ }
16
+ }
17
+ }
18
+ const fromOptionalImpl = (optional, error) => isSome(optional) ? (
19
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
20
+ ok(unwrap(optional))
21
+ ) : err(error);
22
+
23
+ export { fromOptional };
24
+ //# sourceMappingURL=result-from-optional.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"result-from-optional.mjs","sources":["../../../../src/functional/result/impl/result-from-optional.mts"],"sourcesContent":["import { Optional, type UnknownOptional } from '../../optional/index.mjs';\nimport { type Result } from '../result.mjs';\nimport { err } from './result-err.mjs';\nimport { ok } from './result-ok.mjs';\n\n/**\n * Converts an {@link Optional} to a `Result` (the inverse of\n * {@link Result.toOptional}, and the equivalent of Rust's `Option::ok_or`).\n *\n * If the {@link Optional} is `Some`, returns `Ok` with the contained value.\n * If it is `None`, returns `Err` with the provided error value.\n *\n * @example\n *\n * ```ts\n * assert.deepStrictEqual(\n * Result.fromOptional(Optional.some(42), 'missing'),\n * Result.ok(42),\n * );\n *\n * assert.deepStrictEqual(\n * Result.fromOptional(Optional.none, 'missing'),\n * Result.err('missing'),\n * );\n *\n * const withMissingError = Result.fromOptional('missing');\n *\n * assert.deepStrictEqual(withMissingError(Optional.some(1)), Result.ok(1));\n *\n * assert.deepStrictEqual(\n * withMissingError(Optional.none),\n * Result.err('missing'),\n * );\n * ```\n *\n * @template O The input `UnknownOptional` type.\n * @template E The type of the error value used for the `None` case.\n * @param optional The `Optional` to convert.\n * @param error The error value to use if the `Optional` is `None`.\n * @returns `Ok` with the contained value, or `Err` with `error`.\n */\nexport function fromOptional<O extends UnknownOptional, const E>(\n optional: O,\n error: E,\n): Result<Optional.Unwrap<O>, E>;\n\n// Curried version\nexport function fromOptional<const E>(\n error: E,\n): <O extends UnknownOptional>(optional: O) => Result<Optional.Unwrap<O>, E>;\n\nexport function fromOptional<O extends UnknownOptional, const E>(\n ...args: readonly [optional: O, error: E] | readonly [error: E]\n):\n | Result<Optional.Unwrap<O>, E>\n | ((optional: O) => Result<Optional.Unwrap<O>, E>) {\n switch (args.length) {\n case 2: {\n const [optional, error] = args;\n\n return fromOptionalImpl(optional, error);\n }\n\n case 1: {\n // Curried version: the only argument is the error value\n const [error] = args;\n\n return (optional: O) => fromOptionalImpl(optional, error);\n }\n }\n}\n\nconst fromOptionalImpl = <O extends UnknownOptional, E>(\n optional: O,\n error: E,\n): Result<Optional.Unwrap<O>, E> =>\n Optional.isSome(optional)\n ? // eslint-disable-next-line @typescript-eslint/no-non-null-assertion\n ok(Optional.unwrap(optional)!)\n : err(error);\n"],"names":["Optional.isSome","Optional.unwrap"],"mappings":";;;;;AAmDO,SAAS,gBACX,IAAA,EAGgD;AACnD,EAAA,QAAQ,KAAK,MAAA;AAAQ,IACnB,KAAK,CAAA,EAAG;AACN,MAAA,MAAM,CAAC,QAAA,EAAU,KAAK,CAAA,GAAI,IAAA;AAE1B,MAAA,OAAO,gBAAA,CAAiB,UAAU,KAAK,CAAA;AAAA,IACzC;AAAA,IAEA,KAAK,CAAA,EAAG;AAEN,MAAA,MAAM,CAAC,KAAK,CAAA,GAAI,IAAA;AAEhB,MAAA,OAAO,CAAC,QAAA,KAAgB,gBAAA,CAAiB,QAAA,EAAU,KAAK,CAAA;AAAA,IAC1D;AAAA;AAEJ;AAEA,MAAM,mBAAmB,CACvB,QAAA,EACA,KAAA,KAEAA,OAAgB,QAAQ,CAAA;AAAA;AAAA,EAEpB,EAAA,CAAGC,MAAS,CAAO,QAAQ,CAAE;AAAA,IAC7B,IAAI,KAAK,CAAA;;;;"}
@@ -11,6 +11,11 @@ type UnwrapPromise<P extends Promise<unknown>> = P extends Promise<infer V> ? V
11
11
  * input Promise rejects, the `Result` will be `Err` with the rejection
12
12
  * reason.
13
13
  *
14
+ * The error channel is `unknown`, since the rejection reason is carried
15
+ * through untouched. For a concrete error type, use `AsyncResult.fromPromise`
16
+ * with a `mapError` function; called without one it behaves exactly as this
17
+ * function does.
18
+ *
14
19
  * @example
15
20
  *
16
21
  * ```ts
@@ -1 +1 @@
1
- {"version":3,"file":"result-from-promise.d.mts","sourceRoot":"","sources":["../../../../src/functional/result/impl/result-from-promise.mts"],"names":[],"mappings":"AACA,OAAO,EAAE,KAAK,MAAM,EAAE,MAAM,eAAe,CAAC;AAI5C;;;;GAIG;AACH,KAAK,aAAa,CAAC,CAAC,SAAS,OAAO,CAAC,OAAO,CAAC,IAC3C,CAAC,SAAS,OAAO,CAAC,MAAM,CAAC,CAAC,GAAG,CAAC,GAAG,KAAK,CAAC;AAEzC;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2BG;AACH,eAAO,MAAM,WAAW,GAAI,CAAC,SAAS,OAAO,CAAC,OAAO,CAAC,WAC3C,CAAC,KACT,OAAO,CAAC,MAAM,CAAC,aAAa,CAAC,CAAC,CAAC,EAAE,OAAO,CAAC,CAEmB,CAAC"}
1
+ {"version":3,"file":"result-from-promise.d.mts","sourceRoot":"","sources":["../../../../src/functional/result/impl/result-from-promise.mts"],"names":[],"mappings":"AACA,OAAO,EAAE,KAAK,MAAM,EAAE,MAAM,eAAe,CAAC;AAI5C;;;;GAIG;AACH,KAAK,aAAa,CAAC,CAAC,SAAS,OAAO,CAAC,OAAO,CAAC,IAC3C,CAAC,SAAS,OAAO,CAAC,MAAM,CAAC,CAAC,GAAG,CAAC,GAAG,KAAK,CAAC;AAEzC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAgCG;AACH,eAAO,MAAM,WAAW,GAAI,CAAC,SAAS,OAAO,CAAC,OAAO,CAAC,WAC3C,CAAC,KACT,OAAO,CAAC,MAAM,CAAC,aAAa,CAAC,CAAC,CAAC,EAAE,OAAO,CAAC,CAEmB,CAAC"}
@@ -1 +1 @@
1
- {"version":3,"file":"result-from-promise.mjs","sources":["../../../../src/functional/result/impl/result-from-promise.mts"],"sourcesContent":["import { type Ok } from '../../../types.mjs';\nimport { type Result } from '../result.mjs';\nimport { err } from './result-err.mjs';\nimport { ok } from './result-ok.mjs';\n\n/**\n * @template P The Promise type.\n * @internal\n * Utility type to extract the resolved value type from a Promise.\n */\ntype UnwrapPromise<P extends Promise<unknown>> =\n P extends Promise<infer V> ? V : never;\n\n/**\n * Converts a Promise into a Promise that resolves to a `Result`. If the input\n * Promise resolves, the `Result` will be `Ok` with the resolved value. If the\n * input Promise rejects, the `Result` will be `Err` with the rejection\n * reason.\n *\n * @example\n *\n * ```ts\n * const successPromise = Result.fromPromise(Promise.resolve('ok'));\n *\n * const failurePromise = Result.fromPromise(\n * Promise.reject(new Error('fail')),\n * );\n *\n * const resolved = await successPromise;\n *\n * const rejected = await failurePromise;\n *\n * assert.deepStrictEqual(resolved, Result.ok('ok'));\n *\n * assert.isTrue(Result.isErr(rejected));\n * ```\n *\n * @template P The type of the input Promise.\n * @param promise The Promise to convert.\n * @returns A Promise that resolves to `Result<UnwrapPromise<P>, unknown>`.\n */\nexport const fromPromise = <P extends Promise<unknown>>(\n promise: P,\n): Promise<Result<UnwrapPromise<P>, unknown>> =>\n // eslint-disable-next-line total-functions/no-unsafe-type-assertion\n promise.then((v) => ok(v) as Ok<UnwrapPromise<P>>).catch(err);\n"],"names":[],"mappings":";;;AAyCO,MAAM,cAAc,CACzB,OAAA;AAAA;AAAA,EAGA,OAAA,CAAQ,KAAK,CAAC,CAAA,KAAM,GAAG,CAAC,CAAyB,CAAA,CAAE,KAAA,CAAM,GAAG;AAAA;;;;"}
1
+ {"version":3,"file":"result-from-promise.mjs","sources":["../../../../src/functional/result/impl/result-from-promise.mts"],"sourcesContent":["import { type Ok } from '../../../types.mjs';\nimport { type Result } from '../result.mjs';\nimport { err } from './result-err.mjs';\nimport { ok } from './result-ok.mjs';\n\n/**\n * @template P The Promise type.\n * @internal\n * Utility type to extract the resolved value type from a Promise.\n */\ntype UnwrapPromise<P extends Promise<unknown>> =\n P extends Promise<infer V> ? V : never;\n\n/**\n * Converts a Promise into a Promise that resolves to a `Result`. If the input\n * Promise resolves, the `Result` will be `Ok` with the resolved value. If the\n * input Promise rejects, the `Result` will be `Err` with the rejection\n * reason.\n *\n * The error channel is `unknown`, since the rejection reason is carried\n * through untouched. For a concrete error type, use `AsyncResult.fromPromise`\n * with a `mapError` function; called without one it behaves exactly as this\n * function does.\n *\n * @example\n *\n * ```ts\n * const successPromise = Result.fromPromise(Promise.resolve('ok'));\n *\n * const failurePromise = Result.fromPromise(\n * Promise.reject(new Error('fail')),\n * );\n *\n * const resolved = await successPromise;\n *\n * const rejected = await failurePromise;\n *\n * assert.deepStrictEqual(resolved, Result.ok('ok'));\n *\n * assert.isTrue(Result.isErr(rejected));\n * ```\n *\n * @template P The type of the input Promise.\n * @param promise The Promise to convert.\n * @returns A Promise that resolves to `Result<UnwrapPromise<P>, unknown>`.\n */\nexport const fromPromise = <P extends Promise<unknown>>(\n promise: P,\n): Promise<Result<UnwrapPromise<P>, unknown>> =>\n // eslint-disable-next-line total-functions/no-unsafe-type-assertion\n promise.then((v) => ok(v) as Ok<UnwrapPromise<P>>).catch(err);\n"],"names":[],"mappings":";;;AA8CO,MAAM,cAAc,CACzB,OAAA;AAAA;AAAA,EAGA,OAAA,CAAQ,KAAK,CAAC,CAAA,KAAM,GAAG,CAAC,CAAyB,CAAA,CAAE,KAAA,CAAM,GAAG;AAAA;;;;"}
@@ -0,0 +1,47 @@
1
+ import { type UnknownResult } from '../result.mjs';
2
+ import { type UnwrapErr, type UnwrapOk } from './types.mjs';
3
+ /**
4
+ * Folds a `Result` into a plain value by applying one of two case handlers:
5
+ * `ok` if the `Result` is `Ok`, `err` otherwise. This is the function form of
6
+ * pattern matching on a `Result`.
7
+ *
8
+ * Unlike {@link Result.fold}, which maps both sides and returns another
9
+ * `Result`, `match` unwraps: both branches produce the final value directly.
10
+ *
11
+ * @example
12
+ *
13
+ * ```ts
14
+ * const doubledOrZero = Result.match(Result.ok(21), {
15
+ * ok: (value) => value * 2,
16
+ * err: () => 0,
17
+ * });
18
+ *
19
+ * assert.deepStrictEqual(doubledOrZero, 42);
20
+ *
21
+ * const matcher = Result.match({
22
+ * ok: (value: number) => value + 1,
23
+ * err: (error: string) => error.length,
24
+ * });
25
+ *
26
+ * assert.deepStrictEqual(matcher(Result.ok(1)), 2);
27
+ *
28
+ * assert.deepStrictEqual(matcher(Result.err('oops')), 4);
29
+ * ```
30
+ *
31
+ * @template R The input `UnknownResult` type.
32
+ * @template S2 The type returned by the `ok` case.
33
+ * @template E2 The type returned by the `err` case.
34
+ * @param result The `Result` to match on.
35
+ * @param cases An object with an `ok` handler receiving the success value and
36
+ * an `err` handler receiving the error value.
37
+ * @returns The value produced by whichever case handler was applied.
38
+ */
39
+ export declare function match<R extends UnknownResult, S2, E2>(result: R, cases: Readonly<{
40
+ ok: (value: UnwrapOk<R>) => S2;
41
+ err: (error: UnwrapErr<R>) => E2;
42
+ }>): E2 | S2;
43
+ export declare function match<S, E, S2, E2>(cases: Readonly<{
44
+ ok: (value: S) => S2;
45
+ err: (error: E) => E2;
46
+ }>): <R extends UnknownResult>(result: UnwrapOk<R> extends S ? (UnwrapErr<R> extends E ? R : never) : never) => E2 | S2;
47
+ //# sourceMappingURL=result-match.d.mts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"result-match.d.mts","sourceRoot":"","sources":["../../../../src/functional/result/impl/result-match.mts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,aAAa,EAAE,MAAM,eAAe,CAAC;AAEnD,OAAO,EAAE,KAAK,SAAS,EAAE,KAAK,QAAQ,EAAE,MAAM,aAAa,CAAC;AAE5D;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAmCG;AACH,wBAAgB,KAAK,CAAC,CAAC,SAAS,aAAa,EAAE,EAAE,EAAE,EAAE,EACnD,MAAM,EAAE,CAAC,EACT,KAAK,EAAE,QAAQ,CAAC;IACd,EAAE,EAAE,CAAC,KAAK,EAAE,QAAQ,CAAC,CAAC,CAAC,KAAK,EAAE,CAAC;IAC/B,GAAG,EAAE,CAAC,KAAK,EAAE,SAAS,CAAC,CAAC,CAAC,KAAK,EAAE,CAAC;CAClC,CAAC,GACD,EAAE,GAAG,EAAE,CAAC;AASX,wBAAgB,KAAK,CAAC,CAAC,EAAE,CAAC,EAAE,EAAE,EAAE,EAAE,EAChC,KAAK,EAAE,QAAQ,CAAC;IACd,EAAE,EAAE,CAAC,KAAK,EAAE,CAAC,KAAK,EAAE,CAAC;IACrB,GAAG,EAAE,CAAC,KAAK,EAAE,CAAC,KAAK,EAAE,CAAC;CACvB,CAAC,GACD,CAAC,CAAC,SAAS,aAAa,EACzB,MAAM,EAAE,QAAQ,CAAC,CAAC,CAAC,SAAS,CAAC,GAAG,CAAC,SAAS,CAAC,CAAC,CAAC,SAAS,CAAC,GAAG,CAAC,GAAG,KAAK,CAAC,GAAG,KAAK,KACzE,EAAE,GAAG,EAAE,CAAC"}