@nlozgachev/pipelined 0.11.0 → 0.13.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 (193) hide show
  1. package/LICENCE +28 -0
  2. package/README.md +1 -1
  3. package/{types/src/Types/NonEmptyList.d.ts → dist/NonEmptyList-BlGFjor5.d.mts} +4 -3
  4. package/dist/NonEmptyList-BlGFjor5.d.ts +30 -0
  5. package/dist/Task-BB8Wmc1J.d.ts +677 -0
  6. package/dist/Task-ChKyH0pF.d.mts +677 -0
  7. package/dist/chunk-DBIC62UV.mjs +6 -0
  8. package/dist/chunk-QPTGO5AS.mjs +150 -0
  9. package/dist/composition.d.mts +495 -0
  10. package/dist/composition.d.ts +495 -0
  11. package/dist/composition.js +188 -0
  12. package/dist/composition.mjs +135 -0
  13. package/dist/core.d.mts +2170 -0
  14. package/dist/core.d.ts +2170 -0
  15. package/dist/core.js +698 -0
  16. package/dist/core.mjs +517 -0
  17. package/dist/types.d.mts +54 -0
  18. package/{types/src/Types/Brand.d.ts → dist/types.d.ts} +6 -4
  19. package/dist/types.js +41 -0
  20. package/dist/types.mjs +14 -0
  21. package/dist/utils.d.mts +1285 -0
  22. package/dist/utils.d.ts +1285 -0
  23. package/dist/utils.js +722 -0
  24. package/dist/utils.mjs +553 -0
  25. package/package.json +63 -69
  26. package/esm/mod.js +0 -3
  27. package/esm/package.json +0 -3
  28. package/esm/src/Composition/compose.js +0 -3
  29. package/esm/src/Composition/converge.js +0 -3
  30. package/esm/src/Composition/curry.js +0 -42
  31. package/esm/src/Composition/flip.js +0 -20
  32. package/esm/src/Composition/flow.js +0 -8
  33. package/esm/src/Composition/fn.js +0 -85
  34. package/esm/src/Composition/index.js +0 -13
  35. package/esm/src/Composition/juxt.js +0 -3
  36. package/esm/src/Composition/memoize.js +0 -66
  37. package/esm/src/Composition/not.js +0 -25
  38. package/esm/src/Composition/on.js +0 -12
  39. package/esm/src/Composition/pipe.js +0 -3
  40. package/esm/src/Composition/tap.js +0 -33
  41. package/esm/src/Composition/uncurry.js +0 -32
  42. package/esm/src/Core/Deferred.js +0 -30
  43. package/esm/src/Core/InternalTypes.js +0 -1
  44. package/esm/src/Core/Lens.js +0 -98
  45. package/esm/src/Core/Logged.js +0 -111
  46. package/esm/src/Core/Option.js +0 -191
  47. package/esm/src/Core/Optional.js +0 -160
  48. package/esm/src/Core/Predicate.js +0 -133
  49. package/esm/src/Core/Reader.js +0 -134
  50. package/esm/src/Core/Refinement.js +0 -115
  51. package/esm/src/Core/RemoteData.js +0 -211
  52. package/esm/src/Core/Result.js +0 -170
  53. package/esm/src/Core/State.js +0 -181
  54. package/esm/src/Core/Task.js +0 -223
  55. package/esm/src/Core/TaskOption.js +0 -106
  56. package/esm/src/Core/TaskResult.js +0 -127
  57. package/esm/src/Core/TaskValidation.js +0 -128
  58. package/esm/src/Core/These.js +0 -245
  59. package/esm/src/Core/Validation.js +0 -212
  60. package/esm/src/Core/index.js +0 -17
  61. package/esm/src/Types/Brand.js +0 -28
  62. package/esm/src/Types/NonEmptyList.js +0 -14
  63. package/esm/src/Types/index.js +0 -2
  64. package/esm/src/Utils/Arr.js +0 -570
  65. package/esm/src/Utils/Num.js +0 -124
  66. package/esm/src/Utils/Rec.js +0 -215
  67. package/esm/src/Utils/Str.js +0 -134
  68. package/esm/src/Utils/index.js +0 -4
  69. package/script/mod.js +0 -19
  70. package/script/package.json +0 -3
  71. package/script/src/Composition/compose.js +0 -6
  72. package/script/src/Composition/converge.js +0 -6
  73. package/script/src/Composition/curry.js +0 -48
  74. package/script/src/Composition/flip.js +0 -24
  75. package/script/src/Composition/flow.js +0 -11
  76. package/script/src/Composition/fn.js +0 -98
  77. package/script/src/Composition/index.js +0 -29
  78. package/script/src/Composition/juxt.js +0 -6
  79. package/script/src/Composition/memoize.js +0 -71
  80. package/script/src/Composition/not.js +0 -29
  81. package/script/src/Composition/on.js +0 -16
  82. package/script/src/Composition/pipe.js +0 -6
  83. package/script/src/Composition/tap.js +0 -37
  84. package/script/src/Composition/uncurry.js +0 -38
  85. package/script/src/Core/Deferred.js +0 -33
  86. package/script/src/Core/InternalTypes.js +0 -2
  87. package/script/src/Core/Lens.js +0 -101
  88. package/script/src/Core/Logged.js +0 -114
  89. package/script/src/Core/Option.js +0 -194
  90. package/script/src/Core/Optional.js +0 -163
  91. package/script/src/Core/Predicate.js +0 -136
  92. package/script/src/Core/Reader.js +0 -137
  93. package/script/src/Core/Refinement.js +0 -118
  94. package/script/src/Core/RemoteData.js +0 -214
  95. package/script/src/Core/Result.js +0 -173
  96. package/script/src/Core/State.js +0 -184
  97. package/script/src/Core/Task.js +0 -226
  98. package/script/src/Core/TaskOption.js +0 -109
  99. package/script/src/Core/TaskResult.js +0 -130
  100. package/script/src/Core/TaskValidation.js +0 -131
  101. package/script/src/Core/These.js +0 -248
  102. package/script/src/Core/Validation.js +0 -215
  103. package/script/src/Core/index.js +0 -33
  104. package/script/src/Types/Brand.js +0 -31
  105. package/script/src/Types/NonEmptyList.js +0 -18
  106. package/script/src/Types/index.js +0 -18
  107. package/script/src/Utils/Arr.js +0 -573
  108. package/script/src/Utils/Num.js +0 -127
  109. package/script/src/Utils/Rec.js +0 -218
  110. package/script/src/Utils/Str.js +0 -137
  111. package/script/src/Utils/index.js +0 -20
  112. package/types/mod.d.ts +0 -4
  113. package/types/mod.d.ts.map +0 -1
  114. package/types/src/Composition/compose.d.ts +0 -33
  115. package/types/src/Composition/compose.d.ts.map +0 -1
  116. package/types/src/Composition/converge.d.ts +0 -21
  117. package/types/src/Composition/converge.d.ts.map +0 -1
  118. package/types/src/Composition/curry.d.ts +0 -43
  119. package/types/src/Composition/curry.d.ts.map +0 -1
  120. package/types/src/Composition/flip.d.ts +0 -21
  121. package/types/src/Composition/flip.d.ts.map +0 -1
  122. package/types/src/Composition/flow.d.ts +0 -56
  123. package/types/src/Composition/flow.d.ts.map +0 -1
  124. package/types/src/Composition/fn.d.ts +0 -76
  125. package/types/src/Composition/fn.d.ts.map +0 -1
  126. package/types/src/Composition/index.d.ts +0 -14
  127. package/types/src/Composition/index.d.ts.map +0 -1
  128. package/types/src/Composition/juxt.d.ts +0 -18
  129. package/types/src/Composition/juxt.d.ts.map +0 -1
  130. package/types/src/Composition/memoize.d.ts +0 -46
  131. package/types/src/Composition/memoize.d.ts.map +0 -1
  132. package/types/src/Composition/not.d.ts +0 -26
  133. package/types/src/Composition/not.d.ts.map +0 -1
  134. package/types/src/Composition/on.d.ts +0 -13
  135. package/types/src/Composition/on.d.ts.map +0 -1
  136. package/types/src/Composition/pipe.d.ts +0 -56
  137. package/types/src/Composition/pipe.d.ts.map +0 -1
  138. package/types/src/Composition/tap.d.ts +0 -31
  139. package/types/src/Composition/tap.d.ts.map +0 -1
  140. package/types/src/Composition/uncurry.d.ts +0 -54
  141. package/types/src/Composition/uncurry.d.ts.map +0 -1
  142. package/types/src/Core/Deferred.d.ts +0 -49
  143. package/types/src/Core/Deferred.d.ts.map +0 -1
  144. package/types/src/Core/InternalTypes.d.ts +0 -23
  145. package/types/src/Core/InternalTypes.d.ts.map +0 -1
  146. package/types/src/Core/Lens.d.ts +0 -118
  147. package/types/src/Core/Lens.d.ts.map +0 -1
  148. package/types/src/Core/Logged.d.ts +0 -126
  149. package/types/src/Core/Logged.d.ts.map +0 -1
  150. package/types/src/Core/Option.d.ts +0 -209
  151. package/types/src/Core/Option.d.ts.map +0 -1
  152. package/types/src/Core/Optional.d.ts +0 -158
  153. package/types/src/Core/Optional.d.ts.map +0 -1
  154. package/types/src/Core/Predicate.d.ts +0 -161
  155. package/types/src/Core/Predicate.d.ts.map +0 -1
  156. package/types/src/Core/Reader.d.ts +0 -156
  157. package/types/src/Core/Reader.d.ts.map +0 -1
  158. package/types/src/Core/Refinement.d.ts +0 -138
  159. package/types/src/Core/Refinement.d.ts.map +0 -1
  160. package/types/src/Core/RemoteData.d.ts +0 -197
  161. package/types/src/Core/RemoteData.d.ts.map +0 -1
  162. package/types/src/Core/Result.d.ts +0 -182
  163. package/types/src/Core/Result.d.ts.map +0 -1
  164. package/types/src/Core/State.d.ts +0 -192
  165. package/types/src/Core/State.d.ts.map +0 -1
  166. package/types/src/Core/Task.d.ts +0 -219
  167. package/types/src/Core/Task.d.ts.map +0 -1
  168. package/types/src/Core/TaskOption.d.ts +0 -121
  169. package/types/src/Core/TaskOption.d.ts.map +0 -1
  170. package/types/src/Core/TaskResult.d.ts +0 -119
  171. package/types/src/Core/TaskResult.d.ts.map +0 -1
  172. package/types/src/Core/TaskValidation.d.ts +0 -144
  173. package/types/src/Core/TaskValidation.d.ts.map +0 -1
  174. package/types/src/Core/These.d.ts +0 -225
  175. package/types/src/Core/These.d.ts.map +0 -1
  176. package/types/src/Core/Validation.d.ts +0 -203
  177. package/types/src/Core/Validation.d.ts.map +0 -1
  178. package/types/src/Core/index.d.ts +0 -18
  179. package/types/src/Core/index.d.ts.map +0 -1
  180. package/types/src/Types/Brand.d.ts.map +0 -1
  181. package/types/src/Types/NonEmptyList.d.ts.map +0 -1
  182. package/types/src/Types/index.d.ts +0 -3
  183. package/types/src/Types/index.d.ts.map +0 -1
  184. package/types/src/Utils/Arr.d.ts +0 -403
  185. package/types/src/Utils/Arr.d.ts.map +0 -1
  186. package/types/src/Utils/Num.d.ts +0 -110
  187. package/types/src/Utils/Num.d.ts.map +0 -1
  188. package/types/src/Utils/Rec.d.ts +0 -143
  189. package/types/src/Utils/Rec.d.ts.map +0 -1
  190. package/types/src/Utils/Str.d.ts +0 -128
  191. package/types/src/Utils/Str.d.ts.map +0 -1
  192. package/types/src/Utils/index.d.ts +0 -5
  193. package/types/src/Utils/index.d.ts.map +0 -1
