@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,163 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.Optional = void 0;
4
- const Option_js_1 = require("./Option.js");
5
- var Optional;
6
- (function (Optional) {
7
- /**
8
- * Constructs an Optional from a getter (returning Option<A>) and a setter.
9
- *
10
- * @example
11
- * ```ts
12
- * const firstChar = Optional.make(
13
- * (s: string) => s.length > 0 ? Option.some(s[0]) : Option.none(),
14
- * (c) => (s) => s.length > 0 ? c + s.slice(1) : s,
15
- * );
16
- * ```
17
- */
18
- Optional.make = (get, set) => ({ get, set });
19
- /**
20
- * Creates an Optional that focuses on an optional property of an object.
21
- * Only keys whose type includes undefined (i.e. `field?: T`) are accepted.
22
- * Call with the structure type first, then the key.
23
- *
24
- * @example
25
- * ```ts
26
- * type Profile = { username: string; bio?: string };
27
- * const bioOpt = Optional.prop<Profile>()("bio");
28
- * ```
29
- */
30
- Optional.prop = () => (key) => Optional.make((s) => {
31
- const val = s[key];
32
- return val != null ? Option_js_1.Option.some(val) : Option_js_1.Option.none();
33
- }, (a) => (s) => ({ ...s, [key]: a }));
34
- /**
35
- * Creates an Optional that focuses on an element at a given index in an array.
36
- * Returns None when the index is out of bounds; set is a no-op when out of bounds.
37
- *
38
- * @example
39
- * ```ts
40
- * const firstItem = Optional.index<string>(0);
41
- *
42
- * pipe(["a", "b"], Optional.get(firstItem)); // Some("a")
43
- * pipe([], Optional.get(firstItem)); // None
44
- * ```
45
- */
46
- Optional.index = (i) => Optional.make((arr) => i >= 0 && i < arr.length ? Option_js_1.Option.some(arr[i]) : Option_js_1.Option.none(), (a) => (arr) => {
47
- if (i < 0 || i >= arr.length)
48
- return arr;
49
- const copy = [...arr];
50
- copy[i] = a;
51
- return copy;
52
- });
53
- /**
54
- * Reads the focused value from a structure, returning Option<A>.
55
- *
56
- * @example
57
- * ```ts
58
- * pipe(profile, Optional.get(bioOpt)); // Some("...") or None
59
- * ```
60
- */
61
- Optional.get = (opt) => (s) => opt.get(s);
62
- /**
63
- * Replaces the focused value within a structure.
64
- * For indexed focuses, this is a no-op when the index is out of bounds.
65
- *
66
- * @example
67
- * ```ts
68
- * pipe(profile, Optional.set(bioOpt)("hello"));
69
- * ```
70
- */
71
- Optional.set = (opt) => (a) => (s) => opt.set(a)(s);
72
- /**
73
- * Applies a function to the focused value if it is present; returns the
74
- * structure unchanged if the focus is absent.
75
- *
76
- * @example
77
- * ```ts
78
- * pipe(profile, Optional.modify(bioOpt)(s => s.toUpperCase()));
79
- * ```
80
- */
81
- Optional.modify = (opt) => (f) => (s) => {
82
- const val = opt.get(s);
83
- return val.kind === "None" ? s : opt.set(f(val.value))(s);
84
- };
85
- /**
86
- * Returns the focused value or a default when the focus is absent.
87
- *
88
- * @example
89
- * ```ts
90
- * pipe(profile, Optional.getOrElse(bioOpt)(() => "no bio"));
91
- * ```
92
- */
93
- Optional.getOrElse = (opt) => (defaultValue) => (s) => {
94
- const val = opt.get(s);
95
- return val.kind === "Some" ? val.value : defaultValue();
96
- };
97
- /**
98
- * Extracts a value from an Optional focus using handlers for the present
99
- * and absent cases.
100
- *
101
- * @example
102
- * ```ts
103
- * pipe(profile, Optional.fold(bioOpt)(() => "no bio", (bio) => bio.toUpperCase()));
104
- * ```
105
- */
106
- Optional.fold = (opt) => (onNone, onSome) => (s) => {
107
- const val = opt.get(s);
108
- return val.kind === "Some" ? onSome(val.value) : onNone();
109
- };
110
- /**
111
- * Pattern matches on an Optional focus using a named-case object.
112
- *
113
- * @example
114
- * ```ts
115
- * pipe(
116
- * profile,
117
- * Optional.match(bioOpt)({ none: () => "no bio", some: (bio) => bio }),
118
- * );
119
- * ```
120
- */
121
- Optional.match = (opt) => (cases) => (s) => {
122
- const val = opt.get(s);
123
- return val.kind === "Some" ? cases.some(val.value) : cases.none();
124
- };
125
- /**
126
- * Composes two Optionals: focuses through the outer, then through the inner.
127
- * Returns None if either focus is absent.
128
- *
129
- * @example
130
- * ```ts
131
- * const deepOpt = pipe(
132
- * Optional.prop<User>()("address"),
133
- * Optional.andThen(Optional.prop<Address>()("landmark")),
134
- * );
135
- * ```
136
- */
137
- Optional.andThen = (inner) => (outer) => Optional.make((s) => {
138
- const mid = outer.get(s);
139
- return mid.kind === "None" ? Option_js_1.Option.none() : inner.get(mid.value);
140
- }, (b) => (s) => {
141
- const mid = outer.get(s);
142
- return mid.kind === "None" ? s : outer.set(inner.set(b)(mid.value))(s);
143
- });
144
- /**
145
- * Composes an Optional with a Lens, producing an Optional.
146
- * The Lens focuses within the value found by the Optional.
147
- *
148
- * @example
149
- * ```ts
150
- * const cityOpt = pipe(
151
- * Optional.prop<User>()("address"),
152
- * Optional.andThenLens(Lens.prop<Address>()("city")),
153
- * );
154
- * ```
155
- */
156
- Optional.andThenLens = (inner) => (outer) => Optional.make((s) => {
157
- const mid = outer.get(s);
158
- return mid.kind === "None" ? Option_js_1.Option.none() : Option_js_1.Option.some(inner.get(mid.value));
159
- }, (b) => (s) => {
160
- const mid = outer.get(s);
161
- return mid.kind === "None" ? s : outer.set(inner.set(b)(mid.value))(s);
162
- });
163
- })(Optional || (exports.Optional = Optional = {}));
@@ -1,136 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.Predicate = void 0;
4
- var Predicate;
5
- (function (Predicate) {
6
- /**
7
- * Negates a predicate: the result passes exactly when the original fails.
8
- *
9
- * @example
10
- * ```ts
11
- * const isBlank: Predicate<string> = s => s.trim().length === 0;
12
- * const isNotBlank = Predicate.not(isBlank);
13
- *
14
- * isNotBlank("hello"); // true
15
- * isNotBlank(" "); // false
16
- * ```
17
- */
18
- Predicate.not = (p) => (a) => !p(a);
19
- /**
20
- * Combines two predicates with logical AND: passes only when both hold.
21
- *
22
- * Data-last — the first predicate is the data being piped.
23
- *
24
- * @example
25
- * ```ts
26
- * const isPositive: Predicate<number> = n => n > 0;
27
- * const isEven: Predicate<number> = n => n % 2 === 0;
28
- *
29
- * const isPositiveEven: Predicate<number> = pipe(isPositive, Predicate.and(isEven));
30
- *
31
- * isPositiveEven(4); // true
32
- * isPositiveEven(3); // false — positive but odd
33
- * isPositiveEven(-2); // false — even but not positive
34
- * ```
35
- */
36
- Predicate.and = (second) => (first) => (a) => first(a) && second(a);
37
- /**
38
- * Combines two predicates with logical OR: passes when either holds.
39
- *
40
- * Data-last — the first predicate is the data being piped.
41
- *
42
- * @example
43
- * ```ts
44
- * const isChild: Predicate<number> = n => n < 13;
45
- * const isSenior: Predicate<number> = n => n >= 65;
46
- *
47
- * const getsDiscount: Predicate<number> = pipe(isChild, Predicate.or(isSenior));
48
- *
49
- * getsDiscount(8); // true
50
- * getsDiscount(70); // true
51
- * getsDiscount(30); // false
52
- * ```
53
- */
54
- Predicate.or = (second) => (first) => (a) => first(a) || second(a);
55
- /**
56
- * Adapts a `Predicate<A>` to work on a different input type `B` by applying `f`
57
- * to extract the relevant `A` from a `B` before running the check.
58
- *
59
- * Data-last — the predicate is the data being piped; `f` is the extractor.
60
- *
61
- * @example
62
- * ```ts
63
- * type User = { name: string; age: number };
64
- *
65
- * const isAdult: Predicate<number> = n => n >= 18;
66
- *
67
- * // Lift isAdult to work on Users by extracting the age field
68
- * const isAdultUser: Predicate<User> = pipe(
69
- * isAdult,
70
- * Predicate.using((u: User) => u.age)
71
- * );
72
- *
73
- * isAdultUser({ name: "Alice", age: 30 }); // true
74
- * isAdultUser({ name: "Bob", age: 15 }); // false
75
- * ```
76
- */
77
- Predicate.using = (f) => (p) => (b) => p(f(b));
78
- /**
79
- * Combines an array of predicates with AND: passes only when every predicate holds.
80
- * Returns `true` for an empty array (vacuous truth).
81
- *
82
- * @example
83
- * ```ts
84
- * const checks: Predicate<string>[] = [
85
- * s => s.length > 0,
86
- * s => s.length <= 100,
87
- * s => !s.includes("<"),
88
- * ];
89
- *
90
- * Predicate.all(checks)("hello"); // true
91
- * Predicate.all(checks)(""); // false — too short
92
- * Predicate.all(checks)("<b>"); // false — contains "<"
93
- * Predicate.all([])("anything"); // true
94
- * ```
95
- */
96
- Predicate.all = (predicates) => (a) => predicates.every((p) => p(a));
97
- /**
98
- * Combines an array of predicates with OR: passes when at least one holds.
99
- * Returns `false` for an empty array.
100
- *
101
- * @example
102
- * ```ts
103
- * const acceptedFormats: Predicate<string>[] = [
104
- * s => s.endsWith(".jpg"),
105
- * s => s.endsWith(".png"),
106
- * s => s.endsWith(".webp"),
107
- * ];
108
- *
109
- * Predicate.any(acceptedFormats)("photo.jpg"); // true
110
- * Predicate.any(acceptedFormats)("photo.gif"); // false
111
- * Predicate.any([])("anything"); // false
112
- * ```
113
- */
114
- Predicate.any = (predicates) => (a) => predicates.some((p) => p(a));
115
- /**
116
- * Converts a `Refinement<A, B>` into a `Predicate<A>`, discarding the compile-time
117
- * narrowing. Use this when you want to combine a type guard with plain predicates
118
- * using `and`, `or`, or `all`.
119
- *
120
- * @example
121
- * ```ts
122
- * const isString: Refinement<unknown, string> =
123
- * Refinement.make(x => typeof x === "string");
124
- *
125
- * const isShortString: Predicate<unknown> = pipe(
126
- * Predicate.fromRefinement(isString),
127
- * Predicate.and(x => (x as string).length < 10)
128
- * );
129
- *
130
- * isShortString("hi"); // true
131
- * isShortString("a very long string that exceeds ten characters"); // false
132
- * isShortString(42); // false
133
- * ```
134
- */
135
- Predicate.fromRefinement = (r) => r;
136
- })(Predicate || (exports.Predicate = Predicate = {}));
@@ -1,137 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.Reader = void 0;
4
- var Reader;
5
- (function (Reader) {
6
- /**
7
- * Lifts a pure value into a Reader. The environment is ignored.
8
- *
9
- * @example
10
- * ```ts
11
- * const always42: Reader<Config, number> = Reader.resolve(42);
12
- * always42(anyConfig); // 42
13
- * ```
14
- */
15
- Reader.resolve = (value) => (_env) => value;
16
- /**
17
- * Returns the full environment as the result.
18
- * The fundamental way to access the environment in a pipeline.
19
- *
20
- * @example
21
- * ```ts
22
- * pipe(
23
- * Reader.ask<Config>(),
24
- * Reader.map(config => config.baseUrl)
25
- * )(appConfig); // "https://api.example.com"
26
- * ```
27
- */
28
- Reader.ask = () => (env) => env;
29
- /**
30
- * Projects a value from the environment using a selector function.
31
- * Equivalent to `pipe(Reader.ask(), Reader.map(f))` but more direct.
32
- *
33
- * @example
34
- * ```ts
35
- * const getBaseUrl: Reader<Config, string> = Reader.asks(c => c.baseUrl);
36
- * getBaseUrl(appConfig); // "https://api.example.com"
37
- * ```
38
- */
39
- Reader.asks = (f) => (env) => f(env);
40
- /**
41
- * Transforms the value produced by a Reader.
42
- *
43
- * @example
44
- * ```ts
45
- * pipe(
46
- * Reader.asks((c: Config) => c.baseUrl),
47
- * Reader.map(url => url.toUpperCase())
48
- * )(appConfig); // "HTTPS://API.EXAMPLE.COM"
49
- * ```
50
- */
51
- Reader.map = (f) => (data) => (env) => f(data(env));
52
- /**
53
- * Sequences two Readers. Both see the same environment.
54
- * The output of the first is passed to `f`, which returns the next Reader.
55
- *
56
- * @example
57
- * ```ts
58
- * const buildUrl = (path: string): Reader<Config, string> =>
59
- * Reader.asks(c => `${c.baseUrl}${path}`);
60
- *
61
- * const addAuth = (url: string): Reader<Config, string> =>
62
- * Reader.asks(c => `${url}?key=${c.apiKey}`);
63
- *
64
- * pipe(
65
- * buildUrl("/items"),
66
- * Reader.chain(addAuth)
67
- * )(appConfig); // "https://api.example.com/items?key=secret"
68
- * ```
69
- */
70
- Reader.chain = (f) => (data) => (env) => f(data(env))(env);
71
- /**
72
- * Applies a function wrapped in a Reader to a value wrapped in a Reader.
73
- * Both Readers see the same environment.
74
- *
75
- * @example
76
- * ```ts
77
- * const add = (a: number) => (b: number) => a + b;
78
- * pipe(
79
- * Reader.resolve<Config, typeof add>(add),
80
- * Reader.ap(Reader.asks(c => c.timeout)),
81
- * Reader.ap(Reader.resolve(5))
82
- * )(appConfig);
83
- * ```
84
- */
85
- Reader.ap = (arg) => (data) => (env) => data(env)(arg(env));
86
- /**
87
- * Executes a side effect on the produced value without changing the Reader.
88
- * Useful for logging or debugging inside a pipeline.
89
- *
90
- * @example
91
- * ```ts
92
- * pipe(
93
- * buildUrl("/users"),
94
- * Reader.tap(url => console.log("Requesting:", url)),
95
- * Reader.chain(addAuth)
96
- * )(appConfig);
97
- * ```
98
- */
99
- Reader.tap = (f) => (data) => (env) => {
100
- const a = data(env);
101
- f(a);
102
- return a;
103
- };
104
- /**
105
- * Adapts a Reader to work with a different (typically wider) environment
106
- * by transforming the environment before passing it to the Reader.
107
- * This lets you compose Readers that expect different environments.
108
- *
109
- * @example
110
- * ```ts
111
- * type AppEnv = { db: DbPool; config: Config; logger: Logger };
112
- *
113
- * // buildUrl only needs Config
114
- * const buildUrl: Reader<Config, string> = Reader.asks(c => c.baseUrl);
115
- *
116
- * // Zoom in from AppEnv to Config
117
- * const buildUrlFromApp: Reader<AppEnv, string> =
118
- * pipe(buildUrl, Reader.local((env: AppEnv) => env.config));
119
- *
120
- * buildUrlFromApp(appEnv); // works with the full AppEnv
121
- * ```
122
- */
123
- Reader.local = (f) => (data) => (env) => data(f(env));
124
- /**
125
- * Runs a Reader by supplying the environment. Use this at the edge of your
126
- * program where the environment is available.
127
- *
128
- * @example
129
- * ```ts
130
- * pipe(
131
- * buildEndpoint("/users"),
132
- * Reader.run(appConfig)
133
- * ); // "https://api.example.com/users?key=secret"
134
- * ```
135
- */
136
- Reader.run = (env) => (data) => data(env);
137
- })(Reader || (exports.Reader = Reader = {}));
@@ -1,118 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.Refinement = void 0;
4
- const Option_js_1 = require("./Option.js");
5
- const Result_js_1 = require("./Result.js");
6
- var Refinement;
7
- (function (Refinement) {
8
- /**
9
- * Creates a `Refinement<A, B>` from a plain boolean predicate.
10
- *
11
- * This is an unsafe cast — the caller is responsible for ensuring that the
12
- * predicate truly characterises values of type `B`. Use this only when
13
- * bootstrapping a new refinement; prefer `compose`, `and`, or `or` to build
14
- * derived refinements from existing ones.
15
- *
16
- * @example
17
- * ```ts
18
- * type PositiveNumber = number & { readonly _tag: "PositiveNumber" };
19
- *
20
- * const isPositive: Refinement<number, PositiveNumber> =
21
- * Refinement.make(n => n > 0);
22
- * ```
23
- */
24
- Refinement.make = (f) => f;
25
- /**
26
- * Chains two refinements: if `ab` narrows `A` to `B` and `bc` narrows `B` to `C`,
27
- * the result narrows `A` directly to `C`.
28
- *
29
- * Data-last — the first refinement `ab` is the data being piped.
30
- *
31
- * @example
32
- * ```ts
33
- * type NonEmptyString = string & { readonly _tag: "NonEmpty" };
34
- * type TrimmedString = NonEmptyString & { readonly _tag: "Trimmed" };
35
- *
36
- * const isNonEmpty: Refinement<string, NonEmptyString> =
37
- * Refinement.make(s => s.length > 0);
38
- * const isTrimmed: Refinement<NonEmptyString, TrimmedString> =
39
- * Refinement.make(s => s === s.trim());
40
- *
41
- * const isNonEmptyTrimmed: Refinement<string, TrimmedString> = pipe(
42
- * isNonEmpty,
43
- * Refinement.compose(isTrimmed)
44
- * );
45
- * ```
46
- */
47
- Refinement.compose = (bc) => (ab) => (a) => ab(a) && bc(a);
48
- /**
49
- * Intersects two refinements: the result narrows `A` to `B & C`, passing only
50
- * when both refinements hold simultaneously.
51
- *
52
- * Data-last — the first refinement is the data being piped.
53
- *
54
- * @example
55
- * ```ts
56
- * const isString: Refinement<unknown, string> = Refinement.make(x => typeof x === "string");
57
- * const isNonEmpty: Refinement<unknown, { length: number }> =
58
- * Refinement.make(x => (x as any).length > 0);
59
- *
60
- * const isNonEmptyString = pipe(isString, Refinement.and(isNonEmpty));
61
- * isNonEmptyString("hi"); // true
62
- * isNonEmptyString(""); // false
63
- * ```
64
- */
65
- Refinement.and = (second) => (first) => (a) => first(a) && second(a);
66
- /**
67
- * Unions two refinements: the result narrows `A` to `B | C`, passing when either
68
- * refinement holds.
69
- *
70
- * Data-last — the first refinement is the data being piped.
71
- *
72
- * @example
73
- * ```ts
74
- * const isString: Refinement<unknown, string> = Refinement.make(x => typeof x === "string");
75
- * const isNumber: Refinement<unknown, number> = Refinement.make(x => typeof x === "number");
76
- *
77
- * const isStringOrNumber = pipe(isString, Refinement.or(isNumber));
78
- * isStringOrNumber("hi"); // true
79
- * isStringOrNumber(42); // true
80
- * isStringOrNumber(true); // false
81
- * ```
82
- */
83
- Refinement.or = (second) => (first) => (a) => first(a) || second(a);
84
- /**
85
- * Converts a `Refinement<A, B>` into a function `(a: A) => Option<B>`.
86
- *
87
- * Returns `Some(a)` when the refinement holds, `None` otherwise. Useful for
88
- * integrating runtime validation into an `Option`-based pipeline.
89
- *
90
- * @example
91
- * ```ts
92
- * type PositiveNumber = number & { readonly _tag: "Positive" };
93
- * const isPositive: Refinement<number, PositiveNumber> =
94
- * Refinement.make(n => n > 0);
95
- *
96
- * pipe(-1, Refinement.toFilter(isPositive)); // None
97
- * pipe(42, Refinement.toFilter(isPositive)); // Some(42)
98
- * ```
99
- */
100
- Refinement.toFilter = (r) => (a) => r(a) ? Option_js_1.Option.some(a) : Option_js_1.Option.none();
101
- /**
102
- * Converts a `Refinement<A, B>` into a function `(a: A) => Result<E, B>`.
103
- *
104
- * Returns `Ok(a)` when the refinement holds, `Err(onFail(a))` otherwise. Use
105
- * this to surface validation failures as typed errors inside a `Result` pipeline.
106
- *
107
- * @example
108
- * ```ts
109
- * type NonEmptyString = string & { readonly _tag: "NonEmpty" };
110
- * const isNonEmpty: Refinement<string, NonEmptyString> =
111
- * Refinement.make(s => s.length > 0);
112
- *
113
- * pipe("", Refinement.toResult(isNonEmpty, () => "must not be empty")); // Err(...)
114
- * pipe("hi", Refinement.toResult(isNonEmpty, () => "must not be empty")); // Ok("hi")
115
- * ```
116
- */
117
- Refinement.toResult = (r, onFail) => (a) => r(a) ? Result_js_1.Result.ok(a) : Result_js_1.Result.err(onFail(a));
118
- })(Refinement || (exports.Refinement = Refinement = {}));