@nlozgachev/pipelined 0.12.0 → 0.14.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 (213) 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-Bd3gXPRQ.d.mts +677 -0
  6. package/dist/Task-BjAkkD6t.d.ts +677 -0
  7. package/dist/chunk-4TXC322E.mjs +136 -0
  8. package/dist/chunk-BYWKZLHM.mjs +10 -0
  9. package/dist/chunk-DBIC62UV.mjs +6 -0
  10. package/dist/chunk-FAZN3IWZ.mjs +554 -0
  11. package/dist/chunk-QPTGO5AS.mjs +150 -0
  12. package/dist/chunk-UV2HMF2A.mjs +514 -0
  13. package/dist/composition.d.mts +495 -0
  14. package/dist/composition.d.ts +495 -0
  15. package/dist/composition.js +188 -0
  16. package/dist/composition.mjs +58 -0
  17. package/dist/core.d.mts +2170 -0
  18. package/dist/core.d.ts +2170 -0
  19. package/dist/core.js +698 -0
  20. package/dist/core.mjs +42 -0
  21. package/dist/index.d.mts +6 -0
  22. package/dist/index.d.ts +6 -0
  23. package/dist/index.js +1421 -0
  24. package/dist/index.mjs +120 -0
  25. package/dist/types.d.mts +54 -0
  26. package/{types/src/Types/Brand.d.ts → dist/types.d.ts} +6 -4
  27. package/dist/types.js +41 -0
  28. package/dist/types.mjs +10 -0
  29. package/dist/utils.d.mts +1285 -0
  30. package/dist/utils.d.ts +1285 -0
  31. package/dist/utils.js +722 -0
  32. package/dist/utils.mjs +18 -0
  33. package/package.json +64 -69
  34. package/esm/mod.js +0 -3
  35. package/esm/package.json +0 -3
  36. package/esm/src/Composition/compose.js +0 -3
  37. package/esm/src/Composition/converge.js +0 -3
  38. package/esm/src/Composition/curry.js +0 -42
  39. package/esm/src/Composition/flip.js +0 -20
  40. package/esm/src/Composition/flow.js +0 -8
  41. package/esm/src/Composition/fn.js +0 -85
  42. package/esm/src/Composition/index.js +0 -13
  43. package/esm/src/Composition/juxt.js +0 -3
  44. package/esm/src/Composition/memoize.js +0 -66
  45. package/esm/src/Composition/not.js +0 -25
  46. package/esm/src/Composition/on.js +0 -12
  47. package/esm/src/Composition/pipe.js +0 -3
  48. package/esm/src/Composition/tap.js +0 -33
  49. package/esm/src/Composition/uncurry.js +0 -32
  50. package/esm/src/Core/Deferred.js +0 -30
  51. package/esm/src/Core/InternalTypes.js +0 -1
  52. package/esm/src/Core/Lens.js +0 -98
  53. package/esm/src/Core/Logged.js +0 -111
  54. package/esm/src/Core/Option.js +0 -191
  55. package/esm/src/Core/Optional.js +0 -160
  56. package/esm/src/Core/Predicate.js +0 -133
  57. package/esm/src/Core/Reader.js +0 -134
  58. package/esm/src/Core/Refinement.js +0 -115
  59. package/esm/src/Core/RemoteData.js +0 -211
  60. package/esm/src/Core/Result.js +0 -170
  61. package/esm/src/Core/State.js +0 -181
  62. package/esm/src/Core/Task.js +0 -223
  63. package/esm/src/Core/TaskOption.js +0 -106
  64. package/esm/src/Core/TaskResult.js +0 -127
  65. package/esm/src/Core/TaskValidation.js +0 -128
  66. package/esm/src/Core/These.js +0 -245
  67. package/esm/src/Core/Tuple.js +0 -112
  68. package/esm/src/Core/Validation.js +0 -212
  69. package/esm/src/Core/index.js +0 -18
  70. package/esm/src/Types/Brand.js +0 -28
  71. package/esm/src/Types/NonEmptyList.js +0 -14
  72. package/esm/src/Types/index.js +0 -2
  73. package/esm/src/Utils/Arr.js +0 -570
  74. package/esm/src/Utils/Dict.js +0 -421
  75. package/esm/src/Utils/Num.js +0 -124
  76. package/esm/src/Utils/Rec.js +0 -241
  77. package/esm/src/Utils/Str.js +0 -134
  78. package/esm/src/Utils/Uniq.js +0 -265
  79. package/esm/src/Utils/index.js +0 -6
  80. package/script/mod.js +0 -19
  81. package/script/package.json +0 -3
  82. package/script/src/Composition/compose.js +0 -6
  83. package/script/src/Composition/converge.js +0 -6
  84. package/script/src/Composition/curry.js +0 -48
  85. package/script/src/Composition/flip.js +0 -24
  86. package/script/src/Composition/flow.js +0 -11
  87. package/script/src/Composition/fn.js +0 -98
  88. package/script/src/Composition/index.js +0 -29
  89. package/script/src/Composition/juxt.js +0 -6
  90. package/script/src/Composition/memoize.js +0 -71
  91. package/script/src/Composition/not.js +0 -29
  92. package/script/src/Composition/on.js +0 -16
  93. package/script/src/Composition/pipe.js +0 -6
  94. package/script/src/Composition/tap.js +0 -37
  95. package/script/src/Composition/uncurry.js +0 -38
  96. package/script/src/Core/Deferred.js +0 -33
  97. package/script/src/Core/InternalTypes.js +0 -2
  98. package/script/src/Core/Lens.js +0 -101
  99. package/script/src/Core/Logged.js +0 -114
  100. package/script/src/Core/Option.js +0 -194
  101. package/script/src/Core/Optional.js +0 -163
  102. package/script/src/Core/Predicate.js +0 -136
  103. package/script/src/Core/Reader.js +0 -137
  104. package/script/src/Core/Refinement.js +0 -118
  105. package/script/src/Core/RemoteData.js +0 -214
  106. package/script/src/Core/Result.js +0 -173
  107. package/script/src/Core/State.js +0 -184
  108. package/script/src/Core/Task.js +0 -226
  109. package/script/src/Core/TaskOption.js +0 -109
  110. package/script/src/Core/TaskResult.js +0 -130
  111. package/script/src/Core/TaskValidation.js +0 -131
  112. package/script/src/Core/These.js +0 -248
  113. package/script/src/Core/Tuple.js +0 -115
  114. package/script/src/Core/Validation.js +0 -215
  115. package/script/src/Core/index.js +0 -34
  116. package/script/src/Types/Brand.js +0 -31
  117. package/script/src/Types/NonEmptyList.js +0 -18
  118. package/script/src/Types/index.js +0 -18
  119. package/script/src/Utils/Arr.js +0 -573
  120. package/script/src/Utils/Dict.js +0 -424
  121. package/script/src/Utils/Num.js +0 -127
  122. package/script/src/Utils/Rec.js +0 -244
  123. package/script/src/Utils/Str.js +0 -137
  124. package/script/src/Utils/Uniq.js +0 -268
  125. package/script/src/Utils/index.js +0 -22
  126. package/types/mod.d.ts +0 -4
  127. package/types/mod.d.ts.map +0 -1
  128. package/types/src/Composition/compose.d.ts +0 -33
  129. package/types/src/Composition/compose.d.ts.map +0 -1
  130. package/types/src/Composition/converge.d.ts +0 -21
  131. package/types/src/Composition/converge.d.ts.map +0 -1
  132. package/types/src/Composition/curry.d.ts +0 -43
  133. package/types/src/Composition/curry.d.ts.map +0 -1
  134. package/types/src/Composition/flip.d.ts +0 -21
  135. package/types/src/Composition/flip.d.ts.map +0 -1
  136. package/types/src/Composition/flow.d.ts +0 -56
  137. package/types/src/Composition/flow.d.ts.map +0 -1
  138. package/types/src/Composition/fn.d.ts +0 -76
  139. package/types/src/Composition/fn.d.ts.map +0 -1
  140. package/types/src/Composition/index.d.ts +0 -14
  141. package/types/src/Composition/index.d.ts.map +0 -1
  142. package/types/src/Composition/juxt.d.ts +0 -18
  143. package/types/src/Composition/juxt.d.ts.map +0 -1
  144. package/types/src/Composition/memoize.d.ts +0 -46
  145. package/types/src/Composition/memoize.d.ts.map +0 -1
  146. package/types/src/Composition/not.d.ts +0 -26
  147. package/types/src/Composition/not.d.ts.map +0 -1
  148. package/types/src/Composition/on.d.ts +0 -13
  149. package/types/src/Composition/on.d.ts.map +0 -1
  150. package/types/src/Composition/pipe.d.ts +0 -56
  151. package/types/src/Composition/pipe.d.ts.map +0 -1
  152. package/types/src/Composition/tap.d.ts +0 -31
  153. package/types/src/Composition/tap.d.ts.map +0 -1
  154. package/types/src/Composition/uncurry.d.ts +0 -54
  155. package/types/src/Composition/uncurry.d.ts.map +0 -1
  156. package/types/src/Core/Deferred.d.ts +0 -49
  157. package/types/src/Core/Deferred.d.ts.map +0 -1
  158. package/types/src/Core/InternalTypes.d.ts +0 -23
  159. package/types/src/Core/InternalTypes.d.ts.map +0 -1
  160. package/types/src/Core/Lens.d.ts +0 -118
  161. package/types/src/Core/Lens.d.ts.map +0 -1
  162. package/types/src/Core/Logged.d.ts +0 -126
  163. package/types/src/Core/Logged.d.ts.map +0 -1
  164. package/types/src/Core/Option.d.ts +0 -209
  165. package/types/src/Core/Option.d.ts.map +0 -1
  166. package/types/src/Core/Optional.d.ts +0 -158
  167. package/types/src/Core/Optional.d.ts.map +0 -1
  168. package/types/src/Core/Predicate.d.ts +0 -161
  169. package/types/src/Core/Predicate.d.ts.map +0 -1
  170. package/types/src/Core/Reader.d.ts +0 -156
  171. package/types/src/Core/Reader.d.ts.map +0 -1
  172. package/types/src/Core/Refinement.d.ts +0 -138
  173. package/types/src/Core/Refinement.d.ts.map +0 -1
  174. package/types/src/Core/RemoteData.d.ts +0 -197
  175. package/types/src/Core/RemoteData.d.ts.map +0 -1
  176. package/types/src/Core/Result.d.ts +0 -182
  177. package/types/src/Core/Result.d.ts.map +0 -1
  178. package/types/src/Core/State.d.ts +0 -192
  179. package/types/src/Core/State.d.ts.map +0 -1
  180. package/types/src/Core/Task.d.ts +0 -219
  181. package/types/src/Core/Task.d.ts.map +0 -1
  182. package/types/src/Core/TaskOption.d.ts +0 -121
  183. package/types/src/Core/TaskOption.d.ts.map +0 -1
  184. package/types/src/Core/TaskResult.d.ts +0 -119
  185. package/types/src/Core/TaskResult.d.ts.map +0 -1
  186. package/types/src/Core/TaskValidation.d.ts +0 -144
  187. package/types/src/Core/TaskValidation.d.ts.map +0 -1
  188. package/types/src/Core/These.d.ts +0 -225
  189. package/types/src/Core/These.d.ts.map +0 -1
  190. package/types/src/Core/Tuple.d.ts +0 -129
  191. package/types/src/Core/Tuple.d.ts.map +0 -1
  192. package/types/src/Core/Validation.d.ts +0 -203
  193. package/types/src/Core/Validation.d.ts.map +0 -1
  194. package/types/src/Core/index.d.ts +0 -19
  195. package/types/src/Core/index.d.ts.map +0 -1
  196. package/types/src/Types/Brand.d.ts.map +0 -1
  197. package/types/src/Types/NonEmptyList.d.ts.map +0 -1
  198. package/types/src/Types/index.d.ts +0 -3
  199. package/types/src/Types/index.d.ts.map +0 -1
  200. package/types/src/Utils/Arr.d.ts +0 -403
  201. package/types/src/Utils/Arr.d.ts.map +0 -1
  202. package/types/src/Utils/Dict.d.ts +0 -310
  203. package/types/src/Utils/Dict.d.ts.map +0 -1
  204. package/types/src/Utils/Num.d.ts +0 -110
  205. package/types/src/Utils/Num.d.ts.map +0 -1
  206. package/types/src/Utils/Rec.d.ts +0 -159
  207. package/types/src/Utils/Rec.d.ts.map +0 -1
  208. package/types/src/Utils/Str.d.ts +0 -128
  209. package/types/src/Utils/Str.d.ts.map +0 -1
  210. package/types/src/Utils/Uniq.d.ts +0 -179
  211. package/types/src/Utils/Uniq.d.ts.map +0 -1
  212. package/types/src/Utils/index.d.ts +0 -7
  213. package/types/src/Utils/index.d.ts.map +0 -1
