@nlozgachev/pipelined 0.6.4

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 (144) hide show
  1. package/README.md +85 -0
  2. package/esm/mod.js +3 -0
  3. package/esm/package.json +3 -0
  4. package/esm/src/Composition/compose.js +3 -0
  5. package/esm/src/Composition/converge.js +3 -0
  6. package/esm/src/Composition/curry.js +42 -0
  7. package/esm/src/Composition/flip.js +20 -0
  8. package/esm/src/Composition/flow.js +8 -0
  9. package/esm/src/Composition/fn.js +85 -0
  10. package/esm/src/Composition/index.js +13 -0
  11. package/esm/src/Composition/juxt.js +3 -0
  12. package/esm/src/Composition/memoize.js +66 -0
  13. package/esm/src/Composition/not.js +25 -0
  14. package/esm/src/Composition/on.js +12 -0
  15. package/esm/src/Composition/pipe.js +3 -0
  16. package/esm/src/Composition/tap.js +33 -0
  17. package/esm/src/Composition/uncurry.js +32 -0
  18. package/esm/src/Core/Arr.js +463 -0
  19. package/esm/src/Core/Deferred.js +26 -0
  20. package/esm/src/Core/InternalTypes.js +1 -0
  21. package/esm/src/Core/Lens.js +98 -0
  22. package/esm/src/Core/Option.js +186 -0
  23. package/esm/src/Core/Optional.js +160 -0
  24. package/esm/src/Core/Reader.js +134 -0
  25. package/esm/src/Core/Rec.js +167 -0
  26. package/esm/src/Core/RemoteData.js +206 -0
  27. package/esm/src/Core/Result.js +164 -0
  28. package/esm/src/Core/Task.js +187 -0
  29. package/esm/src/Core/TaskOption.js +105 -0
  30. package/esm/src/Core/TaskResult.js +125 -0
  31. package/esm/src/Core/TaskValidation.js +101 -0
  32. package/esm/src/Core/These.js +241 -0
  33. package/esm/src/Core/Validation.js +214 -0
  34. package/esm/src/Core/index.js +15 -0
  35. package/esm/src/Types/Brand.js +28 -0
  36. package/esm/src/Types/NonEmptyList.js +14 -0
  37. package/esm/src/Types/index.js +2 -0
  38. package/package.json +61 -0
  39. package/script/mod.js +19 -0
  40. package/script/package.json +3 -0
  41. package/script/src/Composition/compose.js +6 -0
  42. package/script/src/Composition/converge.js +6 -0
  43. package/script/src/Composition/curry.js +48 -0
  44. package/script/src/Composition/flip.js +24 -0
  45. package/script/src/Composition/flow.js +11 -0
  46. package/script/src/Composition/fn.js +98 -0
  47. package/script/src/Composition/index.js +29 -0
  48. package/script/src/Composition/juxt.js +6 -0
  49. package/script/src/Composition/memoize.js +71 -0
  50. package/script/src/Composition/not.js +29 -0
  51. package/script/src/Composition/on.js +16 -0
  52. package/script/src/Composition/pipe.js +6 -0
  53. package/script/src/Composition/tap.js +37 -0
  54. package/script/src/Composition/uncurry.js +38 -0
  55. package/script/src/Core/Arr.js +466 -0
  56. package/script/src/Core/Deferred.js +29 -0
  57. package/script/src/Core/InternalTypes.js +2 -0
  58. package/script/src/Core/Lens.js +101 -0
  59. package/script/src/Core/Option.js +189 -0
  60. package/script/src/Core/Optional.js +163 -0
  61. package/script/src/Core/Reader.js +137 -0
  62. package/script/src/Core/Rec.js +170 -0
  63. package/script/src/Core/RemoteData.js +209 -0
  64. package/script/src/Core/Result.js +167 -0
  65. package/script/src/Core/Task.js +190 -0
  66. package/script/src/Core/TaskOption.js +108 -0
  67. package/script/src/Core/TaskResult.js +128 -0
  68. package/script/src/Core/TaskValidation.js +104 -0
  69. package/script/src/Core/These.js +244 -0
  70. package/script/src/Core/Validation.js +217 -0
  71. package/script/src/Core/index.js +31 -0
  72. package/script/src/Types/Brand.js +31 -0
  73. package/script/src/Types/NonEmptyList.js +18 -0
  74. package/script/src/Types/index.js +18 -0
  75. package/types/mod.d.ts +4 -0
  76. package/types/mod.d.ts.map +1 -0
  77. package/types/src/Composition/compose.d.ts +33 -0
  78. package/types/src/Composition/compose.d.ts.map +1 -0
  79. package/types/src/Composition/converge.d.ts +21 -0
  80. package/types/src/Composition/converge.d.ts.map +1 -0
  81. package/types/src/Composition/curry.d.ts +43 -0
  82. package/types/src/Composition/curry.d.ts.map +1 -0
  83. package/types/src/Composition/flip.d.ts +21 -0
  84. package/types/src/Composition/flip.d.ts.map +1 -0
  85. package/types/src/Composition/flow.d.ts +56 -0
  86. package/types/src/Composition/flow.d.ts.map +1 -0
  87. package/types/src/Composition/fn.d.ts +76 -0
  88. package/types/src/Composition/fn.d.ts.map +1 -0
  89. package/types/src/Composition/index.d.ts +14 -0
  90. package/types/src/Composition/index.d.ts.map +1 -0
  91. package/types/src/Composition/juxt.d.ts +18 -0
  92. package/types/src/Composition/juxt.d.ts.map +1 -0
  93. package/types/src/Composition/memoize.d.ts +46 -0
  94. package/types/src/Composition/memoize.d.ts.map +1 -0
  95. package/types/src/Composition/not.d.ts +26 -0
  96. package/types/src/Composition/not.d.ts.map +1 -0
  97. package/types/src/Composition/on.d.ts +13 -0
  98. package/types/src/Composition/on.d.ts.map +1 -0
  99. package/types/src/Composition/pipe.d.ts +56 -0
  100. package/types/src/Composition/pipe.d.ts.map +1 -0
  101. package/types/src/Composition/tap.d.ts +31 -0
  102. package/types/src/Composition/tap.d.ts.map +1 -0
  103. package/types/src/Composition/uncurry.d.ts +54 -0
  104. package/types/src/Composition/uncurry.d.ts.map +1 -0
  105. package/types/src/Core/Arr.d.ts +355 -0
  106. package/types/src/Core/Arr.d.ts.map +1 -0
  107. package/types/src/Core/Deferred.d.ts +49 -0
  108. package/types/src/Core/Deferred.d.ts.map +1 -0
  109. package/types/src/Core/InternalTypes.d.ts +20 -0
  110. package/types/src/Core/InternalTypes.d.ts.map +1 -0
  111. package/types/src/Core/Lens.d.ts +118 -0
  112. package/types/src/Core/Lens.d.ts.map +1 -0
  113. package/types/src/Core/Option.d.ts +205 -0
  114. package/types/src/Core/Option.d.ts.map +1 -0
  115. package/types/src/Core/Optional.d.ts +158 -0
  116. package/types/src/Core/Optional.d.ts.map +1 -0
  117. package/types/src/Core/Reader.d.ts +156 -0
  118. package/types/src/Core/Reader.d.ts.map +1 -0
  119. package/types/src/Core/Rec.d.ts +121 -0
  120. package/types/src/Core/Rec.d.ts.map +1 -0
  121. package/types/src/Core/RemoteData.d.ts +192 -0
  122. package/types/src/Core/RemoteData.d.ts.map +1 -0
  123. package/types/src/Core/Result.d.ts +176 -0
  124. package/types/src/Core/Result.d.ts.map +1 -0
  125. package/types/src/Core/Task.d.ts +189 -0
  126. package/types/src/Core/Task.d.ts.map +1 -0
  127. package/types/src/Core/TaskOption.d.ts +120 -0
  128. package/types/src/Core/TaskOption.d.ts.map +1 -0
  129. package/types/src/Core/TaskResult.d.ts +117 -0
  130. package/types/src/Core/TaskResult.d.ts.map +1 -0
  131. package/types/src/Core/TaskValidation.d.ts +119 -0
  132. package/types/src/Core/TaskValidation.d.ts.map +1 -0
  133. package/types/src/Core/These.d.ts +221 -0
  134. package/types/src/Core/These.d.ts.map +1 -0
  135. package/types/src/Core/Validation.d.ts +213 -0
  136. package/types/src/Core/Validation.d.ts.map +1 -0
  137. package/types/src/Core/index.d.ts +16 -0
  138. package/types/src/Core/index.d.ts.map +1 -0
  139. package/types/src/Types/Brand.d.ts +52 -0
  140. package/types/src/Types/Brand.d.ts.map +1 -0
  141. package/types/src/Types/NonEmptyList.d.ts +29 -0
  142. package/types/src/Types/NonEmptyList.d.ts.map +1 -0
  143. package/types/src/Types/index.d.ts +3 -0
  144. package/types/src/Types/index.d.ts.map +1 -0