@@ -1,29 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.not = void 0;
4
- /**
5
- * Negates a predicate function.
6
- * Returns a new predicate that returns true when the original returns false, and vice versa.
7
- *
8
- * @example
9
- * ```ts
10
- * const isEven = (n: number) => n % 2 === 0;
11
- * const isOdd = not(isEven);
12
- *
13
- * isOdd(3); // true
14
- * isOdd(4); // false
15
- *
16
- * // With array methods
17
- * const numbers = [1, 2, 3, 4, 5];
18
- * numbers.filter(not(isEven)); // [1, 3, 5]
19
- *
20
- * // In pipelines
21
- * pipe(
22
- * users,
23
- * Array.filter(not(isAdmin)),
24
- * Array.map(u => u.name)
25
- * );
26
- * ```
27
- */
28
- const not = (predicate) => (...args) => !predicate(...args);
29
- exports.not = not;
@@ -1,16 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.on = void 0;
4
- /**
5
- * Applies a projection to both arguments of a binary function before calling it.
6
- * Most useful for building comparators and equality checks over projected values.
7
- *
8
- * @example
9
- * ```ts
10
- * const byLength = on((a: number, b: number) => a - b, (s: string) => s.length);
11
- *
12
- * ["banana", "fig", "apple"].sort(byLength); // ["fig", "apple", "banana"]
13
- * ```
14
- */
15
- const on = (f, g) => (a, b) => f(g(a), g(b));
16
- exports.on = on;
@@ -1,6 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.pipe = pipe;
4
- function pipe(a, ...fns) {
5
- return fns.reduce((acc, fn) => fn(acc), a);
6
- }
@@ -1,37 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.tap = void 0;
4
- /**
5
- * Executes a side effect function and returns the original value unchanged.
6
- * Useful for logging, debugging, or other side effects within a pipeline.
7
- *
8
- * @example
9
- * ```ts
10
- * // Debugging a pipeline
11
- * pipe(
12
- * Option.some(5),
13
- * tap(x => console.log("Before map:", x)),
14
- * Option.map(n => n * 2),
15
- * tap(x => console.log("After map:", x)),
16
- * Option.getOrElse(0)
17
- * );
18
- * // logs: "Before map: { kind: 'Some', value: 5 }"
19
- * // logs: "After map: { kind: 'Some', value: 10 }"
20
- * // returns: 10
21
- *
22
- * // Collecting intermediate values
23
- * const values: number[] = [];
24
- * pipe(
25
- * [1, 2, 3],
26
- * arr => arr.map(n => n * 2),
27
- * tap(arr => values.push(...arr))
28
- * );
29
- * ```
30
- *
31
- * @see {@link Option.tap} for Option-specific tap that only runs on Some
32
- */
33
- const tap = (f) => (a) => {
34
- f(a);
35
- return a;
36
- };
37
- exports.tap = tap;
@@ -1,38 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.uncurry4 = exports.uncurry3 = void 0;
4
- exports.uncurry = uncurry;
5
- // deno-lint-ignore no-explicit-any
6
- function uncurry(f) {
7
- // f.length determines the outer arity; inner.length determines the inner arity.
8
- // The typed overloads guarantee these are 0, 1, or 2 total args.
9
- // deno-lint-ignore no-explicit-any
10
- return (...args) => {
11
- const inner = f(...args.slice(0, f.length));
12
- return inner.length === 0 ? inner() : inner(...args.slice(f.length));
13
- };
14
- }
15
- /**
16
- * Converts a curried 3-argument function into a multi-argument function.
17
- *
18
- * @example
19
- * ```ts
20
- * const curriedAdd3 = (a: number) => (b: number) => (c: number) => a + b + c;
21
- * const add3 = uncurry3(curriedAdd3);
22
- * add3(1, 2, 3); // 6
23
- * ```
24
- */
25
- const uncurry3 = (f) => (a, b, c) => f(a)(b)(c);
26
- exports.uncurry3 = uncurry3;
27
- /**
28
- * Converts a curried 4-argument function into a multi-argument function.
29
- *
30
- * @example
31
- * ```ts
32
- * const curriedAdd4 = (a: number) => (b: number) => (c: number) => (d: number) => a + b + c + d;
33
- * const add4 = uncurry4(curriedAdd4);
34
- * add4(1, 2, 3, 4); // 10
35
- * ```
36
- */
37
- const uncurry4 = (f) => (a, b, c, d) => f(a)(b)(c)(d);
38
- exports.uncurry4 = uncurry4;
@@ -1,33 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.Deferred = void 0;
4
- const _store = new WeakMap();
5
- var Deferred;
6
- (function (Deferred) {
7
- /**
8
- * Wraps a `Promise` into a `Deferred`, structurally excluding rejection handlers,
9
- * `.catch()`, `.finally()`, and chainable `.then()`.
10
- *
11
- * @example
12
- * ```ts
13
- * const d = Deferred.fromPromise(Promise.resolve("hello"));
14
- * const value = await d; // "hello"
15
- * ```
16
- */
17
- Deferred.fromPromise = (p) => {
18
- const d = ({ then: ((f) => p.then(f)) });
19
- _store.set(d, p);
20
- return d;
21
- };
22
- /**
23
- * Converts a `Deferred` back into a `Promise`.
24
- *
25
- * @example
26
- * ```ts
27
- * const p = Deferred.toPromise(Deferred.fromPromise(Promise.resolve(42)));
28
- * // p is Promise<42>
29
- * ```
30
- */
31
- Deferred.toPromise = (d) => _store.get(d) ??
32
- new Promise((resolve) => d.then(resolve));
33
- })(Deferred || (exports.Deferred = Deferred = {}));
@@ -1,2 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
@@ -1,101 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.Lens = void 0;
4
- var Lens;
5
- (function (Lens) {
6
- /**
7
- * Constructs a Lens from a getter and a setter.
8
- *
9
- * @example
10
- * ```ts
11
- * const nameLens = Lens.make(
12
- * (user: User) => user.name,
13
- * (name) => (user) => ({ ...user, name }),
14
- * );
15
- * ```
16
- */
17
- Lens.make = (get, set) => ({ get, set });
18
- /**
19
- * Creates a Lens that focuses on a property of an object.
20
- * Call with the structure type first, then the key.
21
- *
22
- * @example
23
- * ```ts
24
- * const nameLens = Lens.prop<User>()("name");
25
- * ```
26
- */
27
- Lens.prop = () => (key) => Lens.make((s) => s[key], (a) => (s) => ({ ...s, [key]: a }));
28
- /**
29
- * Reads the focused value from a structure.
30
- *
31
- * @example
32
- * ```ts
33
- * pipe(user, Lens.get(nameLens)); // "Alice"
34
- * ```
35
- */
36
- Lens.get = (lens) => (s) => lens.get(s);
37
- /**
38
- * Replaces the focused value within a structure, returning a new structure.
39
- *
40
- * @example
41
- * ```ts
42
- * pipe(user, Lens.set(nameLens)("Bob")); // new User with name "Bob"
43
- * ```
44
- */
45
- Lens.set = (lens) => (a) => (s) => lens.set(a)(s);
46
- /**
47
- * Applies a function to the focused value, returning a new structure.
48
- *
49
- * @example
50
- * ```ts
51
- * pipe(user, Lens.modify(nameLens)(n => n.toUpperCase())); // "ALICE"
52
- * ```
53
- */
54
- Lens.modify = (lens) => (f) => (s) => lens.set(f(lens.get(s)))(s);
55
- /**
56
- * Composes two Lenses: focuses through the outer, then through the inner.
57
- * Use in a pipe chain to build up a deep focus step by step.
58
- *
59
- * @example
60
- * ```ts
61
- * const userCityLens = pipe(
62
- * Lens.prop<User>()("address"),
63
- * Lens.andThen(Lens.prop<Address>()("city")),
64
- * );
65
- * ```
66
- */
67
- Lens.andThen = (inner) => (outer) => Lens.make((s) => inner.get(outer.get(s)), (b) => (s) => outer.set(inner.set(b)(outer.get(s)))(s));
68
- /**
69
- * Composes a Lens with an Optional, producing an Optional.
70
- * Use when the next step in the focus is optional (may be absent).
71
- *
72
- * @example
73
- * ```ts
74
- * const userBioOpt = pipe(
75
- * Lens.prop<User>()("profile"),
76
- * Lens.andThenOptional(Optional.prop<Profile>()("bio")),
77
- * );
78
- * ```
79
- */
80
- Lens.andThenOptional = (inner) => (outer) => ({
81
- get: (s) => inner.get(outer.get(s)),
82
- set: (b) => (s) => outer.set(inner.set(b)(outer.get(s)))(s),
83
- });
84
- /**
85
- * Converts a Lens to an Optional. Every Lens is a valid Optional
86
- * whose get always returns Some.
87
- *
88
- * @example
89
- * ```ts
90
- * pipe(
91
- * Lens.prop<User>()("address"),
92
- * Lens.toOptional,
93
- * Optional.andThen(Optional.prop<Address>()("landmark")),
94
- * );
95
- * ```
96
- */
97
- Lens.toOptional = (lens) => ({
98
- get: (s) => ({ kind: "Some", value: lens.get(s) }),
99
- set: lens.set,
100
- });
101
- })(Lens || (exports.Lens = Lens = {}));
@@ -1,114 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.Logged = void 0;
4
- var Logged;
5
- (function (Logged) {
6
- /**
7
- * Wraps a pure value into a `Logged` with an empty log.
8
- *
9
- * @example
10
- * ```ts
11
- * Logged.make<string, number>(42); // { value: 42, log: [] }
12
- * ```
13
- */
14
- Logged.make = (value) => ({ value, log: [] });
15
- /**
16
- * Creates a `Logged` that records a single log entry and produces no
17
- * meaningful value. Use this to append to the log inside a `chain`.
18
- *
19
- * @example
20
- * ```ts
21
- * Logged.tell("operation completed"); // { value: undefined, log: ["operation completed"] }
22
- * ```
23
- */
24
- Logged.tell = (entry) => ({ value: undefined, log: [entry] });
25
- /**
26
- * Transforms the value inside a `Logged` without affecting the log.
27
- *
28
- * @example
29
- * ```ts
30
- * pipe(
31
- * Logged.make<string, number>(5),
32
- * Logged.map(n => n * 2),
33
- * ); // { value: 10, log: [] }
34
- * ```
35
- */
36
- Logged.map = (f) => (data) => ({
37
- value: f(data.value),
38
- log: data.log,
39
- });
40
- /**
41
- * Sequences two `Logged` computations, concatenating their logs.
42
- * The value from the first is passed to `f`; the resulting log entries are
43
- * appended after the entries from the first.
44
- *
45
- * Data-last — the first computation is the data being piped.
46
- *
47
- * @example
48
- * ```ts
49
- * const result = pipe(
50
- * Logged.make<string, number>(1),
51
- * Logged.chain(n => pipe(Logged.tell("step"), Logged.map(() => n + 1))),
52
- * Logged.chain(n => pipe(Logged.tell("done"), Logged.map(() => n * 10))),
53
- * );
54
- *
55
- * Logged.run(result); // [20, ["step", "done"]]
56
- * ```
57
- */
58
- Logged.chain = (f) => (data) => {
59
- const next = f(data.value);
60
- return { value: next.value, log: [...data.log, ...next.log] };
61
- };
62
- /**
63
- * Applies a function wrapped in a `Logged` to a value wrapped in a `Logged`,
64
- * concatenating both logs.
65
- *
66
- * @example
67
- * ```ts
68
- * const fn: Logged<string, (n: number) => number> = {
69
- * value: n => n * 2,
70
- * log: ["fn-loaded"],
71
- * };
72
- * const arg: Logged<string, number> = { value: 5, log: ["arg-loaded"] };
73
- *
74
- * const result = pipe(fn, Logged.ap(arg));
75
- * Logged.run(result); // [10, ["fn-loaded", "arg-loaded"]]
76
- * ```
77
- */
78
- Logged.ap = (arg) => (data) => ({
79
- value: data.value(arg.value),
80
- log: [...data.log, ...arg.log],
81
- });
82
- /**
83
- * Runs a side effect on the value without changing the `Logged`.
84
- * Useful for debugging or inspecting intermediate values.
85
- *
86
- * @example
87
- * ```ts
88
- * pipe(
89
- * Logged.make<string, number>(42),
90
- * Logged.tap(n => console.log("value:", n)),
91
- * );
92
- * ```
93
- */
94
- Logged.tap = (f) => (data) => {
95
- f(data.value);
96
- return data;
97
- };
98
- /**
99
- * Extracts the value and log as a `readonly [A, ReadonlyArray<W>]` tuple.
100
- * Use this at the boundary where you need to consume both.
101
- *
102
- * @example
103
- * ```ts
104
- * const result = pipe(
105
- * Logged.make<string, number>(1),
106
- * Logged.chain(n => pipe(Logged.tell("incremented"), Logged.map(() => n + 1))),
107
- * );
108
- *
109
- * const [value, log] = Logged.run(result);
110
- * // value = 2, log = ["incremented"]
111
- * ```
112
- */
113
- Logged.run = (data) => [data.value, data.log];
114
- })(Logged || (exports.Logged = Logged = {}));
@@ -1,194 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.Option = void 0;
4
- const Result_js_1 = require("./Result.js");
5
- const _none = { kind: "None" };
6
- var Option;
7
- (function (Option) {
8
- /**
9
- * Creates a Some containing the given value.
10
- */
11
- Option.some = (value) => ({ kind: "Some", value });
12
- /**
13
- * Type guard that checks if a Option is Some.
14
- */
15
- Option.isSome = (data) => data.kind === "Some";
16
- /**
17
- * Creates a None (empty Option).
18
- */
19
- Option.none = () => _none;
20
- /**
21
- * Type guard that checks if a Option is None.
22
- */
23
- Option.isNone = (data) => data.kind === "None";
24
- /**
25
- * Creates a Option from a nullable value.
26
- * Returns None if the value is null or undefined, Some otherwise.
27
- *
28
- * @example
29
- * ```ts
30
- * Option.fromNullable(null); // None
31
- * Option.fromNullable(42); // Some(42)
32
- * ```
33
- */
34
- Option.fromNullable = (value) => value === null || value === undefined ? Option.none() : Option.some(value);
35
- /**
36
- * Extracts the value from a Option, returning null if None.
37
- */
38
- Option.toNullable = (data) => Option.isSome(data) ? data.value : null;
39
- /**
40
- * Extracts the value from a Option, returning undefined if None.
41
- */
42
- Option.toUndefined = (data) => Option.isSome(data) ? data.value : undefined;
43
- /**
44
- * Creates a Option from a possibly undefined value.
45
- * Returns None if undefined, Some otherwise.
46
- */
47
- Option.fromUndefined = (value) => value === undefined ? Option.none() : Option.some(value);
48
- /**
49
- * Converts an Option to a Result.
50
- * Some becomes Ok, None becomes Err with the provided error.
51
- *
52
- * @example
53
- * ```ts
54
- * pipe(
55
- * Option.some(42),
56
- * Option.toResult(() => "Value was missing")
57
- * ); // Ok(42)
58
- *
59
- * pipe(
60
- * Option.none(),
61
- * Option.toResult(() => "Value was missing")
62
- * ); // Err("Value was missing")
63
- * ```
64
- */
65
- Option.toResult = (onNone) => (data) => Option.isSome(data) ? Result_js_1.Result.ok(data.value) : Result_js_1.Result.err(onNone());
66
- /**
67
- * Creates an Option from a Result.
68
- * Ok becomes Some, Err becomes None (the error is discarded).
69
- *
70
- * @example
71
- * ```ts
72
- * Option.fromResult(Result.ok(42)); // Some(42)
73
- * Option.fromResult(Result.err("oops")); // None
74
- * ```
75
- */
76
- Option.fromResult = (data) => Result_js_1.Result.isOk(data) ? Option.some(data.value) : Option.none();
77
- /**
78
- * Transforms the value inside a Option if it exists.
79
- *
80
- * @example
81
- * ```ts
82
- * pipe(Option.some(5), Option.map(n => n * 2)); // Some(10)
83
- * pipe(Option.none(), Option.map(n => n * 2)); // None
84
- * ```
85
- */
86
- Option.map = (f) => (data) => Option.isSome(data) ? Option.some(f(data.value)) : data;
87
- /**
88
- * Chains Option computations. If the first is Some, passes the value to f.
89
- * If the first is None, propagates None.
90
- *
91
- * @example
92
- * ```ts
93
- * const parseNumber = (s: string): Option<number> => {
94
- * const n = parseInt(s, 10);
95
- * return isNaN(n) ? Option.none() : Option.some(n);
96
- * };
97
- *
98
- * pipe(Option.some("42"), Option.chain(parseNumber)); // Some(42)
99
- * pipe(Option.some("abc"), Option.chain(parseNumber)); // None
100
- * ```
101
- */
102
- Option.chain = (f) => (data) => Option.isSome(data) ? f(data.value) : data;
103
- /**
104
- * Extracts the value from a Option by providing handlers for both cases.
105
- *
106
- * @example
107
- * ```ts
108
- * pipe(
109
- * Option.some(5),
110
- * Option.fold(
111
- * () => "No value",
112
- * n => `Value: ${n}`
113
- * )
114
- * ); // "Value: 5"
115
- * ```
116
- */
117
- Option.fold = (onNone, onSome) => (data) => Option.isSome(data) ? onSome(data.value) : onNone();
118
- /**
119
- * Pattern matches on a Option, returning the result of the matching case.
120
- *
121
- * @example
122
- * ```ts
123
- * pipe(
124
- * optionUser,
125
- * Option.match({
126
- * some: user => `Hello, ${user.name}`,
127
- * none: () => "Hello, stranger"
128
- * })
129
- * );
130
- * ```
131
- */
132
- Option.match = (cases) => (data) => Option.isSome(data) ? cases.some(data.value) : cases.none();
133
- /**
134
- * Returns the value inside an Option, or a default value if None.
135
- * The default is a thunk `() => B` — evaluated only when the Option is None.
136
- * The default can be a different type, widening the result to `A | B`.
137
- *
138
- * @example
139
- * ```ts
140
- * pipe(Option.some(5), Option.getOrElse(() => 0)); // 5
141
- * pipe(Option.none(), Option.getOrElse(() => 0)); // 0
142
- * pipe(Option.none<string>(), Option.getOrElse(() => null)); // null — typed as string | null
143
- * ```
144
- */
145
- Option.getOrElse = (defaultValue) => (data) => Option.isSome(data) ? data.value : defaultValue();
146
- /**
147
- * Executes a side effect on the value without changing the Option.
148
- * Useful for logging or debugging.
149
- *
150
- * @example
151
- * ```ts
152
- * pipe(
153
- * Option.some(5),
154
- * Option.tap(n => console.log("Value:", n)),
155
- * Option.map(n => n * 2)
156
- * );
157
- * ```
158
- */
159
- Option.tap = (f) => (data) => {
160
- if (Option.isSome(data))
161
- f(data.value);
162
- return data;
163
- };
164
- /**
165
- * Filters a Option based on a predicate.
166
- * Returns None if the predicate returns false or if the Option is already None.
167
- *
168
- * @example
169
- * ```ts
170
- * pipe(Option.some(5), Option.filter(n => n > 3)); // Some(5)
171
- * pipe(Option.some(2), Option.filter(n => n > 3)); // None
172
- * ```
173
- */
174
- Option.filter = (predicate) => (data) => Option.isSome(data) ? (predicate(data.value) ? data : Option.none()) : data;
175
- /**
176
- * Recovers from a None by providing a fallback Option.
177
- * The fallback can produce a different type, widening the result to `Option<A | B>`.
178
- */
179
- Option.recover = (fallback) => (data) => Option.isSome(data) ? data : fallback();
180
- /**
181
- * Applies a function wrapped in a Option to a value wrapped in a Option.
182
- *
183
- * @example
184
- * ```ts
185
- * const add = (a: number) => (b: number) => a + b;
186
- * pipe(
187
- * Option.some(add),
188
- * Option.ap(Option.some(5)),
189
- * Option.ap(Option.some(3))
190
- * ); // Some(8)
191
- * ```
192
- */
193
- Option.ap = (arg) => (data) => Option.isSome(data) && Option.isSome(arg) ? Option.some(data.value(arg.value)) : Option.none();
194
- })(Option || (exports.Option = Option = {}));