@@ -1,421 +0,0 @@
1
- import { Option } from "../Core/Option.js";
2
- /**
3
- * Functional utilities for key-value dictionaries (`ReadonlyMap<K, V>`). All functions are pure
4
- * and data-last — they compose naturally with `pipe`.
5
- *
6
- * Unlike plain objects (`Rec`), dictionaries support any key type, preserve insertion order, and
7
- * make membership checks explicit via `lookup` returning `Option`.
8
- *
9
- * @example
10
- * ```ts
11
- * import { Dict } from "@nlozgachev/pipelined/utils";
12
- * import { pipe } from "@nlozgachev/pipelined/composition";
13
- *
14
- * const scores = pipe(
15
- * Dict.fromEntries([["alice", 10], ["bob", 8], ["carol", 10]] as const),
16
- * Dict.filter(n => n >= 10),
17
- * Dict.map(n => `${n} points`),
18
- * );
19
- * // ReadonlyMap { "alice" => "10 points", "carol" => "10 points" }
20
- * ```
21
- */
22
- export var Dict;
23
- (function (Dict) {
24
- // ---------------------------------------------------------------------------
25
- // Constructors
26
- // ---------------------------------------------------------------------------
27
- /**
28
- * Creates an empty dictionary.
29
- *
30
- * @example
31
- * ```ts
32
- * Dict.empty<string, number>(); // ReadonlyMap {}
33
- * ```
34
- */
35
- Dict.empty = () => new globalThis.Map();
36
- /**
37
- * Creates a dictionary with a single entry.
38
- *
39
- * @example
40
- * ```ts
41
- * Dict.singleton("name", "Alice"); // ReadonlyMap { "name" => "Alice" }
42
- * ```
43
- */
44
- Dict.singleton = (key, value) => new globalThis.Map([[key, value]]);
45
- /**
46
- * Creates a dictionary from an array of key-value pairs.
47
- *
48
- * @example
49
- * ```ts
50
- * Dict.fromEntries([["a", 1], ["b", 2]]); // ReadonlyMap { "a" => 1, "b" => 2 }
51
- * ```
52
- */
53
- Dict.fromEntries = (entries) => new globalThis.Map(entries);
54
- /**
55
- * Creates a dictionary from a plain object. Keys are always strings.
56
- *
57
- * @example
58
- * ```ts
59
- * Dict.fromRecord({ a: 1, b: 2 }); // ReadonlyMap { "a" => 1, "b" => 2 }
60
- * ```
61
- */
62
- Dict.fromRecord = (rec) => new globalThis.Map(Object.entries(rec));
63
- /**
64
- * Groups elements of an array into a dictionary keyed by the result of `keyFn`. Each key maps
65
- * to the array of elements that produced it, in insertion order. Uses the native `Map.groupBy`
66
- * when available, falling back to a manual loop in older environments.
67
- *
68
- * @example
69
- * ```ts
70
- * pipe(
71
- * [{ name: "alice", role: "admin" }, { name: "bob", role: "viewer" }, { name: "carol", role: "admin" }],
72
- * Dict.groupBy(user => user.role),
73
- * );
74
- * // ReadonlyMap { "admin" => [alice, carol], "viewer" => [bob] }
75
- * ```
76
- */
77
- Dict.groupBy = (keyFn) => (items) => {
78
- const result = new globalThis.Map();
79
- for (const item of items) {
80
- const key = keyFn(item);
81
- const arr = result.get(key);
82
- if (arr !== undefined)
83
- arr.push(item);
84
- else
85
- result.set(key, [item]);
86
- }
87
- return result;
88
- };
89
- // ---------------------------------------------------------------------------
90
- // Query
91
- // ---------------------------------------------------------------------------
92
- /**
93
- * Returns `true` if the dictionary contains the given key.
94
- *
95
- * @example
96
- * ```ts
97
- * pipe(Dict.fromEntries([["a", 1]]), Dict.has("a")); // true
98
- * pipe(Dict.fromEntries([["a", 1]]), Dict.has("b")); // false
99
- * ```
100
- */
101
- Dict.has = (key) => (m) => m.has(key);
102
- /**
103
- * Looks up a value by key, returning `Some(value)` if found and `None` if not.
104
- *
105
- * @example
106
- * ```ts
107
- * pipe(Dict.fromEntries([["a", 1]]), Dict.lookup("a")); // Some(1)
108
- * pipe(Dict.fromEntries([["a", 1]]), Dict.lookup("b")); // None
109
- * ```
110
- */
111
- Dict.lookup = (key) => (m) => m.has(key) ? Option.some(m.get(key)) : Option.none();
112
- /**
113
- * Returns the number of entries in the dictionary.
114
- *
115
- * @example
116
- * ```ts
117
- * Dict.size(Dict.fromEntries([["a", 1], ["b", 2]])); // 2
118
- * ```
119
- */
120
- Dict.size = (m) => m.size;
121
- /**
122
- * Returns `true` if the dictionary has no entries.
123
- *
124
- * @example
125
- * ```ts
126
- * Dict.isEmpty(Dict.empty()); // true
127
- * ```
128
- */
129
- Dict.isEmpty = (m) => m.size === 0;
130
- /**
131
- * Returns all keys as a readonly array, in insertion order.
132
- *
133
- * @example
134
- * ```ts
135
- * Dict.keys(Dict.fromEntries([["a", 1], ["b", 2]])); // ["a", "b"]
136
- * ```
137
- */
138
- Dict.keys = (m) => [...m.keys()];
139
- /**
140
- * Returns all values as a readonly array, in insertion order.
141
- *
142
- * @example
143
- * ```ts
144
- * Dict.values(Dict.fromEntries([["a", 1], ["b", 2]])); // [1, 2]
145
- * ```
146
- */
147
- Dict.values = (m) => [...m.values()];
148
- /**
149
- * Returns all key-value pairs as a readonly array of tuples, in insertion order.
150
- *
151
- * @example
152
- * ```ts
153
- * Dict.entries(Dict.fromEntries([["a", 1], ["b", 2]])); // [["a", 1], ["b", 2]]
154
- * ```
155
- */
156
- Dict.entries = (m) => [...m.entries()];
157
- // ---------------------------------------------------------------------------
158
- // Modification
159
- // ---------------------------------------------------------------------------
160
- /**
161
- * Returns a new dictionary with the given key set to the given value.
162
- * If the key already exists, its value is replaced.
163
- *
164
- * @example
165
- * ```ts
166
- * pipe(Dict.fromEntries([["a", 1]]), Dict.insert("b", 2));
167
- * // ReadonlyMap { "a" => 1, "b" => 2 }
168
- * ```
169
- */
170
- Dict.insert = (key, value) => (m) => {
171
- const result = new globalThis.Map(m);
172
- result.set(key, value);
173
- return result;
174
- };
175
- /**
176
- * Returns a new dictionary with the given key removed.
177
- * If the key does not exist, the dictionary is returned unchanged.
178
- *
179
- * @example
180
- * ```ts
181
- * pipe(Dict.fromEntries([["a", 1], ["b", 2]]), Dict.remove("a"));
182
- * // ReadonlyMap { "b" => 2 }
183
- * ```
184
- */
185
- Dict.remove = (key) => (m) => {
186
- if (!m.has(key))
187
- return m;
188
- const result = new globalThis.Map(m);
189
- result.delete(key);
190
- return result;
191
- };
192
- /**
193
- * Returns a new dictionary with the value at `key` set by `f`. If the key does not exist,
194
- * `f` receives `None`. If the key exists, `f` receives `Some(currentValue)`.
195
- *
196
- * Useful for incrementing counters, initialising defaults, or conditional updates.
197
- *
198
- * @example
199
- * ```ts
200
- * import { Option } from "@nlozgachev/pipelined/core";
201
- *
202
- * const increment = (opt: Option<number>) => Option.getOrElse(() => 0)(opt) + 1;
203
- * pipe(Dict.fromEntries([["views", 5]]), Dict.upsert("views", increment)); // { views: 6 }
204
- * pipe(Dict.fromEntries([["views", 5]]), Dict.upsert("likes", increment)); // { views: 5, likes: 1 }
205
- * ```
206
- */
207
- Dict.upsert = (key, f) => (m) => {
208
- const result = new globalThis.Map(m);
209
- result.set(key, f(Dict.lookup(key)(m)));
210
- return result;
211
- };
212
- // ---------------------------------------------------------------------------
213
- // Transform
214
- // ---------------------------------------------------------------------------
215
- /**
216
- * Transforms each value in the dictionary.
217
- *
218
- * @example
219
- * ```ts
220
- * pipe(Dict.fromEntries([["a", 1], ["b", 2]]), Dict.map(n => n * 2));
221
- * // ReadonlyMap { "a" => 2, "b" => 4 }
222
- * ```
223
- */
224
- Dict.map = (f) => (m) => {
225
- const result = new globalThis.Map();
226
- for (const [k, v] of m) {
227
- result.set(k, f(v));
228
- }
229
- return result;
230
- };
231
- /**
232
- * Transforms each value in the dictionary, also receiving the key.
233
- *
234
- * @example
235
- * ```ts
236
- * pipe(Dict.fromEntries([["a", 1], ["b", 2]]), Dict.mapWithKey((k, v) => `${k}:${v}`));
237
- * // ReadonlyMap { "a" => "a:1", "b" => "b:2" }
238
- * ```
239
- */
240
- Dict.mapWithKey = (f) => (m) => {
241
- const result = new globalThis.Map();
242
- for (const [k, v] of m) {
243
- result.set(k, f(k, v));
244
- }
245
- return result;
246
- };
247
- /**
248
- * Returns a new dictionary containing only the entries for which the predicate returns `true`.
249
- *
250
- * @example
251
- * ```ts
252
- * pipe(Dict.fromEntries([["a", 1], ["b", 3], ["c", 0]]), Dict.filter(n => n > 0));
253
- * // ReadonlyMap { "a" => 1, "b" => 3 }
254
- * ```
255
- */
256
- Dict.filter = (predicate) => (m) => {
257
- const result = new globalThis.Map();
258
- for (const [k, v] of m) {
259
- if (predicate(v))
260
- result.set(k, v);
261
- }
262
- return result;
263
- };
264
- /**
265
- * Returns a new dictionary containing only the entries for which the predicate returns `true`.
266
- * The predicate also receives the key.
267
- *
268
- * @example
269
- * ```ts
270
- * pipe(Dict.fromEntries([["a", 1], ["b", 2]]), Dict.filterWithKey((k, v) => k !== "a" && v > 0));
271
- * // ReadonlyMap { "b" => 2 }
272
- * ```
273
- */
274
- Dict.filterWithKey = (predicate) => (m) => {
275
- const result = new globalThis.Map();
276
- for (const [k, v] of m) {
277
- if (predicate(k, v))
278
- result.set(k, v);
279
- }
280
- return result;
281
- };
282
- /**
283
- * Removes all `None` values from a `ReadonlyMap<K, Option<A>>`, returning a plain
284
- * `ReadonlyMap<K, A>`. Useful when building dictionaries from fallible lookups.
285
- *
286
- * @example
287
- * ```ts
288
- * import { Option } from "@nlozgachev/pipelined/core";
289
- *
290
- * Dict.compact(Dict.fromEntries([
291
- * ["a", Option.some(1)],
292
- * ["b", Option.none()],
293
- * ["c", Option.some(3)],
294
- * ]));
295
- * // ReadonlyMap { "a" => 1, "c" => 3 }
296
- * ```
297
- */
298
- Dict.compact = (m) => {
299
- const result = new globalThis.Map();
300
- for (const [k, v] of m) {
301
- if (v.kind === "Some")
302
- result.set(k, v.value);
303
- }
304
- return result;
305
- };
306
- // ---------------------------------------------------------------------------
307
- // Combine
308
- // ---------------------------------------------------------------------------
309
- /**
310
- * Merges two dictionaries. When both contain the same key, the value from `other` takes
311
- * precedence.
312
- *
313
- * @example
314
- * ```ts
315
- * pipe(
316
- * Dict.fromEntries([["a", 1], ["b", 2]]),
317
- * Dict.union(Dict.fromEntries([["b", 3], ["c", 4]])),
318
- * );
319
- * // ReadonlyMap { "a" => 1, "b" => 3, "c" => 4 }
320
- * ```
321
- */
322
- Dict.union = (other) => (m) => {
323
- const result = new globalThis.Map(m);
324
- for (const [k, v] of other) {
325
- result.set(k, v);
326
- }
327
- return result;
328
- };
329
- /**
330
- * Returns a new dictionary containing only the entries whose keys appear in both dictionaries.
331
- * Values are taken from the left (base) dictionary.
332
- *
333
- * @example
334
- * ```ts
335
- * pipe(
336
- * Dict.fromEntries([["a", 1], ["b", 2], ["c", 3]]),
337
- * Dict.intersection(Dict.fromEntries([["b", 99], ["c", 0]])),
338
- * );
339
- * // ReadonlyMap { "b" => 2, "c" => 3 }
340
- * ```
341
- */
342
- Dict.intersection = (other) => (m) => {
343
- const result = new globalThis.Map();
344
- for (const [k, v] of m) {
345
- if (other.has(k))
346
- result.set(k, v);
347
- }
348
- return result;
349
- };
350
- /**
351
- * Returns a new dictionary containing only the entries whose keys do not appear in `other`.
352
- *
353
- * @example
354
- * ```ts
355
- * pipe(
356
- * Dict.fromEntries([["a", 1], ["b", 2], ["c", 3]]),
357
- * Dict.difference(Dict.fromEntries([["b", 0]])),
358
- * );
359
- * // ReadonlyMap { "a" => 1, "c" => 3 }
360
- * ```
361
- */
362
- Dict.difference = (other) => (m) => {
363
- const result = new globalThis.Map();
364
- for (const [k, v] of m) {
365
- if (!other.has(k))
366
- result.set(k, v);
367
- }
368
- return result;
369
- };
370
- // ---------------------------------------------------------------------------
371
- // Fold
372
- // ---------------------------------------------------------------------------
373
- /**
374
- * Folds the dictionary into a single value by applying `f` to each value in insertion order.
375
- * When you also need the key, use `reduceWithKey`.
376
- *
377
- * @example
378
- * ```ts
379
- * Dict.reduce(0, (acc, value) => acc + value)(
380
- * Dict.fromEntries([["a", 1], ["b", 2], ["c", 3]])
381
- * ); // 6
382
- * ```
383
- */
384
- Dict.reduce = (init, f) => (m) => {
385
- let acc = init;
386
- for (const v of m.values()) {
387
- acc = f(acc, v);
388
- }
389
- return acc;
390
- };
391
- /**
392
- * Folds the dictionary into a single value by applying `f` to each key-value pair in insertion
393
- * order.
394
- *
395
- * @example
396
- * ```ts
397
- * Dict.reduceWithKey("", (acc, value, key) => acc + key + ":" + value + " ")(
398
- * Dict.fromEntries([["a", 1], ["b", 2]])
399
- * ); // "a:1 b:2 "
400
- * ```
401
- */
402
- Dict.reduceWithKey = (init, f) => (m) => {
403
- let acc = init;
404
- for (const [k, v] of m) {
405
- acc = f(acc, v, k);
406
- }
407
- return acc;
408
- };
409
- // ---------------------------------------------------------------------------
410
- // Convert
411
- // ---------------------------------------------------------------------------
412
- /**
413
- * Converts a `ReadonlyMap<string, V>` to a plain object. Only meaningful when keys are strings.
414
- *
415
- * @example
416
- * ```ts
417
- * Dict.toRecord(Dict.fromEntries([["a", 1], ["b", 2]])); // { a: 1, b: 2 }
418
- * ```
419
- */
420
- Dict.toRecord = (m) => Object.fromEntries(m);
421
- })(Dict || (Dict = {}));
@@ -1,124 +0,0 @@
1
- import { Option } from "../Core/Option.js";
2
- /**
3
- * Number utilities for common operations. All transformation functions are data-last
4
- * and curried so they compose naturally with `pipe` and `Arr.map`.
5
- *
6
- * @example
7
- * ```ts
8
- * import { Num } from "@nlozgachev/pipelined/utils";
9
- * import { pipe } from "@nlozgachev/pipelined/composition";
10
- *
11
- * pipe(
12
- * Num.range(1, 6),
13
- * Arr.map(Num.multiply(2)),
14
- * Arr.filter(Num.between(4, 8))
15
- * ); // [4, 6, 8]
16
- * ```
17
- */
18
- export var Num;
19
- (function (Num) {
20
- /**
21
- * Generates an array of numbers from `from` to `to` (both inclusive),
22
- * stepping by `step` (default `1`). If `step` is negative or zero, or `from > to`,
23
- * returns an empty array. When `step` does not land exactly on `to`, the last value
24
- * is the largest reachable value that does not exceed `to`.
25
- *
26
- * @example
27
- * ```ts
28
- * Num.range(0, 5); // [0, 1, 2, 3, 4, 5]
29
- * Num.range(0, 10, 2); // [0, 2, 4, 6, 8, 10]
30
- * Num.range(0, 9, 2); // [0, 2, 4, 6, 8]
31
- * Num.range(5, 0); // []
32
- * Num.range(3, 3); // [3]
33
- * ```
34
- */
35
- Num.range = (from, to, step = 1) => {
36
- if (step <= 0 || from > to)
37
- return [];
38
- const count = Math.floor((to - from) / step) + 1;
39
- const result = new Array(count);
40
- for (let i = 0; i < count; i++) {
41
- result[i] = from + i * step;
42
- }
43
- return result;
44
- };
45
- /**
46
- * Clamps a number between `min` and `max` (both inclusive).
47
- *
48
- * @example
49
- * ```ts
50
- * pipe(150, Num.clamp(0, 100)); // 100
51
- * pipe(-5, Num.clamp(0, 100)); // 0
52
- * pipe(42, Num.clamp(0, 100)); // 42
53
- * ```
54
- */
55
- Num.clamp = (min, max) => (n) => Math.min(Math.max(n, min), max);
56
- /**
57
- * Returns `true` when the number is between `min` and `max` (both inclusive).
58
- *
59
- * @example
60
- * ```ts
61
- * pipe(5, Num.between(1, 10)); // true
62
- * pipe(0, Num.between(1, 10)); // false
63
- * pipe(10, Num.between(1, 10)); // true
64
- * ```
65
- */
66
- Num.between = (min, max) => (n) => n >= min && n <= max;
67
- /**
68
- * Parses a string as a number. Returns `None` when the result is `NaN`.
69
- *
70
- * @example
71
- * ```ts
72
- * Num.parse("42"); // Some(42)
73
- * Num.parse("3.14"); // Some(3.14)
74
- * Num.parse("abc"); // None
75
- * Num.parse(""); // None
76
- * ```
77
- */
78
- Num.parse = (s) => {
79
- if (s.trim() === "")
80
- return Option.none();
81
- const n = Number(s);
82
- return isNaN(n) ? Option.none() : Option.some(n);
83
- };
84
- /**
85
- * Adds `b` to a number. Data-last: use in `pipe` or `Arr.map`.
86
- *
87
- * @example
88
- * ```ts
89
- * pipe(5, Num.add(3)); // 8
90
- * pipe([1, 2, 3], Arr.map(Num.add(10))); // [11, 12, 13]
91
- * ```
92
- */
93
- Num.add = (b) => (a) => a + b;
94
- /**
95
- * Subtracts `b` from a number. Data-last: `subtract(b)(a)` = `a - b`.
96
- *
97
- * @example
98
- * ```ts
99
- * pipe(10, Num.subtract(3)); // 7
100
- * pipe([5, 10, 15], Arr.map(Num.subtract(2))); // [3, 8, 13]
101
- * ```
102
- */
103
- Num.subtract = (b) => (a) => a - b;
104
- /**
105
- * Multiplies a number by `b`. Data-last: use in `pipe` or `Arr.map`.
106
- *
107
- * @example
108
- * ```ts
109
- * pipe(6, Num.multiply(7)); // 42
110
- * pipe([1, 2, 3], Arr.map(Num.multiply(100))); // [100, 200, 300]
111
- * ```
112
- */
113
- Num.multiply = (b) => (a) => a * b;
114
- /**
115
- * Divides a number by `b`. Data-last: `divide(b)(a)` = `a / b`.
116
- *
117
- * @example
118
- * ```ts
119
- * pipe(20, Num.divide(4)); // 5
120
- * pipe([10, 20, 30], Arr.map(Num.divide(10))); // [1, 2, 3]
121
- * ```
122
- */
123
- Num.divide = (b) => (a) => a / b;
124
- })(Num || (Num = {}));