@@ -0,0 +1,189 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Option = void 0;
4
+ const Result_js_1 = require("./Result.js");
5
+ var Option;
6
+ (function (Option) {
7
+ /**
8
+ * Creates a Some containing the given value.
9
+ */
10
+ Option.some = (value) => ({ kind: "Some", value });
11
+ /**
12
+ * Type guard that checks if a Option is Some.
13
+ */
14
+ Option.isSome = (data) => data.kind === "Some";
15
+ /**
16
+ * Creates a None (empty Option).
17
+ */
18
+ Option.none = () => ({ kind: "None" });
19
+ /**
20
+ * Type guard that checks if a Option is None.
21
+ */
22
+ Option.isNone = (data) => data.kind === "None";
23
+ /**
24
+ * Creates a Option from a nullable value.
25
+ * Returns None if the value is null or undefined, Some otherwise.
26
+ *
27
+ * @example
28
+ * ```ts
29
+ * Option.fromNullable(null); // None
30
+ * Option.fromNullable(42); // Some(42)
31
+ * ```
32
+ */
33
+ Option.fromNullable = (value) => value === null || value === undefined ? Option.none() : Option.some(value);
34
+ /**
35
+ * Extracts the value from a Option, returning null if None.
36
+ */
37
+ Option.toNullable = (data) => Option.isSome(data) ? data.value : null;
38
+ /**
39
+ * Extracts the value from a Option, returning undefined if None.
40
+ */
41
+ Option.toUndefined = (data) => Option.isSome(data) ? data.value : undefined;
42
+ /**
43
+ * Creates a Option from a possibly undefined value.
44
+ * Returns None if undefined, Some otherwise.
45
+ */
46
+ Option.fromUndefined = (value) => value === undefined ? Option.none() : Option.some(value);
47
+ /**
48
+ * Converts an Option to a Result.
49
+ * Some becomes Ok, None becomes Err with the provided error.
50
+ *
51
+ * @example
52
+ * ```ts
53
+ * pipe(
54
+ * Option.some(42),
55
+ * Option.toResult(() => "Value was missing")
56
+ * ); // Ok(42)
57
+ *
58
+ * pipe(
59
+ * Option.none(),
60
+ * Option.toResult(() => "Value was missing")
61
+ * ); // Err("Value was missing")
62
+ * ```
63
+ */
64
+ Option.toResult = (onNone) => (data) => Option.isSome(data) ? Result_js_1.Result.ok(data.value) : Result_js_1.Result.err(onNone());
65
+ /**
66
+ * Creates an Option from a Result.
67
+ * Ok becomes Some, Err becomes None (the error is discarded).
68
+ *
69
+ * @example
70
+ * ```ts
71
+ * Option.fromResult(Result.ok(42)); // Some(42)
72
+ * Option.fromResult(Result.err("oops")); // None
73
+ * ```
74
+ */
75
+ Option.fromResult = (data) => Result_js_1.Result.isOk(data) ? Option.some(data.value) : Option.none();
76
+ /**
77
+ * Transforms the value inside a Option if it exists.
78
+ *
79
+ * @example
80
+ * ```ts
81
+ * pipe(Option.some(5), Option.map(n => n * 2)); // Some(10)
82
+ * pipe(Option.none(), Option.map(n => n * 2)); // None
83
+ * ```
84
+ */
85
+ Option.map = (f) => (data) => Option.isSome(data) ? Option.some(f(data.value)) : data;
86
+ /**
87
+ * Chains Option computations. If the first is Some, passes the value to f.
88
+ * If the first is None, propagates None.
89
+ *
90
+ * @example
91
+ * ```ts
92
+ * const parseNumber = (s: string): Option<number> => {
93
+ * const n = parseInt(s, 10);
94
+ * return isNaN(n) ? Option.none() : Option.some(n);
95
+ * };
96
+ *
97
+ * pipe(Option.some("42"), Option.chain(parseNumber)); // Some(42)
98
+ * pipe(Option.some("abc"), Option.chain(parseNumber)); // None
99
+ * ```
100
+ */
101
+ Option.chain = (f) => (data) => Option.isSome(data) ? f(data.value) : data;
102
+ /**
103
+ * Extracts the value from a Option by providing handlers for both cases.
104
+ *
105
+ * @example
106
+ * ```ts
107
+ * pipe(
108
+ * Option.some(5),
109
+ * Option.fold(
110
+ * () => "No value",
111
+ * n => `Value: ${n}`
112
+ * )
113
+ * ); // "Value: 5"
114
+ * ```
115
+ */
116
+ Option.fold = (onNone, onSome) => (data) => Option.isSome(data) ? onSome(data.value) : onNone();
117
+ /**
118
+ * Pattern matches on a Option, returning the result of the matching case.
119
+ *
120
+ * @example
121
+ * ```ts
122
+ * pipe(
123
+ * optionUser,
124
+ * Option.match({
125
+ * some: user => `Hello, ${user.name}`,
126
+ * none: () => "Hello, stranger"
127
+ * })
128
+ * );
129
+ * ```
130
+ */
131
+ Option.match = (cases) => (data) => Option.isSome(data) ? cases.some(data.value) : cases.none();
132
+ /**
133
+ * Returns the value inside a Option, or a default value if None.
134
+ *
135
+ * @example
136
+ * ```ts
137
+ * pipe(Option.some(5), Option.getOrElse(0)); // 5
138
+ * pipe(Option.none(), Option.getOrElse(0)); // 0
139
+ * ```
140
+ */
141
+ Option.getOrElse = (defaultValue) => (data) => Option.isSome(data) ? data.value : defaultValue;
142
+ /**
143
+ * Executes a side effect on the value without changing the Option.
144
+ * Useful for logging or debugging.
145
+ *
146
+ * @example
147
+ * ```ts
148
+ * pipe(
149
+ * Option.some(5),
150
+ * Option.tap(n => console.log("Value:", n)),
151
+ * Option.map(n => n * 2)
152
+ * );
153
+ * ```
154
+ */
155
+ Option.tap = (f) => (data) => {
156
+ if (Option.isSome(data))
157
+ f(data.value);
158
+ return data;
159
+ };
160
+ /**
161
+ * Filters a Option based on a predicate.
162
+ * Returns None if the predicate returns false or if the Option is already None.
163
+ *
164
+ * @example
165
+ * ```ts
166
+ * pipe(Option.some(5), Option.filter(n => n > 3)); // Some(5)
167
+ * pipe(Option.some(2), Option.filter(n => n > 3)); // None
168
+ * ```
169
+ */
170
+ Option.filter = (predicate) => (data) => Option.isSome(data) && predicate(data.value) ? data : Option.none();
171
+ /**
172
+ * Recovers from a None by providing a fallback Option.
173
+ */
174
+ Option.recover = (fallback) => (data) => Option.isSome(data) ? data : fallback();
175
+ /**
176
+ * Applies a function wrapped in a Option to a value wrapped in a Option.
177
+ *
178
+ * @example
179
+ * ```ts
180
+ * const add = (a: number) => (b: number) => a + b;
181
+ * pipe(
182
+ * Option.some(add),
183
+ * Option.ap(Option.some(5)),
184
+ * Option.ap(Option.some(3))
185
+ * ); // Some(8)
186
+ * ```
187
+ */
188
+ Option.ap = (arg) => (data) => Option.isSome(data) && Option.isSome(arg) ? Option.some(data.value(arg.value)) : Option.none();
189
+ })(Option || (exports.Option = Option = {}));
@@ -0,0 +1,163 @@
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 = {}));
@@ -0,0 +1,137 @@
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 = {}));
@@ -0,0 +1,170 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Rec = void 0;
4
+ const Option_js_1 = require("./Option.js");
5
+ /**
6
+ * Functional record/object utilities that compose well with pipe.
7
+ * All functions are data-last and curried where applicable.
8
+ *
9
+ * @example
10
+ * ```ts
11
+ * pipe(
12
+ * { a: 1, b: 2, c: 3 },
13
+ * Rec.filter(n => n > 1),
14
+ * Rec.map(n => n * 10)
15
+ * ); // { b: 20, c: 30 }
16
+ * ```
17
+ */
18
+ var Rec;
19
+ (function (Rec) {
20
+ /**
21
+ * Transforms each value in a record.
22
+ *
23
+ * @example
24
+ * ```ts
25
+ * pipe({ a: 1, b: 2 }, Rec.map(n => n * 2)); // { a: 2, b: 4 }
26
+ * ```
27
+ */
28
+ Rec.map = (f) => (data) => {
29
+ const result = {};
30
+ for (const key of Object.keys(data)) {
31
+ result[key] = f(data[key]);
32
+ }
33
+ return result;
34
+ };
35
+ /**
36
+ * Transforms each value in a record, also receiving the key.
37
+ *
38
+ * @example
39
+ * ```ts
40
+ * pipe({ a: 1, b: 2 }, Rec.mapWithKey((k, v) => `${k}:${v}`));
41
+ * // { a: "a:1", b: "b:2" }
42
+ * ```
43
+ */
44
+ Rec.mapWithKey = (f) => (data) => {
45
+ const result = {};
46
+ for (const key of Object.keys(data)) {
47
+ result[key] = f(key, data[key]);
48
+ }
49
+ return result;
50
+ };
51
+ /**
52
+ * Filters values in a record by a predicate.
53
+ *
54
+ * @example
55
+ * ```ts
56
+ * pipe({ a: 1, b: 2, c: 3 }, Rec.filter(n => n > 1)); // { b: 2, c: 3 }
57
+ * ```
58
+ */
59
+ Rec.filter = (predicate) => (data) => {
60
+ const result = {};
61
+ for (const key of Object.keys(data)) {
62
+ if (predicate(data[key]))
63
+ result[key] = data[key];
64
+ }
65
+ return result;
66
+ };
67
+ /**
68
+ * Filters values in a record by a predicate that also receives the key.
69
+ *
70
+ * @example
71
+ * ```ts
72
+ * pipe({ a: 1, b: 2 }, Rec.filterWithKey((k, v) => k !== "a" && v > 0));
73
+ * // { b: 2 }
74
+ * ```
75
+ */
76
+ Rec.filterWithKey = (predicate) => (data) => {
77
+ const result = {};
78
+ for (const key of Object.keys(data)) {
79
+ if (predicate(key, data[key]))
80
+ result[key] = data[key];
81
+ }
82
+ return result;
83
+ };
84
+ /**
85
+ * Looks up a value by key, returning Option.
86
+ *
87
+ * @example
88
+ * ```ts
89
+ * pipe({ a: 1, b: 2 }, Rec.lookup("a")); // Some(1)
90
+ * pipe({ a: 1, b: 2 }, Rec.lookup("c")); // None
91
+ * ```
92
+ */
93
+ Rec.lookup = (key) => (data) => Object.prototype.hasOwnProperty.call(data, key) ? Option_js_1.Option.some(data[key]) : Option_js_1.Option.none();
94
+ /**
95
+ * Returns all keys of a record.
96
+ */
97
+ Rec.keys = (data) => Object.keys(data);
98
+ /**
99
+ * Returns all values of a record.
100
+ */
101
+ Rec.values = (data) => Object.values(data);
102
+ /**
103
+ * Returns all key-value pairs of a record.
104
+ */
105
+ Rec.entries = (data) => Object.entries(data);
106
+ /**
107
+ * Creates a record from key-value pairs.
108
+ *
109
+ * @example
110
+ * ```ts
111
+ * Rec.fromEntries([["a", 1], ["b", 2]]); // { a: 1, b: 2 }
112
+ * ```
113
+ */
114
+ Rec.fromEntries = (data) => Object.fromEntries(data);
115
+ /**
116
+ * Picks specific keys from a record.
117
+ *
118
+ * @example
119
+ * ```ts
120
+ * pipe({ a: 1, b: 2, c: 3 }, Rec.pick("a", "c")); // { a: 1, c: 3 }
121
+ * ```
122
+ */
123
+ Rec.pick = (...pickedKeys) => (data) => {
124
+ const result = {};
125
+ for (const key of pickedKeys) {
126
+ if (Object.prototype.hasOwnProperty.call(data, key)) {
127
+ result[key] = data[key];
128
+ }
129
+ }
130
+ return result;
131
+ };
132
+ /**
133
+ * Omits specific keys from a record.
134
+ *
135
+ * @example
136
+ * ```ts
137
+ * pipe({ a: 1, b: 2, c: 3 }, Rec.omit("b")); // { a: 1, c: 3 }
138
+ * ```
139
+ */
140
+ Rec.omit = (...omittedKeys) => (data) => {
141
+ const omitSet = new Set(omittedKeys);
142
+ const result = {};
143
+ for (const key of Object.keys(data)) {
144
+ if (!omitSet.has(key)) {
145
+ result[key] = data[key];
146
+ }
147
+ }
148
+ return result;
149
+ };
150
+ /**
151
+ * Merges two records. Values from the second record take precedence.
152
+ *
153
+ * @example
154
+ * ```ts
155
+ * pipe({ a: 1, b: 2 }, Rec.merge({ b: 3, c: 4 })); // { a: 1, b: 3, c: 4 }
156
+ * ```
157
+ */
158
+ Rec.merge = (other) => (data) => ({
159
+ ...data,
160
+ ...other,
161
+ });
162
+ /**
163
+ * Returns true if the record has no keys.
164
+ */
165
+ Rec.isEmpty = (data) => Object.keys(data).length === 0;
166
+ /**
167
+ * Returns the number of keys in a record.
168
+ */
169
+ Rec.size = (data) => Object.keys(data).length;
170
+ })(Rec || (exports.Rec = Rec = {}));