@nlozgachev/pipelined 0.62.0 → 0.63.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.
- package/README.md +5 -5
- package/dist/{Duration-B8joKzro.d.cts → Duration-DeyxG6VQ.d.cts} +20 -20
- package/dist/{Duration-B8joKzro.d.ts → Duration-DeyxG6VQ.d.ts} +20 -20
- package/dist/{InternalTypes-LdhLQx3N.d.ts → InternalTypes-CCXa8Kvr.d.ts} +11 -11
- package/dist/{InternalTypes-DuK_XpTi.d.cts → InternalTypes-GFn4RTwD.d.cts} +11 -11
- package/dist/{Validation-DZLizBZ0.d.ts → Validation-C5RGZUXy.d.ts} +384 -280
- package/dist/{Validation-DC3uUizM.d.cts → Validation-KFUpea_k.d.cts} +384 -280
- package/dist/composition.cjs +216 -95
- package/dist/composition.d.cts +8 -76
- package/dist/composition.d.ts +8 -76
- package/dist/composition.mjs +216 -95
- package/dist/core.cjs +4784 -996
- package/dist/core.d.cts +269 -769
- package/dist/core.d.ts +269 -769
- package/dist/core.mjs +4784 -996
- package/dist/data.cjs +2916 -1240
- package/dist/data.d.cts +479 -1967
- package/dist/data.d.ts +479 -1967
- package/dist/data.mjs +2916 -1240
- package/dist/index.cjs +6739 -2169
- package/dist/index.d.cts +3 -3
- package/dist/index.d.ts +3 -3
- package/dist/index.mjs +6718 -2148
- package/dist/types.cjs +175 -31
- package/dist/types.d.cts +6 -6
- package/dist/types.d.ts +6 -6
- package/dist/types.mjs +175 -31
- package/package.json +24 -13
package/dist/data.cjs
CHANGED
|
@@ -32,266 +32,1187 @@ __export(Data_exports, {
|
|
|
32
32
|
module.exports = __toCommonJS(Data_exports);
|
|
33
33
|
|
|
34
34
|
// src/Core/Deferred.ts
|
|
35
|
-
var
|
|
36
|
-
((
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
35
|
+
var fromPromise = (p) => ({ then: ((f) => p.then(f)) });
|
|
36
|
+
var toPromise = (d) => new globalThis.Promise((resolve) => d.then(resolve));
|
|
37
|
+
var Deferred = {
|
|
38
|
+
// --- from ---
|
|
39
|
+
from: {
|
|
40
|
+
/**
|
|
41
|
+
* Wraps a `Promise` or `Deferred` into a `Deferred`, structurally excluding rejection handlers,
|
|
42
|
+
* `.catch()`, `.finally()`, and chainable `.then()`.
|
|
43
|
+
*
|
|
44
|
+
* **Precondition**: `p` must never reject. If `p` rejects, the returned `Deferred` will
|
|
45
|
+
* never resolve — `await`-ing it will hang indefinitely. Use `Task.Result.tryCatch` to
|
|
46
|
+
* handle operations that may fail before converting to a `Deferred`.
|
|
47
|
+
*
|
|
48
|
+
* @example
|
|
49
|
+
* ```ts
|
|
50
|
+
* const d = Deferred.from.Promise(Promise.resolve("hello"));
|
|
51
|
+
* const value = await d; // "hello"
|
|
52
|
+
* ```
|
|
53
|
+
*/
|
|
54
|
+
Promise: fromPromise
|
|
55
|
+
},
|
|
56
|
+
// --- to ---
|
|
57
|
+
to: {
|
|
58
|
+
/**
|
|
59
|
+
* Converts a `Deferred` back into a `Promise`.
|
|
60
|
+
*
|
|
61
|
+
* @example
|
|
62
|
+
* ```ts
|
|
63
|
+
* const p = Deferred.to.Promise(Deferred.from.Promise(Promise.resolve(42)));
|
|
64
|
+
* // p is Promise<42>
|
|
65
|
+
* ```
|
|
66
|
+
*/
|
|
67
|
+
Promise: toPromise
|
|
68
|
+
},
|
|
69
|
+
/**
|
|
70
|
+
* Combines an array or tuple of `Deferred` values into a single `Deferred` of a tuple.
|
|
71
|
+
* Resolves when all input `Deferred`s resolve.
|
|
72
|
+
*
|
|
73
|
+
* @example
|
|
74
|
+
* ```ts
|
|
75
|
+
* const [a, b] = await Deferred.all([d1, d2]);
|
|
76
|
+
* ```
|
|
77
|
+
*/
|
|
78
|
+
all: (deferreds) => fromPromise(globalThis.Promise.all(deferreds.map((d) => toPromise(d)))),
|
|
79
|
+
/**
|
|
80
|
+
* Races multiple `Deferred` values and resolves with the first one to settle.
|
|
81
|
+
*
|
|
82
|
+
* @example
|
|
83
|
+
* ```ts
|
|
84
|
+
* const winner = await Deferred.race([d1, d2]);
|
|
85
|
+
* ```
|
|
86
|
+
*/
|
|
87
|
+
race: (deferreds) => fromPromise(globalThis.Promise.race(deferreds.map((d) => toPromise(d))))
|
|
88
|
+
};
|
|
51
89
|
|
|
52
90
|
// src/Core/Maybe.ts
|
|
53
91
|
var _none = { kind: "None" };
|
|
54
|
-
var
|
|
55
|
-
(
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
92
|
+
var makeSome = (value) => ({ kind: "Some", value });
|
|
93
|
+
var makeNone = () => _none;
|
|
94
|
+
var isSome = (data) => data.kind === "Some";
|
|
95
|
+
var isNone = (data) => data.kind === "None";
|
|
96
|
+
var Maybe = {
|
|
97
|
+
make: {
|
|
98
|
+
/**
|
|
99
|
+
* Creates a Some containing the given value.
|
|
100
|
+
*
|
|
101
|
+
* @example
|
|
102
|
+
* ```ts
|
|
103
|
+
* Maybe.make.some(42); // Some(42)
|
|
104
|
+
* ```
|
|
105
|
+
*/
|
|
106
|
+
some: makeSome,
|
|
107
|
+
/**
|
|
108
|
+
* Creates a None (empty Maybe).
|
|
109
|
+
*
|
|
110
|
+
* @example
|
|
111
|
+
* ```ts
|
|
112
|
+
* Maybe.make.none(); // None
|
|
113
|
+
* ```
|
|
114
|
+
*/
|
|
115
|
+
none: makeNone
|
|
116
|
+
},
|
|
117
|
+
is: {
|
|
118
|
+
/**
|
|
119
|
+
* Type guard that checks if a Maybe is Some.
|
|
120
|
+
*
|
|
121
|
+
* @example
|
|
122
|
+
* ```ts
|
|
123
|
+
* const value = Maybe.make.some(42);
|
|
124
|
+
* if (Maybe.is.some(value)) {
|
|
125
|
+
* console.log(value.value); // 42
|
|
126
|
+
* }
|
|
127
|
+
* ```
|
|
128
|
+
*/
|
|
129
|
+
some: isSome,
|
|
130
|
+
/**
|
|
131
|
+
* Type guard that checks if a Maybe is None.
|
|
132
|
+
*
|
|
133
|
+
* @example
|
|
134
|
+
* ```ts
|
|
135
|
+
* const value = Maybe.make.none();
|
|
136
|
+
* if (Maybe.is.none(value)) {
|
|
137
|
+
* console.log("No value present");
|
|
138
|
+
* }
|
|
139
|
+
* ```
|
|
140
|
+
*/
|
|
141
|
+
none: isNone
|
|
142
|
+
},
|
|
143
|
+
// --- to ---
|
|
144
|
+
to: {
|
|
145
|
+
/**
|
|
146
|
+
* Extracts the value from a Maybe, returning null if None.
|
|
147
|
+
*
|
|
148
|
+
* @example
|
|
149
|
+
* ```ts
|
|
150
|
+
* Maybe.to.nullable(Maybe.make.some(42)); // 42
|
|
151
|
+
* Maybe.to.nullable(Maybe.make.none()); // null
|
|
152
|
+
* ```
|
|
153
|
+
*/
|
|
154
|
+
nullable: (data) => isSome(data) ? data.value : null,
|
|
155
|
+
/**
|
|
156
|
+
* Extracts the value from a Maybe, returning undefined if None.
|
|
157
|
+
*
|
|
158
|
+
* @example
|
|
159
|
+
* ```ts
|
|
160
|
+
* Maybe.to.undefined(Maybe.make.some(42)); // 42
|
|
161
|
+
* Maybe.to.undefined(Maybe.make.none()); // undefined
|
|
162
|
+
* ```
|
|
163
|
+
*/
|
|
164
|
+
undefined: (data) => isSome(data) ? data.value : globalThis.undefined,
|
|
165
|
+
/**
|
|
166
|
+
* Converts a Maybe to a Result.
|
|
167
|
+
* Some becomes Ok, None becomes Err with the provided error.
|
|
168
|
+
*
|
|
169
|
+
* @example
|
|
170
|
+
* ```ts
|
|
171
|
+
* pipe(
|
|
172
|
+
* Maybe.make.some(42),
|
|
173
|
+
* Maybe.to.Result(() => "Value was missing")
|
|
174
|
+
* ); // Ok(42)
|
|
175
|
+
*
|
|
176
|
+
* pipe(
|
|
177
|
+
* Maybe.make.none(),
|
|
178
|
+
* Maybe.to.Result(() => "Value was missing")
|
|
179
|
+
* ); // Err("Value was missing")
|
|
180
|
+
* ```
|
|
181
|
+
*/
|
|
182
|
+
Result: (onNone) => (data) => isSome(data) ? Result.make.ok(data.value) : Result.make.err(onNone())
|
|
183
|
+
},
|
|
184
|
+
// --- from ---
|
|
185
|
+
from: {
|
|
186
|
+
/**
|
|
187
|
+
* Creates a Maybe from a nullable value.
|
|
188
|
+
* Returns None if the value is null or undefined, Some otherwise.
|
|
189
|
+
*
|
|
190
|
+
* @example
|
|
191
|
+
* ```ts
|
|
192
|
+
* Maybe.from.nullable(null); // None
|
|
193
|
+
* Maybe.from.nullable(42); // Some(42)
|
|
194
|
+
* ```
|
|
195
|
+
*/
|
|
196
|
+
nullable: (value) => value === null || value === void 0 ? makeNone() : makeSome(value),
|
|
197
|
+
/**
|
|
198
|
+
* Creates a Maybe from a predicate applied to a value.
|
|
199
|
+
* Returns Some if the predicate passes, None otherwise.
|
|
200
|
+
*
|
|
201
|
+
* @example
|
|
202
|
+
* ```ts
|
|
203
|
+
* Maybe.from.Predicate((n: number) => n >= 18)(21); // Some(21)
|
|
204
|
+
* Maybe.from.Predicate((n: number) => n >= 18)(15); // None
|
|
205
|
+
*
|
|
206
|
+
* pipe("hello", Maybe.from.Predicate((s: string) => s.length > 0)); // Some("hello")
|
|
207
|
+
* pipe("", Maybe.from.Predicate((s: string) => s.length > 0)); // None
|
|
208
|
+
* ```
|
|
209
|
+
*/
|
|
210
|
+
Predicate: (pred) => (a) => pred(a) ? makeSome(a) : makeNone(),
|
|
211
|
+
/**
|
|
212
|
+
* Creates a Maybe from a Result.
|
|
213
|
+
* Ok becomes Some, Err becomes None (the error is discarded).
|
|
214
|
+
*
|
|
215
|
+
* @example
|
|
216
|
+
* ```ts
|
|
217
|
+
* Maybe.from.Result(Result.make.ok(42)); // Some(42)
|
|
218
|
+
* Maybe.from.Result(Result.make.err("oops")); // None
|
|
219
|
+
* ```
|
|
220
|
+
*/
|
|
221
|
+
Result: (data) => Result.is.ok(data) ? makeSome(data.value) : makeNone()
|
|
222
|
+
},
|
|
223
|
+
/**
|
|
224
|
+
* Transforms the value inside a Maybe if it exists.
|
|
225
|
+
*
|
|
226
|
+
* @example
|
|
227
|
+
* ```ts
|
|
228
|
+
* pipe(Maybe.make.some(5), Maybe.map(n => n * 2)); // Some(10)
|
|
229
|
+
* pipe(Maybe.make.none(), Maybe.map(n => n * 2)); // None
|
|
230
|
+
* ```
|
|
231
|
+
*/
|
|
232
|
+
map: (f) => (data) => isSome(data) ? makeSome(f(data.value)) : data,
|
|
233
|
+
/**
|
|
234
|
+
* Chains Maybe computations. If the first is Some, passes the value to f.
|
|
235
|
+
* If the first is None, propagates None.
|
|
236
|
+
*
|
|
237
|
+
* @example
|
|
238
|
+
* ```ts
|
|
239
|
+
* const parseNumber = (s: string): Maybe<number> => {
|
|
240
|
+
* const n = parseInt(s, 10);
|
|
241
|
+
* return isNaN(n) ? Maybe.make.none() : Maybe.make.some(n);
|
|
242
|
+
* };
|
|
243
|
+
*
|
|
244
|
+
* pipe(Maybe.make.some("42"), Maybe.chain(parseNumber)); // Some(42)
|
|
245
|
+
* pipe(Maybe.make.some("abc"), Maybe.chain(parseNumber)); // None
|
|
246
|
+
* ```
|
|
247
|
+
*/
|
|
248
|
+
chain: (f) => (data) => isSome(data) ? f(data.value) : data,
|
|
249
|
+
/**
|
|
250
|
+
* Extracts the value from a Maybe by providing handlers for both cases.
|
|
251
|
+
*
|
|
252
|
+
* @example
|
|
253
|
+
* ```ts
|
|
254
|
+
* pipe(
|
|
255
|
+
* Maybe.make.some(5),
|
|
256
|
+
* Maybe.fold(
|
|
257
|
+
* () => "No value",
|
|
258
|
+
* n => `Value: ${n}`
|
|
259
|
+
* )
|
|
260
|
+
* ); // "Value: 5"
|
|
261
|
+
* ```
|
|
262
|
+
*/
|
|
263
|
+
fold: (onNone, onSome) => (data) => isSome(data) ? onSome(data.value) : onNone(),
|
|
264
|
+
/**
|
|
265
|
+
* Pattern matches on a Maybe, returning the result of the matching case.
|
|
266
|
+
*
|
|
267
|
+
* @example
|
|
268
|
+
* ```ts
|
|
269
|
+
* pipe(
|
|
270
|
+
* optionUser,
|
|
271
|
+
* Maybe.match({
|
|
272
|
+
* some: user => `Hello, ${user.name}`,
|
|
273
|
+
* none: () => "Hello, stranger"
|
|
274
|
+
* })
|
|
275
|
+
* );
|
|
276
|
+
* ```
|
|
277
|
+
*/
|
|
278
|
+
match: (cases) => (data) => isSome(data) ? cases.some(data.value) : cases.none(),
|
|
279
|
+
/**
|
|
280
|
+
* Returns the value inside a Maybe, or a default value if None.
|
|
281
|
+
* The default is a thunk `() => B` — evaluated only when the Maybe is None.
|
|
282
|
+
* The default can be a different type, widening the result to `A | B`.
|
|
283
|
+
*
|
|
284
|
+
* @example
|
|
285
|
+
* ```ts
|
|
286
|
+
* pipe(Maybe.make.some(5), Maybe.getOrElse(() => 0)); // 5
|
|
287
|
+
* pipe(Maybe.make.none(), Maybe.getOrElse(() => 0)); // 0
|
|
288
|
+
* pipe(Maybe.make.none<string>(), Maybe.getOrElse(() => null)); // null — typed as string | null
|
|
289
|
+
* ```
|
|
290
|
+
*/
|
|
291
|
+
getOrElse: (defaultValue) => (data) => isSome(data) ? data.value : defaultValue(),
|
|
292
|
+
/**
|
|
293
|
+
* Executes a side effect on the value without changing the Maybe.
|
|
294
|
+
* Useful for logging or debugging.
|
|
295
|
+
*
|
|
296
|
+
* @example
|
|
297
|
+
* ```ts
|
|
298
|
+
* pipe(
|
|
299
|
+
* Maybe.make.some(5),
|
|
300
|
+
* Maybe.tap(n => console.log("Value:", n)),
|
|
301
|
+
* Maybe.map(n => n * 2)
|
|
302
|
+
* );
|
|
303
|
+
* ```
|
|
304
|
+
*/
|
|
305
|
+
tap: (f) => (data) => {
|
|
306
|
+
if (isSome(data)) {
|
|
85
307
|
f(data.value);
|
|
86
308
|
}
|
|
87
309
|
return data;
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
310
|
+
},
|
|
311
|
+
/**
|
|
312
|
+
* Filters a Maybe based on a predicate.
|
|
313
|
+
* Returns None if the predicate returns false or if the Maybe is already None.
|
|
314
|
+
*
|
|
315
|
+
* @example
|
|
316
|
+
* ```ts
|
|
317
|
+
* pipe(Maybe.make.some(5), Maybe.filter(n => n > 3)); // Some(5)
|
|
318
|
+
* pipe(Maybe.make.some(2), Maybe.filter(n => n > 3)); // None
|
|
319
|
+
* ```
|
|
320
|
+
*/
|
|
321
|
+
filter: (predicate) => (data) => isSome(data) ? predicate(data.value) ? data : makeNone() : data,
|
|
322
|
+
/**
|
|
323
|
+
* Recovers from a None by providing a fallback Maybe.
|
|
324
|
+
* The fallback can produce a different type, widening the result to `Maybe<A | B>`.
|
|
325
|
+
*
|
|
326
|
+
* @example
|
|
327
|
+
* ```ts
|
|
328
|
+
* pipe(Maybe.make.none(), Maybe.recover(() => Maybe.make.some(42))); // Some(42)
|
|
329
|
+
* pipe(Maybe.make.some(10), Maybe.recover(() => Maybe.make.some(42))); // Some(10)
|
|
330
|
+
* ```
|
|
331
|
+
*/
|
|
332
|
+
recover: (fallback) => (data) => isSome(data) ? data : fallback(),
|
|
333
|
+
/**
|
|
334
|
+
* Applies a function wrapped in a Maybe to a value wrapped in a Maybe.
|
|
335
|
+
*
|
|
336
|
+
* @example
|
|
337
|
+
* ```ts
|
|
338
|
+
* const add = (a: number) => (b: number) => a + b;
|
|
339
|
+
* pipe(
|
|
340
|
+
* Maybe.make.some(add),
|
|
341
|
+
* Maybe.ap(Maybe.make.some(5)),
|
|
342
|
+
* Maybe.ap(Maybe.make.some(3))
|
|
343
|
+
* ); // Some(8)
|
|
344
|
+
* ```
|
|
345
|
+
*/
|
|
346
|
+
ap: (arg) => (data) => isSome(data) && isSome(arg) ? makeSome(data.value(arg.value)) : makeNone(),
|
|
347
|
+
/**
|
|
348
|
+
* Converts a Maybe value into an object containing a single property.
|
|
349
|
+
* Initiates the pipeline accumulator record.
|
|
350
|
+
*
|
|
351
|
+
* @example
|
|
352
|
+
* ```ts
|
|
353
|
+
* pipe(Maybe.make.some(42), Maybe.bindTo("value")); // Some({ value: 42 })
|
|
354
|
+
* ```
|
|
355
|
+
*/
|
|
356
|
+
bindTo: (key) => (data) => isSome(data) ? makeSome({ [key]: data.value }) : data,
|
|
357
|
+
/**
|
|
358
|
+
* Evaluates a new Maybe using the current accumulator and attaches the output to a new key.
|
|
359
|
+
*
|
|
360
|
+
* @example
|
|
361
|
+
* ```ts
|
|
362
|
+
* pipe(
|
|
363
|
+
* Maybe.make.some({ a: 1 }),
|
|
364
|
+
* Maybe.bind("b", ({ a }) => Maybe.make.some(a + 1))
|
|
365
|
+
* ); // Some({ a: 1, b: 2 })
|
|
366
|
+
* ```
|
|
367
|
+
*/
|
|
368
|
+
bind: (key, f) => (data) => {
|
|
369
|
+
if (!isSome(data)) {
|
|
370
|
+
return data;
|
|
371
|
+
}
|
|
372
|
+
const mb = f(data.value);
|
|
373
|
+
return isSome(mb) ? makeSome({ ...data.value, [key]: mb.value }) : mb;
|
|
374
|
+
},
|
|
375
|
+
/**
|
|
376
|
+
* Combines a record of Maybes into a single Maybe of a record.
|
|
377
|
+
* Evaluates fields in key order and short-circuits on the first None.
|
|
378
|
+
*
|
|
379
|
+
* @example
|
|
380
|
+
* ```ts
|
|
381
|
+
* Maybe.struct({
|
|
382
|
+
* name: Maybe.make.some("Alice"),
|
|
383
|
+
* age: Maybe.make.some(30)
|
|
384
|
+
* }); // Some({ name: "Alice", age: 30 })
|
|
385
|
+
* ```
|
|
386
|
+
*/
|
|
387
|
+
struct: (fields) => {
|
|
97
388
|
const result = {};
|
|
98
389
|
for (const key in fields) {
|
|
99
390
|
if (Object.hasOwn(fields, key)) {
|
|
100
391
|
const res = fields[key];
|
|
101
|
-
if (
|
|
392
|
+
if (isNone(res)) {
|
|
102
393
|
return res;
|
|
103
394
|
}
|
|
104
395
|
result[key] = res.value;
|
|
105
396
|
}
|
|
106
397
|
}
|
|
107
|
-
return
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
|
|
398
|
+
return makeSome(result);
|
|
399
|
+
},
|
|
400
|
+
/**
|
|
401
|
+
* Swaps the outer `Maybe` and inner `Result` context.
|
|
402
|
+
* `Some(Ok(a))` becomes `Ok(Some(a))`, `Some(Err(e))` becomes `Err(e)`, and `None` becomes `Ok(None)`.
|
|
403
|
+
*
|
|
404
|
+
* @example
|
|
405
|
+
* ```ts
|
|
406
|
+
* Maybe.transposeResult(Maybe.make.some(Result.make.ok(42))); // Ok(Some(42))
|
|
407
|
+
* Maybe.transposeResult(Maybe.make.some(Result.make.err("e"))); // Err("e")
|
|
408
|
+
* Maybe.transposeResult(Maybe.make.none()); // Ok(None)
|
|
409
|
+
* ```
|
|
410
|
+
*/
|
|
411
|
+
transposeResult: (data) => isNone(data) ? Result.make.ok(makeNone()) : Result.is.ok(data.value) ? Result.make.ok(makeSome(data.value.value)) : Result.make.err(data.value.error)
|
|
412
|
+
};
|
|
111
413
|
|
|
112
414
|
// src/Core/Result.ts
|
|
113
|
-
var
|
|
114
|
-
(
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
415
|
+
var makeOk = (value) => ({ kind: "Ok", value });
|
|
416
|
+
var makeErr = (e) => ({ kind: "Err", error: e });
|
|
417
|
+
var isOk = (data) => data.kind === "Ok";
|
|
418
|
+
var isErr = (data) => data.kind === "Err";
|
|
419
|
+
var Result = {
|
|
420
|
+
make: {
|
|
421
|
+
/**
|
|
422
|
+
* Creates a successful Result with the given value.
|
|
423
|
+
*
|
|
424
|
+
* @example
|
|
425
|
+
* ```ts
|
|
426
|
+
* Result.make.ok(42); // Ok(42)
|
|
427
|
+
* ```
|
|
428
|
+
*/
|
|
429
|
+
ok: makeOk,
|
|
430
|
+
/**
|
|
431
|
+
* Creates a failed Result with the given error.
|
|
432
|
+
*
|
|
433
|
+
* @example
|
|
434
|
+
* ```ts
|
|
435
|
+
* Result.make.err("Error message"); // Err("Error message")
|
|
436
|
+
* ```
|
|
437
|
+
*/
|
|
438
|
+
err: makeErr
|
|
439
|
+
},
|
|
440
|
+
is: {
|
|
441
|
+
/**
|
|
442
|
+
* Type guard that checks if a Result is Ok.
|
|
443
|
+
*
|
|
444
|
+
* @example
|
|
445
|
+
* ```ts
|
|
446
|
+
* const res = Result.make.ok(42);
|
|
447
|
+
* if (Result.is.ok(res)) {
|
|
448
|
+
* console.log(res.value); // 42
|
|
449
|
+
* }
|
|
450
|
+
* ```
|
|
451
|
+
*/
|
|
452
|
+
ok: isOk,
|
|
453
|
+
/**
|
|
454
|
+
* Type guard that checks if a Result is Err.
|
|
455
|
+
*
|
|
456
|
+
* @example
|
|
457
|
+
* ```ts
|
|
458
|
+
* const res = Result.make.err("failed");
|
|
459
|
+
* if (Result.is.err(res)) {
|
|
460
|
+
* console.log(res.error); // "failed"
|
|
461
|
+
* }
|
|
462
|
+
* ```
|
|
463
|
+
*/
|
|
464
|
+
err: isErr
|
|
465
|
+
},
|
|
466
|
+
/**
|
|
467
|
+
* Creates a Result from a synchronous thunk that may throw.
|
|
468
|
+
* Catches any errors and transforms them using the `onError` function.
|
|
469
|
+
*
|
|
470
|
+
* @example
|
|
471
|
+
* ```ts
|
|
472
|
+
* const result = Result.tryCatch(
|
|
473
|
+
* () => JSON.parse(rawString),
|
|
474
|
+
* { onError: (e) => `Parse error: ${e}` }
|
|
475
|
+
* );
|
|
476
|
+
* ```
|
|
477
|
+
*/
|
|
478
|
+
tryCatch: (f, options) => {
|
|
126
479
|
try {
|
|
127
|
-
return
|
|
480
|
+
return makeOk(f());
|
|
128
481
|
} catch (error) {
|
|
129
|
-
return
|
|
130
|
-
}
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
482
|
+
return makeErr(options.onError(error));
|
|
483
|
+
}
|
|
484
|
+
},
|
|
485
|
+
/**
|
|
486
|
+
* Transforms the success value inside a Result.
|
|
487
|
+
*
|
|
488
|
+
* @example
|
|
489
|
+
* ```ts
|
|
490
|
+
* pipe(Result.make.ok(5), Result.map(n => n * 2)); // Ok(10)
|
|
491
|
+
* pipe(Result.make.err("error"), Result.map(n => n * 2)); // Err("error")
|
|
492
|
+
* ```
|
|
493
|
+
*/
|
|
494
|
+
map: (f) => (data) => isOk(data) ? makeOk(f(data.value)) : data,
|
|
495
|
+
/**
|
|
496
|
+
* Transforms the error value inside a Result.
|
|
497
|
+
*
|
|
498
|
+
* @example
|
|
499
|
+
* ```ts
|
|
500
|
+
* pipe(Result.make.err("oops"), Result.mapError(e => e.toUpperCase())); // Err("OOPS")
|
|
501
|
+
* ```
|
|
502
|
+
*/
|
|
503
|
+
mapError: (f) => (data) => isErr(data) ? makeErr(f(data.error)) : data,
|
|
504
|
+
/**
|
|
505
|
+
* Chains Result computations. If the first is Ok, passes the value to f.
|
|
506
|
+
* If the first is Err, propagates the error.
|
|
507
|
+
*
|
|
508
|
+
* @example
|
|
509
|
+
* ```ts
|
|
510
|
+
* const validatePositive = (n: number): Result<string, number> =>
|
|
511
|
+
* n > 0 ? Result.make.ok(n) : Result.make.err("Must be positive");
|
|
512
|
+
*
|
|
513
|
+
* pipe(Result.make.ok(5), Result.chain(validatePositive)); // Ok(5)
|
|
514
|
+
* pipe(Result.make.ok(-1), Result.chain(validatePositive)); // Err("Must be positive")
|
|
515
|
+
* ```
|
|
516
|
+
*/
|
|
517
|
+
chain: (f) => (data) => isOk(data) ? f(data.value) : data,
|
|
518
|
+
/**
|
|
519
|
+
* Extracts the value from a Result by providing handlers for both cases.
|
|
520
|
+
*
|
|
521
|
+
* @example
|
|
522
|
+
* ```ts
|
|
523
|
+
* pipe(
|
|
524
|
+
* Result.make.ok(5),
|
|
525
|
+
* Result.fold(
|
|
526
|
+
* e => `Error: ${e}`,
|
|
527
|
+
* n => `Value: ${n}`
|
|
528
|
+
* )
|
|
529
|
+
* ); // "Value: 5"
|
|
530
|
+
* ```
|
|
531
|
+
*/
|
|
532
|
+
fold: (onErr, onOk) => (data) => isOk(data) ? onOk(data.value) : onErr(data.error),
|
|
533
|
+
/**
|
|
534
|
+
* Pattern matches on a Result, returning the result of the matching case.
|
|
535
|
+
*
|
|
536
|
+
* @example
|
|
537
|
+
* ```ts
|
|
538
|
+
* pipe(
|
|
539
|
+
* result,
|
|
540
|
+
* Result.match({
|
|
541
|
+
* ok: value => `Got ${value}`,
|
|
542
|
+
* err: error => `Failed: ${error}`
|
|
543
|
+
* })
|
|
544
|
+
* );
|
|
545
|
+
* ```
|
|
546
|
+
*/
|
|
547
|
+
match: (cases) => (data) => isOk(data) ? cases.ok(data.value) : cases.err(data.error),
|
|
548
|
+
/**
|
|
549
|
+
* Returns the success value or a default value if the Result is an error.
|
|
550
|
+
* The default is a thunk `() => B` — evaluated only when the Result is Err.
|
|
551
|
+
* The default can be a different type, widening the result to `A | B`.
|
|
552
|
+
*
|
|
553
|
+
* @example
|
|
554
|
+
* ```ts
|
|
555
|
+
* pipe(Result.make.ok(5), Result.getOrElse(() => 0)); // 5
|
|
556
|
+
* pipe(Result.make.err("error"), Result.getOrElse(() => 0)); // 0
|
|
557
|
+
* pipe(Result.make.err("error"), Result.getOrElse(() => null)); // null — typed as number | null
|
|
558
|
+
* ```
|
|
559
|
+
*/
|
|
560
|
+
getOrElse: (defaultValue) => (data) => isOk(data) ? data.value : defaultValue(),
|
|
561
|
+
/**
|
|
562
|
+
* Executes a side effect on the success value without changing the Result.
|
|
563
|
+
* Useful for logging or debugging.
|
|
564
|
+
*
|
|
565
|
+
* @example
|
|
566
|
+
* ```ts
|
|
567
|
+
* pipe(
|
|
568
|
+
* Result.make.ok(5),
|
|
569
|
+
* Result.tap(n => console.log("Value:", n)),
|
|
570
|
+
* Result.map(n => n * 2)
|
|
571
|
+
* );
|
|
572
|
+
* ```
|
|
573
|
+
*/
|
|
574
|
+
tap: (f) => (data) => {
|
|
575
|
+
if (isOk(data)) {
|
|
140
576
|
f(data.value);
|
|
141
577
|
}
|
|
142
578
|
return data;
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
|
|
579
|
+
},
|
|
580
|
+
/**
|
|
581
|
+
* Executes a side effect on the error value without changing the Result.
|
|
582
|
+
* Useful for logging or reporting errors.
|
|
583
|
+
*
|
|
584
|
+
* @example
|
|
585
|
+
* ```ts
|
|
586
|
+
* pipe(
|
|
587
|
+
* Result.make.err("not found"),
|
|
588
|
+
* Result.tapError(e => console.error("validation failed:", e)),
|
|
589
|
+
* Result.chain(save),
|
|
590
|
+
* )
|
|
591
|
+
* ```
|
|
592
|
+
*/
|
|
593
|
+
tapError: (f) => (data) => {
|
|
594
|
+
if (isErr(data)) {
|
|
146
595
|
f(data.error);
|
|
147
596
|
}
|
|
148
597
|
return data;
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
598
|
+
},
|
|
599
|
+
// --- from ---
|
|
600
|
+
from: {
|
|
601
|
+
/**
|
|
602
|
+
* Creates a Result from a predicate applied to a value.
|
|
603
|
+
* Returns Ok if the predicate passes, Err from onFalse otherwise.
|
|
604
|
+
*
|
|
605
|
+
* @example
|
|
606
|
+
* ```ts
|
|
607
|
+
* pipe(5, Result.from.Predicate(n => n > 0, n => `${n} is not positive`)); // Ok(5)
|
|
608
|
+
* pipe(-1, Result.from.Predicate(n => n > 0, n => `${n} is not positive`)); // Err("-1 is not positive")
|
|
609
|
+
* pipe("", Result.from.Predicate(s => s.length > 0, () => "empty string")); // Err("empty string")
|
|
610
|
+
* ```
|
|
611
|
+
*/
|
|
612
|
+
Predicate: (pred, onFalse) => (a) => pred(a) ? makeOk(a) : makeErr(onFalse(a)),
|
|
613
|
+
/**
|
|
614
|
+
* Creates a Result from a nullable value.
|
|
615
|
+
* Returns Ok if the value is not null or undefined, error from onNull otherwise.
|
|
616
|
+
*
|
|
617
|
+
* @example
|
|
618
|
+
* ```ts
|
|
619
|
+
* pipe(null, Result.from.nullable(() => "is null")); // Err("is null")
|
|
620
|
+
* pipe(42, Result.from.nullable(() => "is null")); // Ok(42)
|
|
621
|
+
* ```
|
|
622
|
+
*/
|
|
623
|
+
nullable: (onNull) => (value) => value === null || value === void 0 ? makeErr(onNull()) : makeOk(value),
|
|
624
|
+
/**
|
|
625
|
+
* Creates a Result from a Maybe.
|
|
626
|
+
* Some becomes Ok, None becomes error from onNone.
|
|
627
|
+
*
|
|
628
|
+
* @example
|
|
629
|
+
* ```ts
|
|
630
|
+
* pipe(Maybe.make.none(), Result.from.Maybe(() => "is none")); // Err("is none")
|
|
631
|
+
* pipe(Maybe.make.some(42), Result.from.Maybe(() => "is none")); // Ok(42)
|
|
632
|
+
* ```
|
|
633
|
+
*/
|
|
634
|
+
Maybe: (onNone) => (maybe) => Maybe.is.none(maybe) ? makeErr(onNone()) : makeOk(maybe.value),
|
|
635
|
+
/**
|
|
636
|
+
* Converts a `Validation` to a `Result`, combining accumulated errors using `combineErrors`.
|
|
637
|
+
* `Passed(a)` becomes `Ok(a)`; `Failed(errors)` becomes `Err(combineErrors(errors))`.
|
|
638
|
+
*
|
|
639
|
+
* @example
|
|
640
|
+
* ```ts
|
|
641
|
+
* Result.from.Validation((errors) => errors.join(", "))(Validation.make.failed("error1")); // Err("error1")
|
|
642
|
+
* ```
|
|
643
|
+
*/
|
|
644
|
+
Validation: (combineErrors) => (val) => Validation.is.passed(val) ? makeOk(val.value) : makeErr(combineErrors(val.errors))
|
|
645
|
+
},
|
|
646
|
+
/**
|
|
647
|
+
* Recovers from an error by providing a fallback Result.
|
|
648
|
+
* The fallback can produce a different success type, widening the result to `Result<E, A | B>`.
|
|
649
|
+
*/
|
|
650
|
+
recover: (fallback) => (data) => isOk(data) ? data : fallback(data.error),
|
|
651
|
+
/**
|
|
652
|
+
* Recovers from an error unless the predicate `isBlocked` returns true for that error.
|
|
653
|
+
* The fallback can produce a different success type, widening the result to `Result<E, A | B>`.
|
|
654
|
+
*
|
|
655
|
+
* @example
|
|
656
|
+
* ```ts
|
|
657
|
+
* pipe(
|
|
658
|
+
* Result.make.err(new Error("not found")),
|
|
659
|
+
* Result.recoverUnless(e => e.message === "fatal", () => Result.make.ok(0))
|
|
660
|
+
* ); // Ok(0)
|
|
661
|
+
* ```
|
|
662
|
+
*/
|
|
663
|
+
recoverUnless: (isBlocked, fallback) => (data) => isErr(data) && !isBlocked(data.error) ? fallback() : data,
|
|
664
|
+
// --- to ---
|
|
665
|
+
to: {
|
|
666
|
+
/**
|
|
667
|
+
* Converts a Result to a Maybe.
|
|
668
|
+
* Ok becomes Some, Err becomes None (the error is discarded).
|
|
669
|
+
*
|
|
670
|
+
* @example
|
|
671
|
+
* ```ts
|
|
672
|
+
* Result.to.Maybe(Result.make.ok(42)); // Some(42)
|
|
673
|
+
* Result.to.Maybe(Result.make.err("oops")); // None
|
|
674
|
+
* ```
|
|
675
|
+
*/
|
|
676
|
+
Maybe: (data) => isOk(data) ? Maybe.make.some(data.value) : Maybe.make.none(),
|
|
677
|
+
/**
|
|
678
|
+
* Converts a `Result` to a `Validation`. `Ok(a)` becomes `Passed(a)`; `Err(e)` becomes `Failed([e])`.
|
|
679
|
+
*
|
|
680
|
+
* @example
|
|
681
|
+
* ```ts
|
|
682
|
+
* Result.to.Validation(Result.make.ok(42)); // Passed(42)
|
|
683
|
+
* Result.to.Validation(Result.make.err("bad")); // Failed(["bad"])
|
|
684
|
+
* ```
|
|
685
|
+
*/
|
|
686
|
+
Validation: (data) => Validation.from.Result(data)
|
|
687
|
+
},
|
|
688
|
+
/**
|
|
689
|
+
* Swaps the outer `Result` and inner `Maybe` context.
|
|
690
|
+
* `Ok(Some(a))` becomes `Some(Ok(a))`, `Ok(None)` becomes `None`, and `Err(e)` becomes `Some(Err(e))`.
|
|
691
|
+
*
|
|
692
|
+
* @example
|
|
693
|
+
* ```ts
|
|
694
|
+
* Result.transposeMaybe(Result.make.ok(Maybe.make.some(42))); // Some(Ok(42))
|
|
695
|
+
* Result.transposeMaybe(Result.make.ok(Maybe.make.none())); // None
|
|
696
|
+
* Result.transposeMaybe(Result.make.err("error")); // Some(Err("error"))
|
|
697
|
+
* ```
|
|
698
|
+
*/
|
|
699
|
+
transposeMaybe: (data) => isErr(data) ? Maybe.make.some(data) : Maybe.is.some(data.value) ? Maybe.make.some(makeOk(data.value.value)) : Maybe.make.none(),
|
|
700
|
+
/**
|
|
701
|
+
* Applies a function wrapped in a Result to a value wrapped in a Result.
|
|
702
|
+
*
|
|
703
|
+
* @example
|
|
704
|
+
* ```ts
|
|
705
|
+
* const add = (a: number) => (b: number) => a + b;
|
|
706
|
+
* pipe(
|
|
707
|
+
* Result.make.ok(add),
|
|
708
|
+
* Result.ap(Result.make.ok(5)),
|
|
709
|
+
* Result.ap(Result.make.ok(3))
|
|
710
|
+
* ); // Ok(8)
|
|
711
|
+
* ```
|
|
712
|
+
*/
|
|
713
|
+
ap: (arg) => (data) => isOk(data) && isOk(arg) ? makeOk(data.value(arg.value)) : isErr(data) ? data : arg,
|
|
714
|
+
/**
|
|
715
|
+
* Converts a Result value into an object containing a single property.
|
|
716
|
+
* Initiates the pipeline accumulator record.
|
|
717
|
+
*
|
|
718
|
+
* @example
|
|
719
|
+
* ```ts
|
|
720
|
+
* pipe(Result.make.ok(42), Result.bindTo("value")); // Ok({ value: 42 })
|
|
721
|
+
* ```
|
|
722
|
+
*/
|
|
723
|
+
bindTo: (key) => (data) => isOk(data) ? makeOk({ [key]: data.value }) : data,
|
|
724
|
+
/**
|
|
725
|
+
* Evaluates a new Result using the current accumulator and attaches the output to a new key.
|
|
726
|
+
*
|
|
727
|
+
* @example
|
|
728
|
+
* ```ts
|
|
729
|
+
* pipe(
|
|
730
|
+
* Result.make.ok({ a: 1 }),
|
|
731
|
+
* Result.bind("b", ({ a }) => Result.make.ok(a + 1))
|
|
732
|
+
* ); // Ok({ a: 1, b: 2 })
|
|
733
|
+
* ```
|
|
734
|
+
*/
|
|
735
|
+
bind: (key, f) => (data) => {
|
|
736
|
+
if (!isOk(data)) {
|
|
737
|
+
return data;
|
|
738
|
+
}
|
|
739
|
+
const res = f(data.value);
|
|
740
|
+
return isOk(res) ? makeOk({ ...data.value, [key]: res.value }) : res;
|
|
741
|
+
},
|
|
742
|
+
/**
|
|
743
|
+
* Combines a record of Results into a single Result of a record.
|
|
744
|
+
* Evaluates fields in key order and short-circuits on the first failure.
|
|
745
|
+
*
|
|
746
|
+
* @example
|
|
747
|
+
* ```ts
|
|
748
|
+
* Result.struct({
|
|
749
|
+
* name: Result.make.ok("Alice"),
|
|
750
|
+
* age: Result.make.ok(30)
|
|
751
|
+
* }); // Ok({ name: "Alice", age: 30 })
|
|
752
|
+
* ```
|
|
753
|
+
*/
|
|
754
|
+
struct: (fields) => {
|
|
171
755
|
const result = {};
|
|
172
756
|
for (const key in fields) {
|
|
173
757
|
if (Object.hasOwn(fields, key)) {
|
|
174
758
|
const res = fields[key];
|
|
175
|
-
if (
|
|
759
|
+
if (isErr(res)) {
|
|
176
760
|
return res;
|
|
177
761
|
}
|
|
178
762
|
result[key] = res.value;
|
|
179
763
|
}
|
|
180
764
|
}
|
|
181
|
-
return
|
|
182
|
-
}
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
765
|
+
return makeOk(result);
|
|
766
|
+
},
|
|
767
|
+
/**
|
|
768
|
+
* Narrows an `Ok` value with a predicate, converting to `Err(onFail(a))` if the predicate returns false.
|
|
769
|
+
*
|
|
770
|
+
* @example
|
|
771
|
+
* ```ts
|
|
772
|
+
* pipe(
|
|
773
|
+
* Result.make.ok(15),
|
|
774
|
+
* Result.ensure((n) => n >= 18, (n) => `Age ${n} is below 18`)
|
|
775
|
+
* ); // Err("Age 15 is below 18")
|
|
776
|
+
* ```
|
|
777
|
+
*/
|
|
778
|
+
ensure: (predicate, onFail) => (data) => isErr(data) ? data : predicate(data.value) ? data : makeErr(onFail(data.value)),
|
|
779
|
+
/**
|
|
780
|
+
* Transforms both branches of a Result simultaneously.
|
|
781
|
+
* Applies `onErr` to `Err` values and `onOk` to `Ok` values.
|
|
782
|
+
*
|
|
783
|
+
* @example
|
|
784
|
+
* ```ts
|
|
785
|
+
* pipe(
|
|
786
|
+
* Result.make.ok(5),
|
|
787
|
+
* Result.bimap(
|
|
788
|
+
* (e) => `Error: ${e}`,
|
|
789
|
+
* (n) => n * 2
|
|
790
|
+
* )
|
|
791
|
+
* ); // Ok(10)
|
|
792
|
+
* ```
|
|
793
|
+
*/
|
|
794
|
+
bimap: (onErr, onOk) => (data) => isOk(data) ? makeOk(onOk(data.value)) : makeErr(onErr(data.error))
|
|
795
|
+
};
|
|
186
796
|
|
|
187
797
|
// src/internal/InternalTypes.ts
|
|
188
798
|
var isNonEmptyArr = (list) => list.length > 0;
|
|
189
799
|
|
|
190
800
|
// src/Core/Validation.ts
|
|
191
|
-
var
|
|
192
|
-
(
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
801
|
+
var makePassed = (value) => ({ kind: "Passed", value });
|
|
802
|
+
var makeFailed = (error) => ({ kind: "Failed", errors: [error] });
|
|
803
|
+
var makeFailedAll = (errors) => ({ kind: "Failed", errors });
|
|
804
|
+
var isPassed = (data) => data.kind === "Passed";
|
|
805
|
+
var isFailed = (data) => data.kind === "Failed";
|
|
806
|
+
function toResult(arg) {
|
|
807
|
+
if (typeof arg === "function") {
|
|
808
|
+
const combine = arg;
|
|
809
|
+
return (val) => isPassed(val) ? Result.make.ok(val.value) : Result.make.err(combine(val.errors));
|
|
810
|
+
}
|
|
811
|
+
return isPassed(arg) ? Result.make.ok(arg.value) : Result.make.err(arg.errors);
|
|
812
|
+
}
|
|
813
|
+
var Validation = {
|
|
814
|
+
make: {
|
|
815
|
+
/**
|
|
816
|
+
* Wraps a value in a passed Validation.
|
|
817
|
+
*
|
|
818
|
+
* @example
|
|
819
|
+
* ```ts
|
|
820
|
+
* Validation.make.passed(42); // Passed(42)
|
|
821
|
+
* ```
|
|
822
|
+
*/
|
|
823
|
+
passed: makePassed,
|
|
824
|
+
/**
|
|
825
|
+
* Creates a failed Validation from a single error.
|
|
826
|
+
*
|
|
827
|
+
* @example
|
|
828
|
+
* ```ts
|
|
829
|
+
* Validation.make.failed("Invalid input");
|
|
830
|
+
* ```
|
|
831
|
+
*/
|
|
832
|
+
failed: makeFailed,
|
|
833
|
+
/**
|
|
834
|
+
* Creates a failed Validation from multiple errors.
|
|
835
|
+
*
|
|
836
|
+
* @example
|
|
837
|
+
* ```ts
|
|
838
|
+
* Validation.make.failedAll(["Invalid input"]);
|
|
839
|
+
* ```
|
|
840
|
+
*/
|
|
841
|
+
failedAll: makeFailedAll
|
|
842
|
+
},
|
|
843
|
+
is: {
|
|
844
|
+
/**
|
|
845
|
+
* Type guard that checks if a Validation is passed.
|
|
846
|
+
*
|
|
847
|
+
* @example
|
|
848
|
+
* ```ts
|
|
849
|
+
* const v = Validation.make.passed(42);
|
|
850
|
+
* if (Validation.is.passed(v)) {
|
|
851
|
+
* console.log(v.value); // 42
|
|
852
|
+
* }
|
|
853
|
+
* ```
|
|
854
|
+
*/
|
|
855
|
+
passed: isPassed,
|
|
856
|
+
/**
|
|
857
|
+
* Type guard that checks if a Validation is failed.
|
|
858
|
+
*
|
|
859
|
+
* @example
|
|
860
|
+
* ```ts
|
|
861
|
+
* const v = Validation.make.failed("invalid");
|
|
862
|
+
* if (Validation.is.failed(v)) {
|
|
863
|
+
* console.log(v.errors); // ["invalid"]
|
|
864
|
+
* }
|
|
865
|
+
* ```
|
|
866
|
+
*/
|
|
867
|
+
failed: isFailed
|
|
868
|
+
},
|
|
869
|
+
/**
|
|
870
|
+
* Creates a Validation from a synchronous thunk that may throw.
|
|
871
|
+
* Catches any errors and transforms them using the `onError` function into a Failed validation.
|
|
872
|
+
*
|
|
873
|
+
* @example
|
|
874
|
+
* ```ts
|
|
875
|
+
* const result = Validation.tryCatch(
|
|
876
|
+
* () => JSON.parse(rawString),
|
|
877
|
+
* { onError: (e) => `Parse error: ${e}` }
|
|
878
|
+
* );
|
|
879
|
+
* ```
|
|
880
|
+
*/
|
|
881
|
+
tryCatch: (f, options) => {
|
|
205
882
|
try {
|
|
206
|
-
return
|
|
883
|
+
return makePassed(f());
|
|
207
884
|
} catch (error) {
|
|
208
|
-
return
|
|
885
|
+
return makeFailed(options.onError(error));
|
|
209
886
|
}
|
|
210
|
-
}
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
887
|
+
},
|
|
888
|
+
// --- from ---
|
|
889
|
+
from: {
|
|
890
|
+
/**
|
|
891
|
+
* Creates a Validation from a predicate applied to a value.
|
|
892
|
+
* Returns Passed if the predicate passes, Failed from `onFalse` otherwise.
|
|
893
|
+
*
|
|
894
|
+
* @example
|
|
895
|
+
* ```ts
|
|
896
|
+
* const validateName = Validation.from.Predicate(
|
|
897
|
+
* (s: string) => s.length > 0,
|
|
898
|
+
* () => "Name is required"
|
|
899
|
+
* );
|
|
900
|
+
*
|
|
901
|
+
* validateName("Alice"); // Passed("Alice")
|
|
902
|
+
* validateName(""); // Failed(["Name is required"])
|
|
903
|
+
* ```
|
|
904
|
+
*/
|
|
905
|
+
Predicate: (pred, onFalse) => (a) => pred(a) ? makePassed(a) : makeFailed(onFalse(a)),
|
|
906
|
+
/**
|
|
907
|
+
* Creates a Validation from a nullable value.
|
|
908
|
+
* If the value is null or undefined, returns Failed with the error from onNull.
|
|
909
|
+
* Otherwise, returns Passed.
|
|
910
|
+
*
|
|
911
|
+
* @example
|
|
912
|
+
* ```ts
|
|
913
|
+
* pipe(null, Validation.from.nullable(() => "is null")); // Failed(["is null"])
|
|
914
|
+
* pipe(42, Validation.from.nullable(() => "is null")); // Passed(42)
|
|
915
|
+
* ```
|
|
916
|
+
*/
|
|
917
|
+
nullable: (onNull) => (value) => value === null || value === void 0 ? makeFailed(onNull()) : makePassed(value),
|
|
918
|
+
/**
|
|
919
|
+
* Creates a Validation from a Maybe.
|
|
920
|
+
* If the Maybe is None, returns Failed with the error from onNone.
|
|
921
|
+
* Otherwise, returns Passed.
|
|
922
|
+
*
|
|
923
|
+
* @example
|
|
924
|
+
* ```ts
|
|
925
|
+
* pipe(Maybe.make.none(), Validation.from.Maybe(() => "is none")); // Failed(["is none"])
|
|
926
|
+
* pipe(Maybe.make.some(42), Validation.from.Maybe(() => "is none")); // Passed(42)
|
|
927
|
+
* ```
|
|
928
|
+
*/
|
|
929
|
+
Maybe: (onNone) => (maybe) => Maybe.is.none(maybe) ? makeFailed(onNone()) : makePassed(maybe.value),
|
|
930
|
+
/**
|
|
931
|
+
* Converts a `Result` to a `Validation`. `Ok` becomes `Passed`; `Err(e)` becomes `Failed([e])`.
|
|
932
|
+
*
|
|
933
|
+
* Useful when bridging from error-short-circuiting `Result` pipelines into
|
|
934
|
+
* error-accumulating `Validation` pipelines.
|
|
935
|
+
*
|
|
936
|
+
* @example
|
|
937
|
+
* ```ts
|
|
938
|
+
* Validation.from.Result(Result.make.ok(42)); // Passed(42)
|
|
939
|
+
* Validation.from.Result(Result.make.err("bad")); // Failed(["bad"])
|
|
940
|
+
* ```
|
|
941
|
+
*/
|
|
942
|
+
Result: (data) => data.kind === "Ok" ? makePassed(data.value) : makeFailed(data.error)
|
|
943
|
+
},
|
|
944
|
+
/**
|
|
945
|
+
* Transforms the success value inside a Validation.
|
|
946
|
+
*
|
|
947
|
+
* @example
|
|
948
|
+
* ```ts
|
|
949
|
+
* pipe(Validation.make.passed(5), Validation.map(n => n * 2)); // Passed(10)
|
|
950
|
+
* pipe(Validation.make.failed("oops"), Validation.map(n => n * 2)); // Failed(["oops"])
|
|
951
|
+
* ```
|
|
952
|
+
*/
|
|
953
|
+
map: (f) => (data) => isPassed(data) ? makePassed(f(data.value)) : data,
|
|
954
|
+
/**
|
|
955
|
+
* Transforms the error list inside a Validation.
|
|
956
|
+
*
|
|
957
|
+
* @example
|
|
958
|
+
* ```ts
|
|
959
|
+
* pipe(Validation.make.failed("oops"), Validation.mapError(e => e.toUpperCase())); // Failed(["OOPS"])
|
|
960
|
+
* ```
|
|
961
|
+
*/
|
|
962
|
+
mapError: (f) => (data) => isFailed(data) ? makeFailedAll(data.errors.map(f)) : data,
|
|
963
|
+
/**
|
|
964
|
+
* Applies a function wrapped in a Validation to a value wrapped in a Validation.
|
|
965
|
+
* Accumulates errors from both sides.
|
|
966
|
+
*
|
|
967
|
+
* @example
|
|
968
|
+
* ```ts
|
|
969
|
+
* const add = (a: number) => (b: number) => a + b;
|
|
970
|
+
* pipe(
|
|
971
|
+
* Validation.make.passed(add),
|
|
972
|
+
* Validation.ap(Validation.make.passed(5)),
|
|
973
|
+
* Validation.ap(Validation.make.passed(3))
|
|
974
|
+
* ); // Passed(8)
|
|
975
|
+
*
|
|
976
|
+
* pipe(
|
|
977
|
+
* Validation.make.passed(add),
|
|
978
|
+
* Validation.ap(Validation.make.failed<string>("bad a")),
|
|
979
|
+
* Validation.ap(Validation.make.failed<string>("bad b"))
|
|
980
|
+
* ); // Failed(["bad a", "bad b"])
|
|
981
|
+
* ```
|
|
982
|
+
*/
|
|
983
|
+
ap: (arg) => (data) => {
|
|
984
|
+
if (isPassed(data)) {
|
|
985
|
+
return isPassed(arg) ? makePassed(data.value(arg.value)) : makeFailedAll(arg.errors);
|
|
986
|
+
}
|
|
987
|
+
return isPassed(arg) ? makeFailedAll(data.errors) : makeFailedAll([...data.errors, ...arg.errors]);
|
|
988
|
+
},
|
|
989
|
+
/**
|
|
990
|
+
* Applies a function wrapped in a Validation to a value wrapped in a Validation,
|
|
991
|
+
* using a custom error concatenator function when both sides fail.
|
|
992
|
+
*
|
|
993
|
+
* @example
|
|
994
|
+
* ```ts
|
|
995
|
+
* const concat = (e1: NonEmptyArr<string>, e2: NonEmptyArr<string>): NonEmptyArr<string> =>
|
|
996
|
+
* [...e1, ...e2];
|
|
997
|
+
* pipe(fnVal, Validation.apCustom(concat)(argVal));
|
|
998
|
+
* ```
|
|
999
|
+
*/
|
|
1000
|
+
apCustom: (concat2) => (arg) => (data) => {
|
|
1001
|
+
if (isPassed(data)) {
|
|
1002
|
+
return isPassed(arg) ? makePassed(data.value(arg.value)) : makeFailedAll(arg.errors);
|
|
1003
|
+
}
|
|
1004
|
+
return isPassed(arg) ? makeFailedAll(data.errors) : makeFailedAll(concat2(data.errors, arg.errors));
|
|
1005
|
+
},
|
|
1006
|
+
/**
|
|
1007
|
+
* Extracts the value from a Validation by providing handlers for both cases.
|
|
1008
|
+
*
|
|
1009
|
+
* @example
|
|
1010
|
+
* ```ts
|
|
1011
|
+
* pipe(
|
|
1012
|
+
* Validation.make.passed(42),
|
|
1013
|
+
* Validation.fold(
|
|
1014
|
+
* errors => `Errors: ${errors.join(", ")}`,
|
|
1015
|
+
* value => `Value: ${value}`
|
|
1016
|
+
* )
|
|
1017
|
+
* );
|
|
1018
|
+
* ```
|
|
1019
|
+
*/
|
|
1020
|
+
fold: (onFailed, onPassed) => (data) => isPassed(data) ? onPassed(data.value) : onFailed(data.errors),
|
|
1021
|
+
/**
|
|
1022
|
+
* Pattern matches on a Validation, returning the result of the matching case.
|
|
1023
|
+
*
|
|
1024
|
+
* @example
|
|
1025
|
+
* ```ts
|
|
1026
|
+
* pipe(
|
|
1027
|
+
* validation,
|
|
1028
|
+
* Validation.match({
|
|
1029
|
+
* passed: value => `Got ${value}`,
|
|
1030
|
+
* failed: errors => `Failed: ${errors.join(", ")}`
|
|
1031
|
+
* })
|
|
1032
|
+
* );
|
|
1033
|
+
* ```
|
|
1034
|
+
*/
|
|
1035
|
+
match: (cases) => (data) => isPassed(data) ? cases.passed(data.value) : cases.failed(data.errors),
|
|
1036
|
+
/**
|
|
1037
|
+
* Returns the success value or a default value if the Validation is failed.
|
|
1038
|
+
* The default can be a different type, widening the result to `A | B`.
|
|
1039
|
+
*
|
|
1040
|
+
* @example
|
|
1041
|
+
* ```ts
|
|
1042
|
+
* pipe(Validation.make.passed(5), Validation.getOrElse(() => 0)); // 5
|
|
1043
|
+
* pipe(Validation.make.failed("oops"), Validation.getOrElse(() => 0)); // 0
|
|
1044
|
+
* pipe(Validation.make.failed("oops"), Validation.getOrElse(() => null)); // null — typed as number | null
|
|
1045
|
+
* ```
|
|
1046
|
+
*/
|
|
1047
|
+
getOrElse: (defaultValue) => (data) => isPassed(data) ? data.value : defaultValue(),
|
|
1048
|
+
/**
|
|
1049
|
+
* Executes a side effect on the success value without changing the Validation.
|
|
1050
|
+
*
|
|
1051
|
+
* @example
|
|
1052
|
+
* ```ts
|
|
1053
|
+
* pipe(
|
|
1054
|
+
* Validation.make.passed(5),
|
|
1055
|
+
* Validation.tap(n => console.log("Value:", n)),
|
|
1056
|
+
* Validation.map(n => n * 2)
|
|
1057
|
+
* );
|
|
1058
|
+
* ```
|
|
1059
|
+
*/
|
|
1060
|
+
tap: (f) => (data) => {
|
|
1061
|
+
if (isPassed(data)) {
|
|
237
1062
|
f(data.value);
|
|
238
1063
|
}
|
|
239
1064
|
return data;
|
|
240
|
-
}
|
|
241
|
-
|
|
242
|
-
|
|
1065
|
+
},
|
|
1066
|
+
/**
|
|
1067
|
+
* Executes a side effect on the accumulated errors without changing the Validation.
|
|
1068
|
+
* Useful for logging or reporting validation failures.
|
|
1069
|
+
*
|
|
1070
|
+
* @example
|
|
1071
|
+
* ```ts
|
|
1072
|
+
* pipe(
|
|
1073
|
+
* Validation.make.failed("Name required"),
|
|
1074
|
+
* Validation.tapError(errors => console.error("validation failed:", errors)),
|
|
1075
|
+
* Validation.map(toUser)
|
|
1076
|
+
* );
|
|
1077
|
+
* ```
|
|
1078
|
+
*/
|
|
1079
|
+
tapError: (f) => (data) => {
|
|
1080
|
+
if (isFailed(data)) {
|
|
243
1081
|
f(data.errors);
|
|
244
1082
|
}
|
|
245
1083
|
return data;
|
|
246
|
-
}
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
1084
|
+
},
|
|
1085
|
+
/**
|
|
1086
|
+
* Recovers from a Failed state by providing a fallback Validation.
|
|
1087
|
+
* The fallback receives the accumulated error list so callers can inspect which errors occurred.
|
|
1088
|
+
* The fallback can produce a different success type, widening the result to `Validation<E, A | B>`.
|
|
1089
|
+
*/
|
|
1090
|
+
recover: (fallback) => (data) => isPassed(data) ? data : fallback(data.errors),
|
|
1091
|
+
/**
|
|
1092
|
+
* Recovers from a Failed state unless `isBlocked` returns true for any of the accumulated errors.
|
|
1093
|
+
* The fallback can produce a different success type, widening the result to `Validation<E, A | B>`.
|
|
1094
|
+
*
|
|
1095
|
+
* @example
|
|
1096
|
+
* ```ts
|
|
1097
|
+
* pipe(
|
|
1098
|
+
* Validation.make.failed("field-error"),
|
|
1099
|
+
* Validation.recoverUnless(e => e === "fatal", () => Validation.make.passed(0))
|
|
1100
|
+
* ); // Passed(0)
|
|
1101
|
+
* ```
|
|
1102
|
+
*/
|
|
1103
|
+
recoverUnless: (isBlocked, fallback) => (data) => isFailed(data) && !data.errors.some(isBlocked) ? fallback() : data,
|
|
1104
|
+
// --- to ---
|
|
1105
|
+
to: {
|
|
1106
|
+
/**
|
|
1107
|
+
* Converts a Validation to a Result.
|
|
1108
|
+
* Passed becomes Ok.
|
|
1109
|
+
* Direct call converts Failed to Err with accumulated error list `NonEmptyArr<E>`.
|
|
1110
|
+
* Curried call converts Failed to Err with combined error `E2` via `combineErrors`.
|
|
1111
|
+
*
|
|
1112
|
+
* @example
|
|
1113
|
+
* ```ts
|
|
1114
|
+
* Validation.to.Result(Validation.make.passed(42)); // Ok(42)
|
|
1115
|
+
* Validation.to.Result(Validation.make.failed("oops")); // Err(["oops"])
|
|
1116
|
+
* pipe(Validation.make.failed("oops"), Validation.to.Result(errors => errors.join(", "))); // Err("oops")
|
|
1117
|
+
* ```
|
|
1118
|
+
*/
|
|
1119
|
+
Result: toResult,
|
|
1120
|
+
/**
|
|
1121
|
+
* Converts a Validation to a Maybe. `Passed` becomes `Some`; `Failed` becomes `None`
|
|
1122
|
+
* (errors are discarded).
|
|
1123
|
+
*
|
|
1124
|
+
* @example
|
|
1125
|
+
* ```ts
|
|
1126
|
+
* Validation.to.Maybe(Validation.make.passed(42)); // Some(42)
|
|
1127
|
+
* Validation.to.Maybe(Validation.make.failed("bad")); // None
|
|
1128
|
+
* ```
|
|
1129
|
+
*/
|
|
1130
|
+
Maybe: (data) => isPassed(data) ? Maybe.make.some(data.value) : Maybe.make.none()
|
|
1131
|
+
},
|
|
1132
|
+
/**
|
|
1133
|
+
* Combines two independent Validation instances into a tuple.
|
|
1134
|
+
* If both are Passed, returns Passed with both values as a tuple.
|
|
1135
|
+
* If either is Failed, accumulates errors from both sides.
|
|
1136
|
+
*
|
|
1137
|
+
* @example
|
|
1138
|
+
* ```ts
|
|
1139
|
+
* Validation.product(
|
|
1140
|
+
* Validation.make.passed("alice"),
|
|
1141
|
+
* Validation.make.passed(30)
|
|
1142
|
+
* ); // Passed(["alice", 30])
|
|
1143
|
+
*
|
|
1144
|
+
* Validation.product(
|
|
1145
|
+
* Validation.make.failed("Name required"),
|
|
1146
|
+
* Validation.make.failed("Age must be >= 0")
|
|
1147
|
+
* ); // Failed(["Name required", "Age must be >= 0"])
|
|
1148
|
+
* ```
|
|
1149
|
+
*/
|
|
1150
|
+
product: (first, second) => {
|
|
1151
|
+
if (isPassed(first)) {
|
|
1152
|
+
return isPassed(second) ? makePassed([first.value, second.value]) : makeFailedAll(second.errors);
|
|
1153
|
+
}
|
|
1154
|
+
return isPassed(second) ? makeFailedAll(first.errors) : makeFailedAll([...first.errors, ...second.errors]);
|
|
1155
|
+
},
|
|
1156
|
+
/**
|
|
1157
|
+
* Combines a non-empty list of Validation instances, accumulating all errors.
|
|
1158
|
+
* If all are Passed, returns Passed with all values collected into an array.
|
|
1159
|
+
* If any are Failed, returns Failed with all accumulated errors.
|
|
1160
|
+
*
|
|
1161
|
+
* @example
|
|
1162
|
+
* ```ts
|
|
1163
|
+
* Validation.productAll([
|
|
1164
|
+
* validateName(name),
|
|
1165
|
+
* validateEmail(email),
|
|
1166
|
+
* validateAge(age)
|
|
1167
|
+
* ]);
|
|
1168
|
+
* // Passed([name, email, age]) or Failed([...all errors])
|
|
1169
|
+
* ```
|
|
1170
|
+
*/
|
|
1171
|
+
productAll: (data) => {
|
|
1172
|
+
const values3 = [];
|
|
269
1173
|
const errors = [];
|
|
270
1174
|
for (const v of data) {
|
|
271
|
-
if (
|
|
272
|
-
|
|
1175
|
+
if (isPassed(v)) {
|
|
1176
|
+
values3.push(v.value);
|
|
273
1177
|
} else {
|
|
274
1178
|
errors.push(...v.errors);
|
|
275
1179
|
}
|
|
276
1180
|
}
|
|
277
|
-
return isNonEmptyArr(errors) ?
|
|
278
|
-
}
|
|
279
|
-
|
|
1181
|
+
return isNonEmptyArr(errors) ? makeFailedAll(errors) : makePassed(values3);
|
|
1182
|
+
},
|
|
1183
|
+
/**
|
|
1184
|
+
* Combines a record of Validations into a single Validation of a record.
|
|
1185
|
+
* Accumulates all failed branches' errors.
|
|
1186
|
+
*
|
|
1187
|
+
* @example
|
|
1188
|
+
* ```ts
|
|
1189
|
+
* Validation.struct({
|
|
1190
|
+
* name: Validation.make.passed("Alice"),
|
|
1191
|
+
* age: Validation.make.passed(30)
|
|
1192
|
+
* }); // Passed({ name: "Alice", age: 30 })
|
|
1193
|
+
*
|
|
1194
|
+
* Validation.struct({
|
|
1195
|
+
* name: Validation.make.failed("Name required"),
|
|
1196
|
+
* age: Validation.make.failed("Age must be >= 0")
|
|
1197
|
+
* }); // Failed(["Name required", "Age must be >= 0"])
|
|
1198
|
+
* ```
|
|
1199
|
+
*/
|
|
1200
|
+
struct: (fields) => {
|
|
280
1201
|
const record = {};
|
|
281
1202
|
const errors = [];
|
|
282
1203
|
for (const key in fields) {
|
|
283
1204
|
if (Object.hasOwn(fields, key)) {
|
|
284
1205
|
const val = fields[key];
|
|
285
|
-
if (
|
|
1206
|
+
if (isPassed(val)) {
|
|
286
1207
|
record[key] = val.value;
|
|
287
1208
|
} else {
|
|
288
1209
|
errors.push(...val.errors);
|
|
289
1210
|
}
|
|
290
1211
|
}
|
|
291
1212
|
}
|
|
292
|
-
return isNonEmptyArr(errors) ?
|
|
293
|
-
}
|
|
294
|
-
}
|
|
1213
|
+
return isNonEmptyArr(errors) ? makeFailedAll(errors) : makePassed(record);
|
|
1214
|
+
}
|
|
1215
|
+
};
|
|
295
1216
|
|
|
296
1217
|
// src/Data/Arr.ts
|
|
297
1218
|
var ArrMaybe;
|
|
@@ -347,448 +1268,499 @@ var ArrTask;
|
|
|
347
1268
|
ArrTask2.sequence = (data) => (0, ArrTask2.traverse)((a) => a)(data);
|
|
348
1269
|
ArrTask2.Result = ArrTaskResult;
|
|
349
1270
|
})(ArrTask || (ArrTask = {}));
|
|
350
|
-
var
|
|
351
|
-
(
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
ArrNonEmpty2.mapWithIndex = (f) => (data) => Arr.mapWithIndex(f)(data);
|
|
363
|
-
ArrNonEmpty2.intersperse = (sep) => (data) => Arr.intersperse(sep)(data);
|
|
364
|
-
ArrNonEmpty2.concat = (other) => (data) => Arr.concat(other)(data);
|
|
365
|
-
ArrNonEmpty2.reverse = (data) => Arr.reverse(data);
|
|
366
|
-
})(ArrNonEmpty || (ArrNonEmpty = {}));
|
|
367
|
-
var Arr;
|
|
368
|
-
((Arr2) => {
|
|
369
|
-
Arr2.head = (data) => data.length > 0 ? Maybe.make.some(data[0]) : Maybe.make.none();
|
|
370
|
-
Arr2.last = (data) => data.length > 0 ? Maybe.make.some(data[data.length - 1]) : Maybe.make.none();
|
|
371
|
-
Arr2.tail = (data) => data.length > 0 ? Maybe.make.some(data.slice(1)) : Maybe.make.none();
|
|
372
|
-
Arr2.init = (data) => data.length > 0 ? Maybe.make.some(data.slice(0, -1)) : Maybe.make.none();
|
|
373
|
-
Arr2.findFirst = (predicate) => (data) => {
|
|
374
|
-
const idx = data.findIndex(predicate);
|
|
375
|
-
return idx !== -1 ? Maybe.make.some(data[idx]) : Maybe.make.none();
|
|
376
|
-
};
|
|
377
|
-
Arr2.findLast = (predicate) => (data) => {
|
|
378
|
-
for (let i = data.length - 1; i >= 0; i--) {
|
|
379
|
-
if (predicate(data[i])) {
|
|
380
|
-
return Maybe.make.some(data[i]);
|
|
381
|
-
}
|
|
1271
|
+
var head = (data) => data.length > 0 ? Maybe.make.some(data[0]) : Maybe.make.none();
|
|
1272
|
+
var last = (data) => data.length > 0 ? Maybe.make.some(data[data.length - 1]) : Maybe.make.none();
|
|
1273
|
+
var tail = (data) => data.length > 0 ? Maybe.make.some(data.slice(1)) : Maybe.make.none();
|
|
1274
|
+
var init = (data) => data.length > 0 ? Maybe.make.some(data.slice(0, -1)) : Maybe.make.none();
|
|
1275
|
+
var findFirst = (predicate) => (data) => {
|
|
1276
|
+
const idx = data.findIndex(predicate);
|
|
1277
|
+
return idx !== -1 ? Maybe.make.some(data[idx]) : Maybe.make.none();
|
|
1278
|
+
};
|
|
1279
|
+
var findLast = (predicate) => (data) => {
|
|
1280
|
+
for (let i = data.length - 1; i >= 0; i--) {
|
|
1281
|
+
if (predicate(data[i])) {
|
|
1282
|
+
return Maybe.make.some(data[i]);
|
|
382
1283
|
}
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
1284
|
+
}
|
|
1285
|
+
return Maybe.make.none();
|
|
1286
|
+
};
|
|
1287
|
+
var findIndex = (predicate) => (data) => {
|
|
1288
|
+
const idx = data.findIndex(predicate);
|
|
1289
|
+
return idx !== -1 ? Maybe.make.some(idx) : Maybe.make.none();
|
|
1290
|
+
};
|
|
1291
|
+
var map = (f) => (data) => {
|
|
1292
|
+
const n = data.length;
|
|
1293
|
+
const result = new Array(n);
|
|
1294
|
+
for (let i = 0; i < n; i++) {
|
|
1295
|
+
result[i] = f(data[i]);
|
|
1296
|
+
}
|
|
1297
|
+
return result;
|
|
1298
|
+
};
|
|
1299
|
+
var mapWithIndex = (f) => (data) => {
|
|
1300
|
+
const n = data.length;
|
|
1301
|
+
const result = new Array(n);
|
|
1302
|
+
for (let i = 0; i < n; i++) {
|
|
1303
|
+
result[i] = f(i, data[i]);
|
|
1304
|
+
}
|
|
1305
|
+
return result;
|
|
1306
|
+
};
|
|
1307
|
+
var filter = (predicate) => (data) => {
|
|
1308
|
+
const n = data.length;
|
|
1309
|
+
const result = [];
|
|
1310
|
+
for (let i = 0; i < n; i++) {
|
|
1311
|
+
if (predicate(data[i])) {
|
|
1312
|
+
result.push(data[i]);
|
|
394
1313
|
}
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
1314
|
+
}
|
|
1315
|
+
return result;
|
|
1316
|
+
};
|
|
1317
|
+
var filterMap = (f) => (data) => {
|
|
1318
|
+
const result = [];
|
|
1319
|
+
for (let i = 0; i < data.length; i++) {
|
|
1320
|
+
const mapped = f(data[i]);
|
|
1321
|
+
if (mapped.kind === "Some") {
|
|
1322
|
+
result.push(mapped.value);
|
|
402
1323
|
}
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
if (mapped.kind === "Some") {
|
|
420
|
-
result.push(mapped.value);
|
|
421
|
-
}
|
|
422
|
-
}
|
|
423
|
-
return result;
|
|
424
|
-
};
|
|
425
|
-
Arr2.partition = (predicate) => (data) => {
|
|
426
|
-
const pass = [];
|
|
427
|
-
const fail = [];
|
|
428
|
-
for (const a of data) {
|
|
429
|
-
(predicate(a) ? pass : fail).push(a);
|
|
430
|
-
}
|
|
431
|
-
return [pass, fail];
|
|
432
|
-
};
|
|
433
|
-
Arr2.compact = (data) => {
|
|
434
|
-
const result = [];
|
|
435
|
-
for (const item of data) {
|
|
436
|
-
if (item.kind === "Some") {
|
|
437
|
-
result.push(item.value);
|
|
438
|
-
}
|
|
439
|
-
}
|
|
440
|
-
return result;
|
|
441
|
-
};
|
|
442
|
-
Arr2.separate = (data) => {
|
|
443
|
-
const errors = [];
|
|
444
|
-
const successes = [];
|
|
445
|
-
for (const item of data) {
|
|
446
|
-
if (item.kind === "Ok") {
|
|
447
|
-
successes.push(item.value);
|
|
448
|
-
} else {
|
|
449
|
-
errors.push(item.error);
|
|
450
|
-
}
|
|
451
|
-
}
|
|
452
|
-
return [errors, successes];
|
|
453
|
-
};
|
|
454
|
-
Arr2.partitionMap = (f) => (data) => {
|
|
455
|
-
const errors = [];
|
|
456
|
-
const successes = [];
|
|
457
|
-
for (const item of data) {
|
|
458
|
-
const mapped = f(item);
|
|
459
|
-
if (mapped.kind === "Ok") {
|
|
460
|
-
successes.push(mapped.value);
|
|
461
|
-
} else {
|
|
462
|
-
errors.push(mapped.error);
|
|
463
|
-
}
|
|
464
|
-
}
|
|
465
|
-
return [errors, successes];
|
|
466
|
-
};
|
|
467
|
-
Arr2.groupBy = (f) => (data) => {
|
|
468
|
-
const result = {};
|
|
469
|
-
for (const a of data) {
|
|
470
|
-
const key = f(a);
|
|
471
|
-
if (!result[key]) {
|
|
472
|
-
result[key] = [];
|
|
473
|
-
}
|
|
474
|
-
result[key].push(a);
|
|
475
|
-
}
|
|
476
|
-
return result;
|
|
477
|
-
};
|
|
478
|
-
Arr2.uniq = (data) => data.length <= 1 ? data : [...new Set(data)];
|
|
479
|
-
Arr2.uniqBy = (f) => (data) => {
|
|
480
|
-
const seen = /* @__PURE__ */ new Set();
|
|
481
|
-
const result = [];
|
|
482
|
-
for (const a of data) {
|
|
483
|
-
const key = f(a);
|
|
484
|
-
if (!seen.has(key)) {
|
|
485
|
-
seen.add(key);
|
|
486
|
-
result.push(a);
|
|
487
|
-
}
|
|
488
|
-
}
|
|
489
|
-
return result;
|
|
490
|
-
};
|
|
491
|
-
Arr2.uniqWith = (eq) => (data) => {
|
|
492
|
-
const result = [];
|
|
493
|
-
for (const a of data) {
|
|
494
|
-
if (!result.some((x) => eq(x, a))) {
|
|
495
|
-
result.push(a);
|
|
496
|
-
}
|
|
497
|
-
}
|
|
498
|
-
return result;
|
|
499
|
-
};
|
|
500
|
-
Arr2.sortBy = (compare) => (data) => {
|
|
501
|
-
const arr = data;
|
|
502
|
-
if (typeof arr.toSorted === "function") {
|
|
503
|
-
return arr.toSorted(compare);
|
|
504
|
-
}
|
|
505
|
-
return [...data].sort(compare);
|
|
506
|
-
};
|
|
507
|
-
Arr2.sortWith = (ord) => (data) => {
|
|
508
|
-
const arr = data;
|
|
509
|
-
if (typeof arr.toSorted === "function") {
|
|
510
|
-
return arr.toSorted(ord);
|
|
511
|
-
}
|
|
512
|
-
return [...data].sort(ord);
|
|
513
|
-
};
|
|
514
|
-
Arr2.zip = (other) => (data) => {
|
|
515
|
-
const len = Math.min(data.length, other.length);
|
|
516
|
-
const result = new Array(len);
|
|
517
|
-
for (let i = 0; i < len; i++) {
|
|
518
|
-
result[i] = [data[i], other[i]];
|
|
519
|
-
}
|
|
520
|
-
return result;
|
|
521
|
-
};
|
|
522
|
-
Arr2.zipWith = (f) => (other) => (data) => {
|
|
523
|
-
const len = Math.min(data.length, other.length);
|
|
524
|
-
const result = new Array(len);
|
|
525
|
-
for (let i = 0; i < len; i++) {
|
|
526
|
-
result[i] = f(data[i], other[i]);
|
|
527
|
-
}
|
|
528
|
-
return result;
|
|
529
|
-
};
|
|
530
|
-
Arr2.intersperse = (sep) => (data) => {
|
|
531
|
-
if (data.length <= 1) {
|
|
532
|
-
return data;
|
|
533
|
-
}
|
|
534
|
-
const result = [data[0]];
|
|
535
|
-
for (let i = 1; i < data.length; i++) {
|
|
536
|
-
result.push(sep, data[i]);
|
|
537
|
-
}
|
|
538
|
-
return result;
|
|
539
|
-
};
|
|
540
|
-
Arr2.concat = (other) => (data) => [...data, ...other];
|
|
541
|
-
Arr2.chunksOf = (n) => (data) => {
|
|
542
|
-
if (n <= 0) {
|
|
543
|
-
return [];
|
|
544
|
-
}
|
|
545
|
-
const result = [];
|
|
546
|
-
for (let i = 0; i < data.length; i += n) {
|
|
547
|
-
result.push(data.slice(i, i + n));
|
|
548
|
-
}
|
|
549
|
-
return result;
|
|
550
|
-
};
|
|
551
|
-
Arr2.flatten = (data) => {
|
|
552
|
-
let totalLen = 0;
|
|
553
|
-
const outerLen = data.length;
|
|
554
|
-
for (let i = 0; i < outerLen; i++) {
|
|
555
|
-
totalLen += data[i].length;
|
|
556
|
-
}
|
|
557
|
-
const result = new Array(totalLen);
|
|
558
|
-
let idx = 0;
|
|
559
|
-
for (let i = 0; i < outerLen; i++) {
|
|
560
|
-
const chunk = data[i];
|
|
561
|
-
const innerLen = chunk.length;
|
|
562
|
-
for (let j = 0; j < innerLen; j++) {
|
|
563
|
-
result[idx++] = chunk[j];
|
|
564
|
-
}
|
|
565
|
-
}
|
|
566
|
-
return result;
|
|
567
|
-
};
|
|
568
|
-
Arr2.flatMap = (f) => (data) => {
|
|
569
|
-
const n = data.length;
|
|
570
|
-
const result = [];
|
|
571
|
-
for (let i = 0; i < n; i++) {
|
|
572
|
-
const chunk = f(data[i]);
|
|
573
|
-
const m = chunk.length;
|
|
574
|
-
for (let j = 0; j < m; j++) {
|
|
575
|
-
result.push(chunk[j]);
|
|
576
|
-
}
|
|
577
|
-
}
|
|
578
|
-
return result;
|
|
579
|
-
};
|
|
580
|
-
Arr2.reduce = (initial, f) => (data) => data.reduce(f, initial);
|
|
581
|
-
const _traverseTask = Object.assign((f) => ArrTask.traverse(f), {
|
|
582
|
-
Result: ArrTaskResult.traverse
|
|
583
|
-
});
|
|
584
|
-
const _sequenceTask = Object.assign((data) => ArrTask.sequence(data), {
|
|
585
|
-
Result: ArrTaskResult.sequence
|
|
586
|
-
});
|
|
587
|
-
let traverse;
|
|
588
|
-
((traverse2) => {
|
|
589
|
-
traverse2.Maybe = ArrMaybe.traverse;
|
|
590
|
-
traverse2.Result = ArrResult.traverse;
|
|
591
|
-
traverse2.Task = _traverseTask;
|
|
592
|
-
})(traverse = Arr2.traverse || (Arr2.traverse = {}));
|
|
593
|
-
let sequence;
|
|
594
|
-
((sequence2) => {
|
|
595
|
-
sequence2.Maybe = ArrMaybe.sequence;
|
|
596
|
-
sequence2.Result = ArrResult.sequence;
|
|
597
|
-
sequence2.Task = _sequenceTask;
|
|
598
|
-
})(sequence = Arr2.sequence || (Arr2.sequence = {}));
|
|
599
|
-
let is;
|
|
600
|
-
((is2) => {
|
|
601
|
-
is2.empty = (data) => data.length === 0;
|
|
602
|
-
is2.nonEmpty = (data) => isNonEmptyArr(data);
|
|
603
|
-
})(is = Arr2.is || (Arr2.is = {}));
|
|
604
|
-
Arr2.prepend = (value) => (data) => [value, ...data];
|
|
605
|
-
Arr2.append = (value) => (data) => [...data, value];
|
|
606
|
-
Arr2.size = (data) => data.length;
|
|
607
|
-
Arr2.some = (predicate) => (data) => {
|
|
608
|
-
const n = data.length;
|
|
609
|
-
for (let i = 0; i < n; i++) {
|
|
610
|
-
if (predicate(data[i])) {
|
|
611
|
-
return true;
|
|
612
|
-
}
|
|
1324
|
+
}
|
|
1325
|
+
return result;
|
|
1326
|
+
};
|
|
1327
|
+
var partition = (predicate) => (data) => {
|
|
1328
|
+
const pass = [];
|
|
1329
|
+
const fail = [];
|
|
1330
|
+
for (const a of data) {
|
|
1331
|
+
(predicate(a) ? pass : fail).push(a);
|
|
1332
|
+
}
|
|
1333
|
+
return [pass, fail];
|
|
1334
|
+
};
|
|
1335
|
+
var compact = (data) => {
|
|
1336
|
+
const result = [];
|
|
1337
|
+
for (const item of data) {
|
|
1338
|
+
if (item.kind === "Some") {
|
|
1339
|
+
result.push(item.value);
|
|
613
1340
|
}
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
1341
|
+
}
|
|
1342
|
+
return result;
|
|
1343
|
+
};
|
|
1344
|
+
var separate = (data) => {
|
|
1345
|
+
const errors = [];
|
|
1346
|
+
const successes = [];
|
|
1347
|
+
for (const item of data) {
|
|
1348
|
+
if (item.kind === "Ok") {
|
|
1349
|
+
successes.push(item.value);
|
|
1350
|
+
} else {
|
|
1351
|
+
errors.push(item.error);
|
|
622
1352
|
}
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
};
|
|
636
|
-
Arr2.removeAt = (index) => (data) => {
|
|
637
|
-
if (index < 0 || index >= data.length) {
|
|
638
|
-
return data;
|
|
1353
|
+
}
|
|
1354
|
+
return [errors, successes];
|
|
1355
|
+
};
|
|
1356
|
+
var partitionMap = (f) => (data) => {
|
|
1357
|
+
const errors = [];
|
|
1358
|
+
const successes = [];
|
|
1359
|
+
for (const item of data) {
|
|
1360
|
+
const mapped = f(item);
|
|
1361
|
+
if (mapped.kind === "Ok") {
|
|
1362
|
+
successes.push(mapped.value);
|
|
1363
|
+
} else {
|
|
1364
|
+
errors.push(mapped.error);
|
|
639
1365
|
}
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
1366
|
+
}
|
|
1367
|
+
return [errors, successes];
|
|
1368
|
+
};
|
|
1369
|
+
var groupBy = (f) => (data) => {
|
|
1370
|
+
const result = {};
|
|
1371
|
+
for (const a of data) {
|
|
1372
|
+
const key = f(a);
|
|
1373
|
+
if (!result[key]) {
|
|
1374
|
+
result[key] = [];
|
|
1375
|
+
}
|
|
1376
|
+
result[key].push(a);
|
|
1377
|
+
}
|
|
1378
|
+
return result;
|
|
1379
|
+
};
|
|
1380
|
+
var uniq = (data) => data.length <= 1 ? data : [...new Set(data)];
|
|
1381
|
+
var uniqBy = (f) => (data) => {
|
|
1382
|
+
const seen = /* @__PURE__ */ new Set();
|
|
1383
|
+
const result = [];
|
|
1384
|
+
for (const a of data) {
|
|
1385
|
+
const key = f(a);
|
|
1386
|
+
if (!seen.has(key)) {
|
|
1387
|
+
seen.add(key);
|
|
1388
|
+
result.push(a);
|
|
643
1389
|
}
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
const result = [];
|
|
652
|
-
for (const a of data) {
|
|
653
|
-
if (!predicate(a)) {
|
|
654
|
-
break;
|
|
655
|
-
}
|
|
1390
|
+
}
|
|
1391
|
+
return result;
|
|
1392
|
+
};
|
|
1393
|
+
var uniqWith = (eq) => (data) => {
|
|
1394
|
+
const result = [];
|
|
1395
|
+
for (const a of data) {
|
|
1396
|
+
if (!result.some((x) => eq(x, a))) {
|
|
656
1397
|
result.push(a);
|
|
657
1398
|
}
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
1399
|
+
}
|
|
1400
|
+
return result;
|
|
1401
|
+
};
|
|
1402
|
+
var sortBy = (compare) => (data) => {
|
|
1403
|
+
const arr = data;
|
|
1404
|
+
if (typeof arr.toSorted === "function") {
|
|
1405
|
+
return arr.toSorted(compare);
|
|
1406
|
+
}
|
|
1407
|
+
return [...data].sort(compare);
|
|
1408
|
+
};
|
|
1409
|
+
var sortWith = (ord) => (data) => {
|
|
1410
|
+
const arr = data;
|
|
1411
|
+
if (typeof arr.toSorted === "function") {
|
|
1412
|
+
return arr.toSorted(ord);
|
|
1413
|
+
}
|
|
1414
|
+
return [...data].sort(ord);
|
|
1415
|
+
};
|
|
1416
|
+
var zip = (other) => (data) => {
|
|
1417
|
+
const len = Math.min(data.length, other.length);
|
|
1418
|
+
const result = new Array(len);
|
|
1419
|
+
for (let i = 0; i < len; i++) {
|
|
1420
|
+
result[i] = [data[i], other[i]];
|
|
1421
|
+
}
|
|
1422
|
+
return result;
|
|
1423
|
+
};
|
|
1424
|
+
var zipWith = (f) => (other) => (data) => {
|
|
1425
|
+
const len = Math.min(data.length, other.length);
|
|
1426
|
+
const result = new Array(len);
|
|
1427
|
+
for (let i = 0; i < len; i++) {
|
|
1428
|
+
result[i] = f(data[i], other[i]);
|
|
1429
|
+
}
|
|
1430
|
+
return result;
|
|
1431
|
+
};
|
|
1432
|
+
var intersperse = (sep) => (data) => {
|
|
1433
|
+
if (data.length <= 1) {
|
|
1434
|
+
return data;
|
|
1435
|
+
}
|
|
1436
|
+
const result = [data[0]];
|
|
1437
|
+
for (let i = 1; i < data.length; i++) {
|
|
1438
|
+
result.push(sep, data[i]);
|
|
1439
|
+
}
|
|
1440
|
+
return result;
|
|
1441
|
+
};
|
|
1442
|
+
var concat = (other) => (data) => [...data, ...other];
|
|
1443
|
+
var chunksOf = (n) => (data) => {
|
|
1444
|
+
if (n <= 0) {
|
|
1445
|
+
return [];
|
|
1446
|
+
}
|
|
1447
|
+
const result = [];
|
|
1448
|
+
for (let i = 0; i < data.length; i += n) {
|
|
1449
|
+
result.push(data.slice(i, i + n));
|
|
1450
|
+
}
|
|
1451
|
+
return result;
|
|
1452
|
+
};
|
|
1453
|
+
var flatten = (data) => {
|
|
1454
|
+
let totalLen = 0;
|
|
1455
|
+
const outerLen = data.length;
|
|
1456
|
+
for (let i = 0; i < outerLen; i++) {
|
|
1457
|
+
totalLen += data[i].length;
|
|
1458
|
+
}
|
|
1459
|
+
const result = new Array(totalLen);
|
|
1460
|
+
let idx = 0;
|
|
1461
|
+
for (let i = 0; i < outerLen; i++) {
|
|
1462
|
+
const chunk = data[i];
|
|
1463
|
+
const innerLen = chunk.length;
|
|
1464
|
+
for (let j = 0; j < innerLen; j++) {
|
|
1465
|
+
result[idx++] = chunk[j];
|
|
664
1466
|
}
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
1467
|
+
}
|
|
1468
|
+
return result;
|
|
1469
|
+
};
|
|
1470
|
+
var flatMap = (f) => (data) => {
|
|
1471
|
+
const n = data.length;
|
|
1472
|
+
const result = [];
|
|
1473
|
+
for (let i = 0; i < n; i++) {
|
|
1474
|
+
const chunk = f(data[i]);
|
|
1475
|
+
const m = chunk.length;
|
|
1476
|
+
for (let j = 0; j < m; j++) {
|
|
1477
|
+
result.push(chunk[j]);
|
|
674
1478
|
}
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
1479
|
+
}
|
|
1480
|
+
return result;
|
|
1481
|
+
};
|
|
1482
|
+
var reduce = (initial, f) => (data) => data.reduce(f, initial);
|
|
1483
|
+
var _traverseTask = Object.assign((f) => ArrTask.traverse(f), {
|
|
1484
|
+
Result: ArrTaskResult.traverse
|
|
1485
|
+
});
|
|
1486
|
+
var _sequenceTask = Object.assign((data) => ArrTask.sequence(data), {
|
|
1487
|
+
Result: ArrTaskResult.sequence
|
|
1488
|
+
});
|
|
1489
|
+
var prepend = (value) => (data) => [value, ...data];
|
|
1490
|
+
var append = (value) => (data) => [...data, value];
|
|
1491
|
+
var size = (data) => data.length;
|
|
1492
|
+
var some = (predicate) => (data) => {
|
|
1493
|
+
const n = data.length;
|
|
1494
|
+
for (let i = 0; i < n; i++) {
|
|
1495
|
+
if (predicate(data[i])) {
|
|
1496
|
+
return true;
|
|
691
1497
|
}
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
1498
|
+
}
|
|
1499
|
+
return false;
|
|
1500
|
+
};
|
|
1501
|
+
var every = (predicate) => (data) => {
|
|
1502
|
+
const n = data.length;
|
|
1503
|
+
for (let i = 0; i < n; i++) {
|
|
1504
|
+
if (!predicate(data[i])) {
|
|
1505
|
+
return false;
|
|
698
1506
|
}
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
1507
|
+
}
|
|
1508
|
+
return true;
|
|
1509
|
+
};
|
|
1510
|
+
var reverse = (data) => [...data].toReversed();
|
|
1511
|
+
var insertAt = (index, item) => (data) => {
|
|
1512
|
+
const i = Math.max(0, Math.min(index, data.length));
|
|
1513
|
+
const arr = data;
|
|
1514
|
+
if (typeof arr.toSpliced === "function") {
|
|
1515
|
+
return arr.toSpliced(i, 0, item);
|
|
1516
|
+
}
|
|
1517
|
+
const result = [...data];
|
|
1518
|
+
result.splice(i, 0, item);
|
|
1519
|
+
return result;
|
|
1520
|
+
};
|
|
1521
|
+
var removeAt = (index) => (data) => {
|
|
1522
|
+
if (index < 0 || index >= data.length) {
|
|
1523
|
+
return data;
|
|
1524
|
+
}
|
|
1525
|
+
const arr = data;
|
|
1526
|
+
if (typeof arr.toSpliced === "function") {
|
|
1527
|
+
return arr.toSpliced(index, 1);
|
|
1528
|
+
}
|
|
1529
|
+
const result = [...data];
|
|
1530
|
+
result.splice(index, 1);
|
|
1531
|
+
return result;
|
|
1532
|
+
};
|
|
1533
|
+
var take = (n) => (data) => n <= 0 ? [] : data.slice(0, n);
|
|
1534
|
+
var drop = (n) => (data) => data.slice(n);
|
|
1535
|
+
var takeWhile = (predicate) => (data) => {
|
|
1536
|
+
const result = [];
|
|
1537
|
+
for (const a of data) {
|
|
1538
|
+
if (!predicate(a)) {
|
|
1539
|
+
break;
|
|
1540
|
+
}
|
|
1541
|
+
result.push(a);
|
|
1542
|
+
}
|
|
1543
|
+
return result;
|
|
1544
|
+
};
|
|
1545
|
+
var dropWhile = (predicate) => (data) => {
|
|
1546
|
+
let i = 0;
|
|
1547
|
+
while (i < data.length && predicate(data[i])) {
|
|
1548
|
+
i++;
|
|
1549
|
+
}
|
|
1550
|
+
return data.slice(i);
|
|
1551
|
+
};
|
|
1552
|
+
var scan = (initial, f) => (data) => {
|
|
1553
|
+
const n = data.length;
|
|
1554
|
+
const result = new Array(n);
|
|
1555
|
+
let acc = initial;
|
|
1556
|
+
for (let i = 0; i < n; i++) {
|
|
1557
|
+
acc = f(acc, data[i]);
|
|
1558
|
+
result[i] = acc;
|
|
1559
|
+
}
|
|
1560
|
+
return result;
|
|
1561
|
+
};
|
|
1562
|
+
var splitAt = (index) => (data) => {
|
|
1563
|
+
const i = Math.max(0, index);
|
|
1564
|
+
return [data.slice(0, i), data.slice(i)];
|
|
1565
|
+
};
|
|
1566
|
+
var partitionMaybe = (f) => (data) => {
|
|
1567
|
+
const failures = [];
|
|
1568
|
+
const successes = [];
|
|
1569
|
+
for (let i = 0; i < data.length; i++) {
|
|
1570
|
+
const res = f(data[i]);
|
|
1571
|
+
if (res.kind === "Some") {
|
|
1572
|
+
successes.push(res.value);
|
|
1573
|
+
} else {
|
|
1574
|
+
failures.push(data[i]);
|
|
707
1575
|
}
|
|
1576
|
+
}
|
|
1577
|
+
return [failures, successes];
|
|
1578
|
+
};
|
|
1579
|
+
var at = (index) => (data) => {
|
|
1580
|
+
const targetIndex = index < 0 ? data.length + index : index;
|
|
1581
|
+
if (targetIndex < 0 || targetIndex >= data.length) {
|
|
708
1582
|
return Maybe.make.none();
|
|
709
|
-
}
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
Arr2.frequencies = (data) => {
|
|
718
|
-
const map2 = new globalThis.Map();
|
|
719
|
-
for (let i = 0; i < data.length; i++) {
|
|
720
|
-
const item = data[i];
|
|
721
|
-
map2.set(item, (map2.get(item) ?? 0) + 1);
|
|
722
|
-
}
|
|
723
|
-
return map2;
|
|
724
|
-
};
|
|
725
|
-
Arr2.chunkBy = (keyFn) => (data) => {
|
|
726
|
-
if (data.length === 0) {
|
|
727
|
-
return [];
|
|
728
|
-
}
|
|
729
|
-
const result = [];
|
|
730
|
-
let currentChunk = [data[0]];
|
|
731
|
-
let currentKey = keyFn(data[0]);
|
|
732
|
-
for (let i = 1; i < data.length; i++) {
|
|
733
|
-
const item = data[i];
|
|
734
|
-
const key = keyFn(item);
|
|
735
|
-
if (Object.is(key, currentKey)) {
|
|
736
|
-
currentChunk.push(item);
|
|
737
|
-
} else {
|
|
738
|
-
result.push(currentChunk);
|
|
739
|
-
currentChunk = [item];
|
|
740
|
-
currentKey = key;
|
|
741
|
-
}
|
|
742
|
-
}
|
|
743
|
-
result.push(currentChunk);
|
|
744
|
-
return result;
|
|
745
|
-
};
|
|
746
|
-
Arr2.dedupeAdjacent = (eq = (a, b) => Object.is(a, b)) => (data) => {
|
|
747
|
-
if (data.length === 0) {
|
|
748
|
-
return [];
|
|
749
|
-
}
|
|
750
|
-
const result = [data[0]];
|
|
751
|
-
for (let i = 1; i < data.length; i++) {
|
|
752
|
-
if (!eq(data[i], result[result.length - 1])) {
|
|
753
|
-
result.push(data[i]);
|
|
754
|
-
}
|
|
755
|
-
}
|
|
756
|
-
return result;
|
|
757
|
-
};
|
|
758
|
-
Arr2.windowed = (size2, options) => (data) => {
|
|
759
|
-
const step = options?.step ?? 1;
|
|
760
|
-
if (size2 <= 0 || step <= 0 || data.length < size2) {
|
|
761
|
-
return [];
|
|
1583
|
+
}
|
|
1584
|
+
return Maybe.make.some(data[targetIndex]);
|
|
1585
|
+
};
|
|
1586
|
+
var findMap = (f) => (data) => {
|
|
1587
|
+
for (let i = 0; i < data.length; i++) {
|
|
1588
|
+
const res = f(data[i]);
|
|
1589
|
+
if (res.kind === "Some") {
|
|
1590
|
+
return res;
|
|
762
1591
|
}
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
1592
|
+
}
|
|
1593
|
+
return Maybe.make.none();
|
|
1594
|
+
};
|
|
1595
|
+
var indexBy = (keyFn) => (data) => {
|
|
1596
|
+
const resultMap = new globalThis.Map();
|
|
1597
|
+
for (let i = 0; i < data.length; i++) {
|
|
1598
|
+
resultMap.set(keyFn(data[i]), data[i]);
|
|
1599
|
+
}
|
|
1600
|
+
return resultMap;
|
|
1601
|
+
};
|
|
1602
|
+
var frequencies = (data) => {
|
|
1603
|
+
const resultMap = new globalThis.Map();
|
|
1604
|
+
for (let i = 0; i < data.length; i++) {
|
|
1605
|
+
const item = data[i];
|
|
1606
|
+
resultMap.set(item, (resultMap.get(item) ?? 0) + 1);
|
|
1607
|
+
}
|
|
1608
|
+
return resultMap;
|
|
1609
|
+
};
|
|
1610
|
+
var chunkBy = (keyFn) => (data) => {
|
|
1611
|
+
if (data.length === 0) {
|
|
1612
|
+
return [];
|
|
1613
|
+
}
|
|
1614
|
+
const result = [];
|
|
1615
|
+
let currentChunk = [data[0]];
|
|
1616
|
+
let currentKey = keyFn(data[0]);
|
|
1617
|
+
for (let i = 1; i < data.length; i++) {
|
|
1618
|
+
const item = data[i];
|
|
1619
|
+
const key = keyFn(item);
|
|
1620
|
+
if (Object.is(key, currentKey)) {
|
|
1621
|
+
currentChunk.push(item);
|
|
1622
|
+
} else {
|
|
1623
|
+
result.push(currentChunk);
|
|
1624
|
+
currentChunk = [item];
|
|
1625
|
+
currentKey = key;
|
|
766
1626
|
}
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
result.push(
|
|
779
|
-
currentState = nextState;
|
|
1627
|
+
}
|
|
1628
|
+
result.push(currentChunk);
|
|
1629
|
+
return result;
|
|
1630
|
+
};
|
|
1631
|
+
var dedupeAdjacent = (eq = (a, b) => Object.is(a, b)) => (data) => {
|
|
1632
|
+
if (data.length === 0) {
|
|
1633
|
+
return [];
|
|
1634
|
+
}
|
|
1635
|
+
const result = [data[0]];
|
|
1636
|
+
for (let i = 1; i < data.length; i++) {
|
|
1637
|
+
if (!eq(data[i], result[result.length - 1])) {
|
|
1638
|
+
result.push(data[i]);
|
|
780
1639
|
}
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
|
|
1640
|
+
}
|
|
1641
|
+
return result;
|
|
1642
|
+
};
|
|
1643
|
+
var windowed = (windowSize, options) => (data) => {
|
|
1644
|
+
const step = options?.step ?? 1;
|
|
1645
|
+
if (windowSize <= 0 || step <= 0 || data.length < windowSize) {
|
|
1646
|
+
return [];
|
|
1647
|
+
}
|
|
1648
|
+
const result = [];
|
|
1649
|
+
for (let i = 0; i <= data.length - windowSize; i += step) {
|
|
1650
|
+
result.push(data.slice(i, i + windowSize));
|
|
1651
|
+
}
|
|
1652
|
+
return result;
|
|
1653
|
+
};
|
|
1654
|
+
var unfold = (initial, f) => {
|
|
1655
|
+
const result = [];
|
|
1656
|
+
let currentState = initial;
|
|
1657
|
+
while (true) {
|
|
1658
|
+
const next = f(currentState);
|
|
1659
|
+
if (next.kind === "None") {
|
|
1660
|
+
break;
|
|
1661
|
+
}
|
|
1662
|
+
const [item, nextState] = next.value;
|
|
1663
|
+
result.push(item);
|
|
1664
|
+
currentState = nextState;
|
|
1665
|
+
}
|
|
1666
|
+
return result;
|
|
1667
|
+
};
|
|
1668
|
+
var ArrFrom = {
|
|
1669
|
+
Array: (data) => data.length > 0 ? Maybe.make.some(data) : Maybe.make.none()
|
|
1670
|
+
};
|
|
1671
|
+
var ArrIs = {
|
|
1672
|
+
empty: (data) => data.length === 0,
|
|
1673
|
+
nonEmpty: (data) => isNonEmptyArr(data)
|
|
1674
|
+
};
|
|
1675
|
+
var ArrNonEmpty = {
|
|
1676
|
+
singleton: (value) => [value],
|
|
1677
|
+
from: {
|
|
1678
|
+
Array: (data) => isNonEmptyArr(data) ? Maybe.make.some(data) : Maybe.make.none()
|
|
1679
|
+
},
|
|
1680
|
+
head: (data) => data[0],
|
|
1681
|
+
last: (data) => data[data.length - 1],
|
|
1682
|
+
tail: (data) => data.slice(1),
|
|
1683
|
+
reduce: (f) => (data) => data.reduce(f),
|
|
1684
|
+
map: (f) => (data) => map(f)(data),
|
|
1685
|
+
mapWithIndex: (f) => (data) => mapWithIndex(f)(data),
|
|
1686
|
+
intersperse: (sep) => (data) => intersperse(sep)(data),
|
|
1687
|
+
concat: (other) => (data) => concat(other)(data),
|
|
1688
|
+
reverse: (data) => reverse(data)
|
|
1689
|
+
};
|
|
1690
|
+
var Arr = {
|
|
1691
|
+
head,
|
|
1692
|
+
last,
|
|
1693
|
+
tail,
|
|
1694
|
+
init,
|
|
1695
|
+
findFirst,
|
|
1696
|
+
findLast,
|
|
1697
|
+
findIndex,
|
|
1698
|
+
map,
|
|
1699
|
+
mapWithIndex,
|
|
1700
|
+
filter,
|
|
1701
|
+
filterMap,
|
|
1702
|
+
partition,
|
|
1703
|
+
compact,
|
|
1704
|
+
separate,
|
|
1705
|
+
partitionMap,
|
|
1706
|
+
groupBy,
|
|
1707
|
+
uniq,
|
|
1708
|
+
uniqBy,
|
|
1709
|
+
uniqWith,
|
|
1710
|
+
sortBy,
|
|
1711
|
+
sortWith,
|
|
1712
|
+
zip,
|
|
1713
|
+
zipWith,
|
|
1714
|
+
intersperse,
|
|
1715
|
+
concat,
|
|
1716
|
+
chunksOf,
|
|
1717
|
+
flatten,
|
|
1718
|
+
flatMap,
|
|
1719
|
+
reduce,
|
|
1720
|
+
prepend,
|
|
1721
|
+
append,
|
|
1722
|
+
size,
|
|
1723
|
+
some,
|
|
1724
|
+
every,
|
|
1725
|
+
reverse,
|
|
1726
|
+
insertAt,
|
|
1727
|
+
removeAt,
|
|
1728
|
+
take,
|
|
1729
|
+
drop,
|
|
1730
|
+
takeWhile,
|
|
1731
|
+
dropWhile,
|
|
1732
|
+
scan,
|
|
1733
|
+
splitAt,
|
|
1734
|
+
partitionMaybe,
|
|
1735
|
+
at,
|
|
1736
|
+
findMap,
|
|
1737
|
+
indexBy,
|
|
1738
|
+
frequencies,
|
|
1739
|
+
chunkBy,
|
|
1740
|
+
dedupeAdjacent,
|
|
1741
|
+
windowed,
|
|
1742
|
+
unfold,
|
|
1743
|
+
from: ArrFrom,
|
|
1744
|
+
is: ArrIs,
|
|
1745
|
+
traverse: { Maybe: ArrMaybe.traverse, Result: ArrResult.traverse, Task: _traverseTask },
|
|
1746
|
+
sequence: { Maybe: ArrMaybe.sequence, Result: ArrResult.sequence, Task: _sequenceTask },
|
|
1747
|
+
NonEmpty: ArrNonEmpty
|
|
1748
|
+
};
|
|
785
1749
|
|
|
786
1750
|
// src/Data/BigNum.ts
|
|
787
|
-
var BigNum
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
|
|
1751
|
+
var BigNum = {
|
|
1752
|
+
// --- from ---
|
|
1753
|
+
from: {
|
|
1754
|
+
/**
|
|
1755
|
+
* Safely parses a string into a `bigint`. Returns `None` if parsing fails.
|
|
1756
|
+
*
|
|
1757
|
+
* @example
|
|
1758
|
+
* ```ts
|
|
1759
|
+
* BigNum.from.string("123"); // Some(123n)
|
|
1760
|
+
* BigNum.from.string("abc"); // None
|
|
1761
|
+
* ```
|
|
1762
|
+
*/
|
|
1763
|
+
string: (s) => {
|
|
792
1764
|
try {
|
|
793
1765
|
if (s.trim() === "") {
|
|
794
1766
|
return Maybe.make.none();
|
|
@@ -797,272 +1769,542 @@ var BigNum;
|
|
|
797
1769
|
} catch {
|
|
798
1770
|
return Maybe.make.none();
|
|
799
1771
|
}
|
|
800
|
-
}
|
|
801
|
-
|
|
1772
|
+
},
|
|
1773
|
+
/**
|
|
1774
|
+
* Safely converts a number into a `bigint`. Returns `None` for floats, `NaN`, or non-safe integers.
|
|
1775
|
+
*
|
|
1776
|
+
* @example
|
|
1777
|
+
* ```ts
|
|
1778
|
+
* BigNum.from.number(42); // Some(42n)
|
|
1779
|
+
* BigNum.from.number(3.14); // None
|
|
1780
|
+
* ```
|
|
1781
|
+
*/
|
|
1782
|
+
number: (n) => {
|
|
802
1783
|
if (!Number.isInteger(n) || n < Number.MIN_SAFE_INTEGER || n > Number.MAX_SAFE_INTEGER) {
|
|
803
1784
|
return Maybe.make.none();
|
|
804
1785
|
}
|
|
805
1786
|
return Maybe.make.some(BigInt(n));
|
|
806
|
-
}
|
|
807
|
-
}
|
|
808
|
-
|
|
809
|
-
|
|
810
|
-
|
|
1787
|
+
}
|
|
1788
|
+
},
|
|
1789
|
+
// --- to ---
|
|
1790
|
+
to: {
|
|
1791
|
+
/**
|
|
1792
|
+
* Safely converts a `bigint` to a `number`. Returns `None` if the value is outside JavaScript's safe integer range.
|
|
1793
|
+
*
|
|
1794
|
+
* @example
|
|
1795
|
+
* ```ts
|
|
1796
|
+
* BigNum.to.number(42n); // Some(42)
|
|
1797
|
+
* BigNum.to.number(9007199254740993n); // None
|
|
1798
|
+
* ```
|
|
1799
|
+
*/
|
|
1800
|
+
number: (b) => {
|
|
811
1801
|
if (b < BigInt(Number.MIN_SAFE_INTEGER) || b > BigInt(Number.MAX_SAFE_INTEGER)) {
|
|
812
1802
|
return Maybe.make.none();
|
|
813
1803
|
}
|
|
814
1804
|
return Maybe.make.some(Number(b));
|
|
815
|
-
}
|
|
816
|
-
}
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
|
|
1805
|
+
}
|
|
1806
|
+
},
|
|
1807
|
+
/**
|
|
1808
|
+
* Adds `b` to `a`. Data-last curried signature: `add(b)(a)` = `a + b`.
|
|
1809
|
+
*
|
|
1810
|
+
* @example
|
|
1811
|
+
* ```ts
|
|
1812
|
+
* pipe(10n, BigNum.add(5n)); // 15n
|
|
1813
|
+
* ```
|
|
1814
|
+
*/
|
|
1815
|
+
add: (b) => (a) => a + b,
|
|
1816
|
+
/**
|
|
1817
|
+
* Subtracts `b` from `a`. Data-last curried signature: `sub(b)(a)` = `a - b`.
|
|
1818
|
+
*
|
|
1819
|
+
* @example
|
|
1820
|
+
* ```ts
|
|
1821
|
+
* pipe(10n, BigNum.sub(3n)); // 7n
|
|
1822
|
+
* ```
|
|
1823
|
+
*/
|
|
1824
|
+
sub: (b) => (a) => a - b,
|
|
1825
|
+
/**
|
|
1826
|
+
* Multiplies `a` by `b`. Data-last curried signature: `mul(b)(a)` = `a * b`.
|
|
1827
|
+
*
|
|
1828
|
+
* @example
|
|
1829
|
+
* ```ts
|
|
1830
|
+
* pipe(6n, BigNum.mul(7n)); // 42n
|
|
1831
|
+
* ```
|
|
1832
|
+
*/
|
|
1833
|
+
mul: (b) => (a) => a * b,
|
|
1834
|
+
/**
|
|
1835
|
+
* Divides `a` by `b`. Returns `None` if `b` is `0n`.
|
|
1836
|
+
*
|
|
1837
|
+
* @example
|
|
1838
|
+
* ```ts
|
|
1839
|
+
* pipe(20n, BigNum.div(4n)); // Some(5n)
|
|
1840
|
+
* pipe(5n, BigNum.div(0n)); // None
|
|
1841
|
+
* ```
|
|
1842
|
+
*/
|
|
1843
|
+
div: (b) => (a) => b === 0n ? Maybe.make.none() : Maybe.make.some(a / b),
|
|
1844
|
+
/**
|
|
1845
|
+
* Computes remainder of `a / b`. Returns `None` if `b` is `0n`.
|
|
1846
|
+
*
|
|
1847
|
+
* @example
|
|
1848
|
+
* ```ts
|
|
1849
|
+
* pipe(10n, BigNum.mod(3n)); // Some(1n)
|
|
1850
|
+
* pipe(5n, BigNum.mod(0n)); // None
|
|
1851
|
+
* ```
|
|
1852
|
+
*/
|
|
1853
|
+
mod: (b) => (a) => b === 0n ? Maybe.make.none() : Maybe.make.some(a % b),
|
|
1854
|
+
/**
|
|
1855
|
+
* Clamps `a` between `min` and `max` (inclusive).
|
|
1856
|
+
*
|
|
1857
|
+
* @example
|
|
1858
|
+
* ```ts
|
|
1859
|
+
* pipe(150n, BigNum.clamp(0n, 100n)); // 100n
|
|
1860
|
+
* ```
|
|
1861
|
+
*/
|
|
1862
|
+
clamp: (min, max) => (a) => a < min ? min : a > max ? max : a,
|
|
1863
|
+
/**
|
|
1864
|
+
* Returns `true` if `a` is in the range `[start, end)` (inclusive start, exclusive end).
|
|
1865
|
+
*
|
|
1866
|
+
* @example
|
|
1867
|
+
* ```ts
|
|
1868
|
+
* pipe(5n, BigNum.inRange(1n, 10n)); // true
|
|
1869
|
+
* ```
|
|
1870
|
+
*/
|
|
1871
|
+
inRange: (start, end) => (a) => a >= start && a < end,
|
|
1872
|
+
/**
|
|
1873
|
+
* Returns absolute value of a `bigint`.
|
|
1874
|
+
*
|
|
1875
|
+
* @example
|
|
1876
|
+
* ```ts
|
|
1877
|
+
* BigNum.abs(-42n); // 42n
|
|
1878
|
+
* ```
|
|
1879
|
+
*/
|
|
1880
|
+
abs: (a) => a < 0n ? -a : a,
|
|
1881
|
+
/**
|
|
1882
|
+
* Returns the minimum of `a` and `b`.
|
|
1883
|
+
*
|
|
1884
|
+
* @example
|
|
1885
|
+
* ```ts
|
|
1886
|
+
* pipe(10n, BigNum.min(5n)); // 5n
|
|
1887
|
+
* ```
|
|
1888
|
+
*/
|
|
1889
|
+
min: (b) => (a) => a < b ? a : b,
|
|
1890
|
+
/**
|
|
1891
|
+
* Returns the maximum of `a` and `b`.
|
|
1892
|
+
*
|
|
1893
|
+
* @example
|
|
1894
|
+
* ```ts
|
|
1895
|
+
* pipe(10n, BigNum.max(5n)); // 10n
|
|
1896
|
+
* ```
|
|
1897
|
+
*/
|
|
1898
|
+
max: (b) => (a) => a > b ? a : b
|
|
1899
|
+
};
|
|
828
1900
|
|
|
829
1901
|
// src/Data/Dict.ts
|
|
830
|
-
var
|
|
831
|
-
(
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
(
|
|
846
|
-
|
|
847
|
-
((is2) => {
|
|
848
|
-
is2.empty = (m) => m.size === 0;
|
|
849
|
-
is2.nonEmpty = (m) => m.size > 0;
|
|
850
|
-
})(is = Dict2.is || (Dict2.is = {}));
|
|
851
|
-
Dict2.empty = () => new globalThis.Map();
|
|
852
|
-
Dict2.singleton = (key, value) => new globalThis.Map([[key, value]]);
|
|
853
|
-
let from;
|
|
854
|
-
((from2) => {
|
|
855
|
-
from2.entries = (entries3) => new globalThis.Map(entries3);
|
|
856
|
-
from2.Record = (rec) => new globalThis.Map(Object.entries(rec));
|
|
857
|
-
})(from = Dict2.from || (Dict2.from = {}));
|
|
858
|
-
Dict2.groupBy = (keyFn) => (items) => {
|
|
859
|
-
const result = new globalThis.Map();
|
|
860
|
-
for (const item of items) {
|
|
861
|
-
const key = keyFn(item);
|
|
862
|
-
const arr = result.get(key);
|
|
863
|
-
if (arr !== void 0) {
|
|
864
|
-
arr.push(item);
|
|
865
|
-
} else {
|
|
866
|
-
result.set(key, [item]);
|
|
867
|
-
}
|
|
868
|
-
}
|
|
869
|
-
return result;
|
|
870
|
-
};
|
|
871
|
-
Dict2.has = (key) => (m) => m.has(key);
|
|
872
|
-
Dict2.lookup = (key) => (m) => m.has(key) ? Maybe.make.some(m.get(key)) : Maybe.make.none();
|
|
873
|
-
Dict2.size = (m) => m.size;
|
|
874
|
-
Dict2.keys = (m) => [...m.keys()];
|
|
875
|
-
Dict2.values = (m) => [...m.values()];
|
|
876
|
-
Dict2.entries = (m) => [...m.entries()];
|
|
877
|
-
Dict2.insert = (key, value) => (m) => {
|
|
878
|
-
const result = new globalThis.Map(m);
|
|
879
|
-
result.set(key, value);
|
|
880
|
-
return result;
|
|
881
|
-
};
|
|
882
|
-
Dict2.remove = (key) => (m) => {
|
|
883
|
-
if (!m.has(key)) {
|
|
884
|
-
return m;
|
|
885
|
-
}
|
|
886
|
-
const result = new globalThis.Map(m);
|
|
887
|
-
result.delete(key);
|
|
888
|
-
return result;
|
|
889
|
-
};
|
|
890
|
-
Dict2.upsert = (key, f) => (m) => {
|
|
891
|
-
const result = new globalThis.Map(m);
|
|
892
|
-
result.set(key, f((0, Dict2.lookup)(key)(m)));
|
|
893
|
-
return result;
|
|
894
|
-
};
|
|
895
|
-
Dict2.map = (f) => (m) => {
|
|
896
|
-
const result = new globalThis.Map();
|
|
897
|
-
for (const [k, v] of m) {
|
|
898
|
-
result.set(k, f(v));
|
|
899
|
-
}
|
|
900
|
-
return result;
|
|
901
|
-
};
|
|
902
|
-
Dict2.mapWithKey = (f) => (m) => {
|
|
903
|
-
const result = new globalThis.Map();
|
|
904
|
-
for (const [k, v] of m) {
|
|
905
|
-
result.set(k, f(k, v));
|
|
906
|
-
}
|
|
907
|
-
return result;
|
|
908
|
-
};
|
|
909
|
-
Dict2.filter = (predicate) => (m) => {
|
|
910
|
-
const result = new globalThis.Map();
|
|
911
|
-
for (const [k, v] of m) {
|
|
912
|
-
if (predicate(v)) {
|
|
913
|
-
result.set(k, v);
|
|
914
|
-
}
|
|
915
|
-
}
|
|
916
|
-
return result;
|
|
917
|
-
};
|
|
918
|
-
Dict2.filterWithKey = (predicate) => (m) => {
|
|
919
|
-
const result = new globalThis.Map();
|
|
920
|
-
for (const [k, v] of m) {
|
|
921
|
-
if (predicate(k, v)) {
|
|
922
|
-
result.set(k, v);
|
|
923
|
-
}
|
|
1902
|
+
var DictIs = {
|
|
1903
|
+
empty: (m) => m.size === 0,
|
|
1904
|
+
nonEmpty: (m) => m.size > 0
|
|
1905
|
+
};
|
|
1906
|
+
var empty = () => new globalThis.Map();
|
|
1907
|
+
var singleton = (key, value) => new globalThis.Map([[key, value]]);
|
|
1908
|
+
var DictFrom = {
|
|
1909
|
+
entries: (entries3) => new globalThis.Map(entries3),
|
|
1910
|
+
Record: (record) => new globalThis.Map(Object.entries(record)),
|
|
1911
|
+
Array: (data) => new globalThis.Map(data),
|
|
1912
|
+
nullable: (data) => data === null || data === void 0 ? Maybe.make.none() : Maybe.make.some(data)
|
|
1913
|
+
};
|
|
1914
|
+
var DictTo = {
|
|
1915
|
+
Record: (map5) => {
|
|
1916
|
+
const result = {};
|
|
1917
|
+
for (const [k, v] of map5) {
|
|
1918
|
+
result[k] = v;
|
|
924
1919
|
}
|
|
925
1920
|
return result;
|
|
926
|
-
}
|
|
927
|
-
|
|
928
|
-
|
|
929
|
-
|
|
930
|
-
|
|
931
|
-
|
|
932
|
-
|
|
1921
|
+
}
|
|
1922
|
+
};
|
|
1923
|
+
var groupBy2 = (f) => (as) => {
|
|
1924
|
+
const result = new globalThis.Map();
|
|
1925
|
+
for (const a of as) {
|
|
1926
|
+
const k = f(a);
|
|
1927
|
+
const existing = result.get(k);
|
|
1928
|
+
if (existing !== void 0) {
|
|
1929
|
+
existing.push(a);
|
|
1930
|
+
} else {
|
|
1931
|
+
result.set(k, [a]);
|
|
933
1932
|
}
|
|
934
|
-
|
|
935
|
-
|
|
936
|
-
|
|
937
|
-
|
|
938
|
-
|
|
939
|
-
|
|
940
|
-
|
|
941
|
-
|
|
942
|
-
|
|
1933
|
+
}
|
|
1934
|
+
return result;
|
|
1935
|
+
};
|
|
1936
|
+
var has = (key) => (data) => data.has(key);
|
|
1937
|
+
var lookup = (key) => (data) => {
|
|
1938
|
+
const val = data.get(key);
|
|
1939
|
+
return val !== void 0 || data.has(key) ? Maybe.make.some(val) : Maybe.make.none();
|
|
1940
|
+
};
|
|
1941
|
+
var size2 = (data) => data.size;
|
|
1942
|
+
var keys = (data) => Array.from(data.keys());
|
|
1943
|
+
var values = (data) => Array.from(data.values());
|
|
1944
|
+
var entries = (data) => Array.from(data.entries());
|
|
1945
|
+
var insert = (key, value) => (data) => {
|
|
1946
|
+
const res = new globalThis.Map(data);
|
|
1947
|
+
res.set(key, value);
|
|
1948
|
+
return res;
|
|
1949
|
+
};
|
|
1950
|
+
var remove = (key) => (data) => {
|
|
1951
|
+
if (!data.has(key)) {
|
|
1952
|
+
return data;
|
|
1953
|
+
}
|
|
1954
|
+
const res = new globalThis.Map(data);
|
|
1955
|
+
res.delete(key);
|
|
1956
|
+
return res;
|
|
1957
|
+
};
|
|
1958
|
+
var upsert = (key, f) => (data) => {
|
|
1959
|
+
const res = new globalThis.Map(data);
|
|
1960
|
+
const existing = data.has(key) ? Maybe.make.some(data.get(key)) : Maybe.make.none();
|
|
1961
|
+
res.set(key, f(existing));
|
|
1962
|
+
return res;
|
|
1963
|
+
};
|
|
1964
|
+
var map2 = (f) => (data) => {
|
|
1965
|
+
const res = new globalThis.Map();
|
|
1966
|
+
for (const [k, v] of data) {
|
|
1967
|
+
res.set(k, f(v));
|
|
1968
|
+
}
|
|
1969
|
+
return res;
|
|
1970
|
+
};
|
|
1971
|
+
var mapWithKey = (f) => (data) => {
|
|
1972
|
+
const res = new globalThis.Map();
|
|
1973
|
+
for (const [k, v] of data) {
|
|
1974
|
+
res.set(k, f(k, v));
|
|
1975
|
+
}
|
|
1976
|
+
return res;
|
|
1977
|
+
};
|
|
1978
|
+
var filter2 = (predicate) => (data) => {
|
|
1979
|
+
const res = new globalThis.Map();
|
|
1980
|
+
for (const [k, v] of data) {
|
|
1981
|
+
if (predicate(v)) {
|
|
1982
|
+
res.set(k, v);
|
|
943
1983
|
}
|
|
944
|
-
|
|
945
|
-
|
|
946
|
-
|
|
947
|
-
|
|
948
|
-
|
|
949
|
-
|
|
1984
|
+
}
|
|
1985
|
+
return res;
|
|
1986
|
+
};
|
|
1987
|
+
var filterWithKey = (predicate) => (data) => {
|
|
1988
|
+
const res = new globalThis.Map();
|
|
1989
|
+
for (const [k, v] of data) {
|
|
1990
|
+
if (predicate(k, v)) {
|
|
1991
|
+
res.set(k, v);
|
|
950
1992
|
}
|
|
951
|
-
|
|
952
|
-
|
|
953
|
-
|
|
954
|
-
|
|
955
|
-
|
|
956
|
-
|
|
957
|
-
|
|
958
|
-
|
|
1993
|
+
}
|
|
1994
|
+
return res;
|
|
1995
|
+
};
|
|
1996
|
+
var compact2 = (data) => {
|
|
1997
|
+
const res = new globalThis.Map();
|
|
1998
|
+
for (const [k, v] of data) {
|
|
1999
|
+
if (v.kind === "Some") {
|
|
2000
|
+
res.set(k, v.value);
|
|
959
2001
|
}
|
|
960
|
-
|
|
961
|
-
|
|
962
|
-
|
|
963
|
-
|
|
964
|
-
|
|
965
|
-
|
|
966
|
-
|
|
967
|
-
|
|
2002
|
+
}
|
|
2003
|
+
return res;
|
|
2004
|
+
};
|
|
2005
|
+
var filterMap2 = (f) => (data) => {
|
|
2006
|
+
const res = new globalThis.Map();
|
|
2007
|
+
for (const [k, v] of data) {
|
|
2008
|
+
const mb = f(v);
|
|
2009
|
+
if (mb.kind === "Some") {
|
|
2010
|
+
res.set(k, mb.value);
|
|
968
2011
|
}
|
|
969
|
-
|
|
970
|
-
|
|
971
|
-
|
|
972
|
-
|
|
973
|
-
|
|
974
|
-
|
|
2012
|
+
}
|
|
2013
|
+
return res;
|
|
2014
|
+
};
|
|
2015
|
+
var union = (other) => (data) => {
|
|
2016
|
+
if (data.size === 0) {
|
|
2017
|
+
return other;
|
|
2018
|
+
}
|
|
2019
|
+
if (other.size === 0) {
|
|
2020
|
+
return data;
|
|
2021
|
+
}
|
|
2022
|
+
const res = new globalThis.Map(data);
|
|
2023
|
+
for (const [k, v] of other) {
|
|
2024
|
+
res.set(k, v);
|
|
2025
|
+
}
|
|
2026
|
+
return res;
|
|
2027
|
+
};
|
|
2028
|
+
var intersection = (other) => (data) => {
|
|
2029
|
+
const res = new globalThis.Map();
|
|
2030
|
+
for (const [k, v] of data) {
|
|
2031
|
+
if (other.has(k)) {
|
|
2032
|
+
res.set(k, v);
|
|
975
2033
|
}
|
|
976
|
-
|
|
977
|
-
|
|
978
|
-
|
|
979
|
-
|
|
980
|
-
|
|
981
|
-
|
|
2034
|
+
}
|
|
2035
|
+
return res;
|
|
2036
|
+
};
|
|
2037
|
+
var difference = (other) => (data) => {
|
|
2038
|
+
if (other.size === 0) {
|
|
2039
|
+
return data;
|
|
2040
|
+
}
|
|
2041
|
+
const res = new globalThis.Map();
|
|
2042
|
+
for (const [k, v] of data) {
|
|
2043
|
+
if (!other.has(k)) {
|
|
2044
|
+
res.set(k, v);
|
|
982
2045
|
}
|
|
983
|
-
|
|
984
|
-
|
|
985
|
-
|
|
986
|
-
|
|
987
|
-
|
|
988
|
-
|
|
989
|
-
|
|
990
|
-
|
|
991
|
-
|
|
992
|
-
|
|
993
|
-
|
|
994
|
-
|
|
995
|
-
|
|
996
|
-
|
|
2046
|
+
}
|
|
2047
|
+
return res;
|
|
2048
|
+
};
|
|
2049
|
+
var reduce2 = (init2, f) => (data) => {
|
|
2050
|
+
let acc = init2;
|
|
2051
|
+
for (const [, v] of data) {
|
|
2052
|
+
acc = f(acc, v);
|
|
2053
|
+
}
|
|
2054
|
+
return acc;
|
|
2055
|
+
};
|
|
2056
|
+
var reduceWithKey = (init2, f) => (data) => {
|
|
2057
|
+
let acc = init2;
|
|
2058
|
+
for (const [k, v] of data) {
|
|
2059
|
+
acc = f(acc, v, k);
|
|
2060
|
+
}
|
|
2061
|
+
return acc;
|
|
2062
|
+
};
|
|
2063
|
+
function mergeWith(combine) {
|
|
2064
|
+
return ((arg1, arg2) => {
|
|
2065
|
+
if (arg2 !== void 0) {
|
|
2066
|
+
const res = new globalThis.Map(arg1);
|
|
2067
|
+
for (const [k, v] of arg2) {
|
|
2068
|
+
if (res.has(k)) {
|
|
2069
|
+
res.set(k, combine(res.get(k), v));
|
|
2070
|
+
} else {
|
|
2071
|
+
res.set(k, v);
|
|
997
2072
|
}
|
|
998
|
-
return res;
|
|
999
2073
|
}
|
|
2074
|
+
return res;
|
|
2075
|
+
}
|
|
2076
|
+
return (first) => {
|
|
1000
2077
|
const second = arg1;
|
|
1001
|
-
|
|
1002
|
-
|
|
1003
|
-
|
|
1004
|
-
|
|
1005
|
-
|
|
1006
|
-
|
|
1007
|
-
res.set(k, v);
|
|
1008
|
-
}
|
|
2078
|
+
const res = new globalThis.Map(first);
|
|
2079
|
+
for (const [k, v] of second) {
|
|
2080
|
+
if (res.has(k)) {
|
|
2081
|
+
res.set(k, combine(res.get(k), v));
|
|
2082
|
+
} else {
|
|
2083
|
+
res.set(k, v);
|
|
1009
2084
|
}
|
|
1010
|
-
|
|
1011
|
-
|
|
2085
|
+
}
|
|
2086
|
+
return res;
|
|
1012
2087
|
};
|
|
2088
|
+
});
|
|
2089
|
+
}
|
|
2090
|
+
var mapEntries = (f) => (data) => {
|
|
2091
|
+
const res = new globalThis.Map();
|
|
2092
|
+
for (const [k, v] of data) {
|
|
2093
|
+
const [nk, nv] = f(k, v);
|
|
2094
|
+
res.set(nk, nv);
|
|
1013
2095
|
}
|
|
1014
|
-
|
|
1015
|
-
|
|
1016
|
-
|
|
1017
|
-
|
|
1018
|
-
|
|
1019
|
-
|
|
1020
|
-
|
|
1021
|
-
|
|
1022
|
-
|
|
1023
|
-
|
|
1024
|
-
|
|
1025
|
-
|
|
1026
|
-
|
|
1027
|
-
|
|
1028
|
-
|
|
1029
|
-
|
|
1030
|
-
|
|
1031
|
-
|
|
1032
|
-
|
|
1033
|
-
}
|
|
1034
|
-
|
|
1035
|
-
|
|
2096
|
+
return res;
|
|
2097
|
+
};
|
|
2098
|
+
var mapKeys = (f) => (data) => {
|
|
2099
|
+
const res = new globalThis.Map();
|
|
2100
|
+
for (const [k, v] of data) {
|
|
2101
|
+
res.set(f(k), v);
|
|
2102
|
+
}
|
|
2103
|
+
return res;
|
|
2104
|
+
};
|
|
2105
|
+
var _nonEmptySingleton = (key, value) => new globalThis.Map([[key, value]]);
|
|
2106
|
+
var _nonEmptyFromMap = (m) => m.size > 0 ? Maybe.make.some(m) : Maybe.make.none();
|
|
2107
|
+
var _nonEmptyKeys = (m) => keys(m);
|
|
2108
|
+
var _nonEmptyValues = (m) => values(m);
|
|
2109
|
+
var _nonEmptyEntries = (m) => entries(m);
|
|
2110
|
+
var _nonEmptyReduce = (f) => (m) => _nonEmptyValues(m).reduce(f);
|
|
2111
|
+
var _nonEmptyMap = (f) => (m) => map2(f)(m);
|
|
2112
|
+
var _nonEmptyMapWithKey = (f) => (m) => mapWithKey(f)(m);
|
|
2113
|
+
var DictNonEmptyConst = {
|
|
2114
|
+
singleton: _nonEmptySingleton,
|
|
2115
|
+
from: { Map: _nonEmptyFromMap },
|
|
2116
|
+
keys: _nonEmptyKeys,
|
|
2117
|
+
values: _nonEmptyValues,
|
|
2118
|
+
entries: _nonEmptyEntries,
|
|
2119
|
+
reduce: _nonEmptyReduce,
|
|
2120
|
+
map: _nonEmptyMap,
|
|
2121
|
+
mapWithKey: _nonEmptyMapWithKey
|
|
2122
|
+
};
|
|
2123
|
+
var Dict = {
|
|
2124
|
+
is: DictIs,
|
|
2125
|
+
empty,
|
|
2126
|
+
singleton,
|
|
2127
|
+
from: DictFrom,
|
|
2128
|
+
to: DictTo,
|
|
2129
|
+
groupBy: groupBy2,
|
|
2130
|
+
has,
|
|
2131
|
+
lookup,
|
|
2132
|
+
size: size2,
|
|
2133
|
+
keys,
|
|
2134
|
+
values,
|
|
2135
|
+
entries,
|
|
2136
|
+
insert,
|
|
2137
|
+
remove,
|
|
2138
|
+
upsert,
|
|
2139
|
+
map: map2,
|
|
2140
|
+
mapWithKey,
|
|
2141
|
+
filter: filter2,
|
|
2142
|
+
filterWithKey,
|
|
2143
|
+
compact: compact2,
|
|
2144
|
+
filterMap: filterMap2,
|
|
2145
|
+
union,
|
|
2146
|
+
intersection,
|
|
2147
|
+
difference,
|
|
2148
|
+
reduce: reduce2,
|
|
2149
|
+
reduceWithKey,
|
|
2150
|
+
mergeWith,
|
|
2151
|
+
mapEntries,
|
|
2152
|
+
mapKeys,
|
|
2153
|
+
NonEmpty: DictNonEmptyConst
|
|
2154
|
+
};
|
|
1036
2155
|
|
|
1037
2156
|
// src/Data/Json.ts
|
|
1038
2157
|
var isSyntaxError = (err) => typeof err === "object" && err !== null && "name" in err && err.name === "SyntaxError";
|
|
1039
2158
|
var isTypeError = (err) => typeof err === "object" && err !== null && "name" in err && err.name === "TypeError";
|
|
1040
|
-
var Json
|
|
1041
|
-
|
|
1042
|
-
|
|
2159
|
+
var Json = {
|
|
2160
|
+
/**
|
|
2161
|
+
* Safely parses a JSON string into `unknown`.
|
|
2162
|
+
* Converts thrown exceptions into a `Result<SyntaxError, unknown>`.
|
|
2163
|
+
*
|
|
2164
|
+
* @example
|
|
2165
|
+
* ```ts
|
|
2166
|
+
* Json.parse('{"a": 1}'); // Ok({ a: 1 })
|
|
2167
|
+
* Json.parse('{invalid}'); // Err(SyntaxError)
|
|
2168
|
+
* ```
|
|
2169
|
+
*/
|
|
2170
|
+
parse: (text) => Result.tryCatch(() => JSON.parse(text), {
|
|
1043
2171
|
onError: (err) => isSyntaxError(err) ? err : new SyntaxError(String(err))
|
|
1044
|
-
})
|
|
1045
|
-
|
|
2172
|
+
}),
|
|
2173
|
+
/**
|
|
2174
|
+
* Safely stringifies a value into a JSON string.
|
|
2175
|
+
* Converts thrown exceptions (e.g. circular references) into a `Result<TypeError, string>`.
|
|
2176
|
+
*
|
|
2177
|
+
* @example
|
|
2178
|
+
* ```ts
|
|
2179
|
+
* Json.stringify({ a: 1 }); // Ok('{"a":1}')
|
|
2180
|
+
* ```
|
|
2181
|
+
*/
|
|
2182
|
+
stringify: (value, replacer, space) => Result.tryCatch(() => JSON.stringify(value, replacer, space), {
|
|
1046
2183
|
onError: (err) => isTypeError(err) ? err : new TypeError(String(err))
|
|
1047
|
-
})
|
|
1048
|
-
}
|
|
2184
|
+
})
|
|
2185
|
+
};
|
|
1049
2186
|
|
|
1050
2187
|
// src/Data/Num.ts
|
|
1051
|
-
var
|
|
1052
|
-
|
|
1053
|
-
let
|
|
1054
|
-
|
|
1055
|
-
|
|
1056
|
-
|
|
1057
|
-
|
|
1058
|
-
|
|
1059
|
-
|
|
1060
|
-
|
|
1061
|
-
|
|
1062
|
-
|
|
1063
|
-
|
|
1064
|
-
|
|
1065
|
-
|
|
2188
|
+
var sumFn = (ns) => {
|
|
2189
|
+
let result = 0;
|
|
2190
|
+
for (let i = 0; i < ns.length; i++) {
|
|
2191
|
+
result += ns[i];
|
|
2192
|
+
}
|
|
2193
|
+
return result;
|
|
2194
|
+
};
|
|
2195
|
+
var Num = {
|
|
2196
|
+
is: {
|
|
2197
|
+
/**
|
|
2198
|
+
* Returns `true` when the number is equal to zero.
|
|
2199
|
+
*
|
|
2200
|
+
* @example
|
|
2201
|
+
* ```ts
|
|
2202
|
+
* Num.is.zero(0); // true
|
|
2203
|
+
* Num.is.zero(5); // false
|
|
2204
|
+
* ```
|
|
2205
|
+
*/
|
|
2206
|
+
zero: (n) => n === 0,
|
|
2207
|
+
/**
|
|
2208
|
+
* Returns `true` when the number is a whole integer.
|
|
2209
|
+
*
|
|
2210
|
+
* @example
|
|
2211
|
+
* ```ts
|
|
2212
|
+
* Num.is.integer(5); // true
|
|
2213
|
+
* Num.is.integer(3.14); // false
|
|
2214
|
+
* ```
|
|
2215
|
+
*/
|
|
2216
|
+
integer: (n) => Number.isInteger(n),
|
|
2217
|
+
/**
|
|
2218
|
+
* Returns `true` when the number is a finite float (fractional number).
|
|
2219
|
+
*
|
|
2220
|
+
* @example
|
|
2221
|
+
* ```ts
|
|
2222
|
+
* Num.is.float(3.14); // true
|
|
2223
|
+
* Num.is.float(5); // false
|
|
2224
|
+
* ```
|
|
2225
|
+
*/
|
|
2226
|
+
float: (n) => Number.isFinite(n) && !Number.isInteger(n),
|
|
2227
|
+
/**
|
|
2228
|
+
* Returns `true` when the number is finite (not `Infinity`, `-Infinity`, or `NaN`).
|
|
2229
|
+
*
|
|
2230
|
+
* @example
|
|
2231
|
+
* ```ts
|
|
2232
|
+
* Num.is.finite(42); // true
|
|
2233
|
+
* Num.is.finite(Infinity); // false
|
|
2234
|
+
* ```
|
|
2235
|
+
*/
|
|
2236
|
+
finite: (n) => Number.isFinite(n),
|
|
2237
|
+
/**
|
|
2238
|
+
* Returns `true` when the value is `NaN`.
|
|
2239
|
+
*
|
|
2240
|
+
* @example
|
|
2241
|
+
* ```ts
|
|
2242
|
+
* Num.is.nan(NaN); // true
|
|
2243
|
+
* Num.is.nan(42); // false
|
|
2244
|
+
* ```
|
|
2245
|
+
*/
|
|
2246
|
+
nan: (n) => Number.isNaN(n),
|
|
2247
|
+
/**
|
|
2248
|
+
* Returns `true` when the number is an even integer.
|
|
2249
|
+
*
|
|
2250
|
+
* @example
|
|
2251
|
+
* ```ts
|
|
2252
|
+
* Num.is.even(4); // true
|
|
2253
|
+
* Num.is.even(3); // false
|
|
2254
|
+
* Num.is.even(2.5); // false
|
|
2255
|
+
* ```
|
|
2256
|
+
*/
|
|
2257
|
+
even: (n) => Number.isInteger(n) && n % 2 === 0,
|
|
2258
|
+
/**
|
|
2259
|
+
* Returns `true` when the number is an odd integer.
|
|
2260
|
+
*
|
|
2261
|
+
* @example
|
|
2262
|
+
* ```ts
|
|
2263
|
+
* Num.is.odd(3); // true
|
|
2264
|
+
* Num.is.odd(4); // false
|
|
2265
|
+
* Num.is.odd(2.5); // false
|
|
2266
|
+
* ```
|
|
2267
|
+
*/
|
|
2268
|
+
odd: (n) => Number.isInteger(n) && n % 2 !== 0,
|
|
2269
|
+
/**
|
|
2270
|
+
* Returns `true` when the number is strictly greater than zero.
|
|
2271
|
+
*
|
|
2272
|
+
* @example
|
|
2273
|
+
* ```ts
|
|
2274
|
+
* Num.is.positive(5); // true
|
|
2275
|
+
* Num.is.positive(0); // false
|
|
2276
|
+
* Num.is.positive(-5); // false
|
|
2277
|
+
* ```
|
|
2278
|
+
*/
|
|
2279
|
+
positive: (n) => n > 0,
|
|
2280
|
+
/**
|
|
2281
|
+
* Returns `true` when the number is strictly less than zero.
|
|
2282
|
+
*
|
|
2283
|
+
* @example
|
|
2284
|
+
* ```ts
|
|
2285
|
+
* Num.is.negative(-5); // true
|
|
2286
|
+
* Num.is.negative(0); // false
|
|
2287
|
+
* Num.is.negative(5); // false
|
|
2288
|
+
* ```
|
|
2289
|
+
*/
|
|
2290
|
+
negative: (n) => n < 0
|
|
2291
|
+
},
|
|
2292
|
+
/**
|
|
2293
|
+
* Generates an array of numbers from `from` to `to` (both inclusive),
|
|
2294
|
+
* stepping by `step` (default `1`). If `step` is negative or zero, or `from > to`,
|
|
2295
|
+
* returns an empty array. When `step` does not land exactly on `to`, the last value
|
|
2296
|
+
* is the largest reachable value that does not exceed `to`.
|
|
2297
|
+
*
|
|
2298
|
+
* @example
|
|
2299
|
+
* ```ts
|
|
2300
|
+
* Num.range(0, 5); // [0, 1, 2, 3, 4, 5]
|
|
2301
|
+
* Num.range(0, 10, 2); // [0, 2, 4, 6, 8, 10]
|
|
2302
|
+
* Num.range(0, 9, 2); // [0, 2, 4, 6, 8]
|
|
2303
|
+
* Num.range(5, 0); // []
|
|
2304
|
+
* Num.range(3, 3); // [3]
|
|
2305
|
+
* ```
|
|
2306
|
+
*/
|
|
2307
|
+
range: (from, to, step = 1) => {
|
|
1066
2308
|
if (step <= 0 || from > to) {
|
|
1067
2309
|
return [];
|
|
1068
2310
|
}
|
|
@@ -1072,36 +2314,191 @@ var Num;
|
|
|
1072
2314
|
result[i] = from + i * step;
|
|
1073
2315
|
}
|
|
1074
2316
|
return result;
|
|
1075
|
-
}
|
|
1076
|
-
|
|
1077
|
-
|
|
1078
|
-
|
|
1079
|
-
|
|
2317
|
+
},
|
|
2318
|
+
/**
|
|
2319
|
+
* Clamps a number between `min` and `max` (both inclusive).
|
|
2320
|
+
*
|
|
2321
|
+
* @example
|
|
2322
|
+
* ```ts
|
|
2323
|
+
* pipe(150, Num.clamp(0, 100)); // 100
|
|
2324
|
+
* pipe(-5, Num.clamp(0, 100)); // 0
|
|
2325
|
+
* pipe(42, Num.clamp(0, 100)); // 42
|
|
2326
|
+
* ```
|
|
2327
|
+
*/
|
|
2328
|
+
clamp: (min, max) => (n) => Math.min(Math.max(n, min), max),
|
|
2329
|
+
/**
|
|
2330
|
+
* Returns `true` when the number is between `min` and `max` (both inclusive).
|
|
2331
|
+
*
|
|
2332
|
+
* @example
|
|
2333
|
+
* ```ts
|
|
2334
|
+
* pipe(5, Num.between(1, 10)); // true
|
|
2335
|
+
* pipe(0, Num.between(1, 10)); // false
|
|
2336
|
+
* pipe(10, Num.between(1, 10)); // true
|
|
2337
|
+
* ```
|
|
2338
|
+
*/
|
|
2339
|
+
between: (min, max) => (n) => n >= min && n <= max,
|
|
2340
|
+
/**
|
|
2341
|
+
* Returns `true` when the number is in the range `[start, end)` (inclusive of `start`, exclusive of `end`).
|
|
2342
|
+
*
|
|
2343
|
+
* @example
|
|
2344
|
+
* ```ts
|
|
2345
|
+
* pipe(5, Num.inRange(1, 10)); // true
|
|
2346
|
+
* pipe(1, Num.inRange(1, 10)); // true
|
|
2347
|
+
* pipe(10, Num.inRange(1, 10)); // false
|
|
2348
|
+
* ```
|
|
2349
|
+
*/
|
|
2350
|
+
inRange: (start, end) => (n) => n >= start && n < end,
|
|
2351
|
+
/**
|
|
2352
|
+
* Parses a string as a number. Returns `None` when the result is `NaN`.
|
|
2353
|
+
*
|
|
2354
|
+
* @example
|
|
2355
|
+
* ```ts
|
|
2356
|
+
* Num.parse("42"); // Some(42)
|
|
2357
|
+
* Num.parse("3.14"); // Some(3.14)
|
|
2358
|
+
* Num.parse("abc"); // None
|
|
2359
|
+
* Num.parse(""); // None
|
|
2360
|
+
* ```
|
|
2361
|
+
*/
|
|
2362
|
+
parse: (s) => {
|
|
1080
2363
|
if (s.trim() === "") {
|
|
1081
2364
|
return Maybe.make.none();
|
|
1082
2365
|
}
|
|
1083
2366
|
const n = Number(s);
|
|
1084
2367
|
return isNaN(n) ? Maybe.make.none() : Maybe.make.some(n);
|
|
1085
|
-
}
|
|
1086
|
-
|
|
1087
|
-
|
|
1088
|
-
|
|
1089
|
-
|
|
1090
|
-
|
|
1091
|
-
|
|
1092
|
-
|
|
1093
|
-
|
|
1094
|
-
|
|
1095
|
-
|
|
1096
|
-
|
|
1097
|
-
|
|
1098
|
-
|
|
1099
|
-
|
|
1100
|
-
|
|
1101
|
-
|
|
1102
|
-
|
|
1103
|
-
|
|
1104
|
-
|
|
2368
|
+
},
|
|
2369
|
+
/**
|
|
2370
|
+
* Adds `b` to a number. Data-last: use in `pipe` or `Arr.map`.
|
|
2371
|
+
*
|
|
2372
|
+
* @example
|
|
2373
|
+
* ```ts
|
|
2374
|
+
* pipe(5, Num.add(3)); // 8
|
|
2375
|
+
* pipe([1, 2, 3], Arr.map(Num.add(10))); // [11, 12, 13]
|
|
2376
|
+
* ```
|
|
2377
|
+
*/
|
|
2378
|
+
add: (b) => (a) => a + b,
|
|
2379
|
+
/**
|
|
2380
|
+
* Subtracts `b` from a number. Data-last: `subtract(b)(a)` = `a - b`.
|
|
2381
|
+
*
|
|
2382
|
+
* @example
|
|
2383
|
+
* ```ts
|
|
2384
|
+
* pipe(10, Num.subtract(3)); // 7
|
|
2385
|
+
* pipe([5, 10, 15], Arr.map(Num.subtract(2))); // [3, 8, 13]
|
|
2386
|
+
* ```
|
|
2387
|
+
*/
|
|
2388
|
+
subtract: (b) => (a) => a - b,
|
|
2389
|
+
/**
|
|
2390
|
+
* Multiplies a number by `b`. Data-last: use in `pipe` or `Arr.map`.
|
|
2391
|
+
*
|
|
2392
|
+
* @example
|
|
2393
|
+
* ```ts
|
|
2394
|
+
* pipe(6, Num.multiply(7)); // 42
|
|
2395
|
+
* pipe([1, 2, 3], Arr.map(Num.multiply(100))); // [100, 200, 300]
|
|
2396
|
+
* ```
|
|
2397
|
+
*/
|
|
2398
|
+
multiply: (b) => (a) => a * b,
|
|
2399
|
+
/**
|
|
2400
|
+
* Divides a number by `b`. Returns `None` when `b` is zero. Data-last: `divide(b)(a)` = `a / b`.
|
|
2401
|
+
*
|
|
2402
|
+
* @example
|
|
2403
|
+
* ```ts
|
|
2404
|
+
* pipe(20, Num.divide(4)); // Some(5)
|
|
2405
|
+
* pipe(5, Num.divide(0)); // None
|
|
2406
|
+
* pipe([10, 20, 30], Arr.filterMap(Num.divide(10))); // [1, 2, 3]
|
|
2407
|
+
* ```
|
|
2408
|
+
*/
|
|
2409
|
+
divide: (b) => (a) => b === 0 ? Maybe.make.none() : Maybe.make.some(a / b),
|
|
2410
|
+
/**
|
|
2411
|
+
* Returns the absolute value of a number.
|
|
2412
|
+
*
|
|
2413
|
+
* @example
|
|
2414
|
+
* ```ts
|
|
2415
|
+
* pipe(-5, Num.abs); // 5
|
|
2416
|
+
* pipe(5, Num.abs); // 5
|
|
2417
|
+
* ```
|
|
2418
|
+
*/
|
|
2419
|
+
abs: (n) => Math.abs(n),
|
|
2420
|
+
/**
|
|
2421
|
+
* Negates a number (arithmetic negation).
|
|
2422
|
+
*
|
|
2423
|
+
* @example
|
|
2424
|
+
* ```ts
|
|
2425
|
+
* pipe(5, Num.negate); // -5
|
|
2426
|
+
* pipe(-5, Num.negate); // 5
|
|
2427
|
+
* ```
|
|
2428
|
+
*/
|
|
2429
|
+
negate: (n) => -n,
|
|
2430
|
+
/**
|
|
2431
|
+
* Rounds a number to the nearest integer.
|
|
2432
|
+
*
|
|
2433
|
+
* @example
|
|
2434
|
+
* ```ts
|
|
2435
|
+
* pipe(3.5, Num.round); // 4
|
|
2436
|
+
* pipe(3.4, Num.round); // 3
|
|
2437
|
+
* ```
|
|
2438
|
+
*/
|
|
2439
|
+
round: (n) => Math.round(n),
|
|
2440
|
+
/**
|
|
2441
|
+
* Rounds a number down to the nearest integer.
|
|
2442
|
+
*
|
|
2443
|
+
* @example
|
|
2444
|
+
* ```ts
|
|
2445
|
+
* pipe(3.9, Num.floor); // 3
|
|
2446
|
+
* pipe(-3.2, Num.floor); // -4
|
|
2447
|
+
* ```
|
|
2448
|
+
*/
|
|
2449
|
+
floor: (n) => Math.floor(n),
|
|
2450
|
+
/**
|
|
2451
|
+
* Rounds a number up to the nearest integer.
|
|
2452
|
+
*
|
|
2453
|
+
* @example
|
|
2454
|
+
* ```ts
|
|
2455
|
+
* pipe(3.1, Num.ceil); // 4
|
|
2456
|
+
* pipe(-3.9, Num.ceil); // -3
|
|
2457
|
+
* ```
|
|
2458
|
+
*/
|
|
2459
|
+
ceil: (n) => Math.ceil(n),
|
|
2460
|
+
/**
|
|
2461
|
+
* Returns the remainder of dividing a number by `divisor`. Returns `None` when `divisor` is zero.
|
|
2462
|
+
* Data-last: `remainder(divisor)(a)` = `a % divisor`.
|
|
2463
|
+
*
|
|
2464
|
+
* @example
|
|
2465
|
+
* ```ts
|
|
2466
|
+
* pipe(10, Num.remainder(3)); // Some(1)
|
|
2467
|
+
* pipe(5, Num.remainder(0)); // None
|
|
2468
|
+
* pipe([10, 11, 12], Arr.filterMap(Num.remainder(3))); // [1, 2, 0]
|
|
2469
|
+
* ```
|
|
2470
|
+
*/
|
|
2471
|
+
remainder: (divisor) => (n) => divisor === 0 ? Maybe.make.none() : Maybe.make.some(n % divisor),
|
|
2472
|
+
/**
|
|
2473
|
+
* Computes the sum of a list of numbers. Returns `0` if the list is empty.
|
|
2474
|
+
*
|
|
2475
|
+
* @example
|
|
2476
|
+
* ```ts
|
|
2477
|
+
* Num.sum([1, 2, 3]); // 6
|
|
2478
|
+
* Num.sum([]); // 0
|
|
2479
|
+
* ```
|
|
2480
|
+
*/
|
|
2481
|
+
sum: sumFn,
|
|
2482
|
+
/**
|
|
2483
|
+
* Computes the mean of a list of numbers. Returns `None` if the list is empty.
|
|
2484
|
+
*
|
|
2485
|
+
* @example
|
|
2486
|
+
* ```ts
|
|
2487
|
+
* Num.mean([1, 2, 3]); // Some(2)
|
|
2488
|
+
* Num.mean([]); // None
|
|
2489
|
+
* ```
|
|
2490
|
+
*/
|
|
2491
|
+
mean: (ns) => ns.length === 0 ? Maybe.make.none() : Maybe.make.some(sumFn(ns) / ns.length),
|
|
2492
|
+
/**
|
|
2493
|
+
* Computes the minimum of a list of numbers. Returns `None` if the list is empty.
|
|
2494
|
+
*
|
|
2495
|
+
* @example
|
|
2496
|
+
* ```ts
|
|
2497
|
+
* Num.min([5, 1, 3]); // Some(1)
|
|
2498
|
+
* Num.min([]); // None
|
|
2499
|
+
* ```
|
|
2500
|
+
*/
|
|
2501
|
+
min: (ns) => {
|
|
1105
2502
|
if (ns.length === 0) {
|
|
1106
2503
|
return Maybe.make.none();
|
|
1107
2504
|
}
|
|
@@ -1112,8 +2509,17 @@ var Num;
|
|
|
1112
2509
|
}
|
|
1113
2510
|
}
|
|
1114
2511
|
return Maybe.make.some(result);
|
|
1115
|
-
}
|
|
1116
|
-
|
|
2512
|
+
},
|
|
2513
|
+
/**
|
|
2514
|
+
* Computes the maximum of a list of numbers. Returns `None` if the list is empty.
|
|
2515
|
+
*
|
|
2516
|
+
* @example
|
|
2517
|
+
* ```ts
|
|
2518
|
+
* Num.max([1, 5, 3]); // Some(5)
|
|
2519
|
+
* Num.max([]); // None
|
|
2520
|
+
* ```
|
|
2521
|
+
*/
|
|
2522
|
+
max: (ns) => {
|
|
1117
2523
|
if (ns.length === 0) {
|
|
1118
2524
|
return Maybe.make.none();
|
|
1119
2525
|
}
|
|
@@ -1124,9 +2530,20 @@ var Num;
|
|
|
1124
2530
|
}
|
|
1125
2531
|
}
|
|
1126
2532
|
return Maybe.make.some(result);
|
|
1127
|
-
}
|
|
1128
|
-
|
|
1129
|
-
|
|
2533
|
+
},
|
|
2534
|
+
/**
|
|
2535
|
+
* Formats a number using `Intl.NumberFormat`. Returns `None` when `n` is `NaN` or non-finite.
|
|
2536
|
+
* Data-last curried signature.
|
|
2537
|
+
*
|
|
2538
|
+
* @example
|
|
2539
|
+
* ```ts
|
|
2540
|
+
* const formatCurrency = Num.format({ style: "currency", currency: "USD" }, "en-US");
|
|
2541
|
+
* pipe(1234.5, formatCurrency); // Some("$1,234.50")
|
|
2542
|
+
* pipe(NaN, formatCurrency); // None
|
|
2543
|
+
* ```
|
|
2544
|
+
*/
|
|
2545
|
+
format: (options, locales) => (n) => !Number.isFinite(n) ? Maybe.make.none() : Maybe.make.some(new Intl.NumberFormat(locales, options).format(n))
|
|
2546
|
+
};
|
|
1130
2547
|
|
|
1131
2548
|
// src/Data/Rec.ts
|
|
1132
2549
|
var _isNonEmpty = (data) => Object.keys(data).length > 0;
|
|
@@ -1174,259 +2591,466 @@ var RecResult;
|
|
|
1174
2591
|
};
|
|
1175
2592
|
RecResult2.sequence = (data) => (0, RecResult2.traverse)((a) => a)(data);
|
|
1176
2593
|
})(RecResult || (RecResult = {}));
|
|
1177
|
-
var
|
|
1178
|
-
|
|
1179
|
-
|
|
1180
|
-
|
|
1181
|
-
|
|
1182
|
-
|
|
1183
|
-
|
|
1184
|
-
|
|
1185
|
-
|
|
1186
|
-
|
|
1187
|
-
|
|
1188
|
-
|
|
1189
|
-
|
|
1190
|
-
|
|
1191
|
-
|
|
1192
|
-
|
|
1193
|
-
|
|
1194
|
-
|
|
1195
|
-
|
|
1196
|
-
|
|
1197
|
-
|
|
1198
|
-
|
|
1199
|
-
|
|
1200
|
-
|
|
1201
|
-
|
|
1202
|
-
|
|
1203
|
-
|
|
1204
|
-
|
|
1205
|
-
|
|
1206
|
-
|
|
1207
|
-
|
|
1208
|
-
|
|
1209
|
-
|
|
1210
|
-
|
|
1211
|
-
}
|
|
1212
|
-
|
|
1213
|
-
|
|
1214
|
-
}
|
|
1215
|
-
return result;
|
|
1216
|
-
};
|
|
1217
|
-
Rec2.filterMap = (f) => (data) => {
|
|
1218
|
-
const recordKeys = Object.keys(data);
|
|
1219
|
-
const recordValues = Object.values(data);
|
|
1220
|
-
const result = Object.create(Object.getPrototypeOf(data));
|
|
1221
|
-
for (let i = 0; i < recordKeys.length; i++) {
|
|
1222
|
-
const maybeVal = f(recordValues[i]);
|
|
1223
|
-
if (maybeVal.kind === "Some") {
|
|
1224
|
-
_setKey(result, recordKeys[i], maybeVal.value);
|
|
1225
|
-
}
|
|
2594
|
+
var RecIs = {
|
|
2595
|
+
/**
|
|
2596
|
+
* Returns true if the record has no keys.
|
|
2597
|
+
*
|
|
2598
|
+
* @example
|
|
2599
|
+
* ```ts
|
|
2600
|
+
* Rec.is.empty({}); // true
|
|
2601
|
+
* Rec.is.empty({ a: 1 }); // false
|
|
2602
|
+
* ```
|
|
2603
|
+
*/
|
|
2604
|
+
empty: (data) => Object.keys(data).length === 0,
|
|
2605
|
+
/**
|
|
2606
|
+
* Type guard to check if a record is non-empty.
|
|
2607
|
+
*
|
|
2608
|
+
* @example
|
|
2609
|
+
* ```ts
|
|
2610
|
+
* Rec.is.nonEmpty({ a: 1 }); // true
|
|
2611
|
+
* Rec.is.nonEmpty({}); // false
|
|
2612
|
+
* ```
|
|
2613
|
+
*/
|
|
2614
|
+
nonEmpty: _isNonEmpty
|
|
2615
|
+
};
|
|
2616
|
+
var map3 = (f) => (data) => {
|
|
2617
|
+
const recordKeys = Object.keys(data);
|
|
2618
|
+
const recordValues = Object.values(data);
|
|
2619
|
+
const result = Object.create(Object.getPrototypeOf(data));
|
|
2620
|
+
for (let i = 0; i < recordKeys.length; i++) {
|
|
2621
|
+
const key = recordKeys[i];
|
|
2622
|
+
if (key === "__proto__") {
|
|
2623
|
+
Object.defineProperty(result, "__proto__", {
|
|
2624
|
+
value: f(recordValues[i]),
|
|
2625
|
+
writable: true,
|
|
2626
|
+
enumerable: true,
|
|
2627
|
+
configurable: true
|
|
2628
|
+
});
|
|
2629
|
+
} else {
|
|
2630
|
+
result[key] = f(recordValues[i]);
|
|
1226
2631
|
}
|
|
1227
|
-
|
|
1228
|
-
|
|
1229
|
-
|
|
1230
|
-
|
|
1231
|
-
|
|
1232
|
-
|
|
1233
|
-
|
|
1234
|
-
|
|
1235
|
-
|
|
2632
|
+
}
|
|
2633
|
+
return result;
|
|
2634
|
+
};
|
|
2635
|
+
var filterMap3 = (f) => (data) => {
|
|
2636
|
+
const recordKeys = Object.keys(data);
|
|
2637
|
+
const recordValues = Object.values(data);
|
|
2638
|
+
const result = Object.create(Object.getPrototypeOf(data));
|
|
2639
|
+
for (let i = 0; i < recordKeys.length; i++) {
|
|
2640
|
+
const maybeVal = f(recordValues[i]);
|
|
2641
|
+
if (maybeVal.kind === "Some") {
|
|
2642
|
+
_setKey(result, recordKeys[i], maybeVal.value);
|
|
1236
2643
|
}
|
|
1237
|
-
|
|
1238
|
-
|
|
1239
|
-
|
|
1240
|
-
|
|
1241
|
-
|
|
1242
|
-
|
|
1243
|
-
|
|
1244
|
-
|
|
1245
|
-
|
|
1246
|
-
|
|
2644
|
+
}
|
|
2645
|
+
return result;
|
|
2646
|
+
};
|
|
2647
|
+
var mapWithKey2 = (f) => (data) => {
|
|
2648
|
+
const recordKeys = Object.keys(data);
|
|
2649
|
+
const recordValues = Object.values(data);
|
|
2650
|
+
const result = Object.create(Object.getPrototypeOf(data));
|
|
2651
|
+
for (let i = 0; i < recordKeys.length; i++) {
|
|
2652
|
+
const key = recordKeys[i];
|
|
2653
|
+
_setKey(result, key, f(key, recordValues[i]));
|
|
2654
|
+
}
|
|
2655
|
+
return result;
|
|
2656
|
+
};
|
|
2657
|
+
var filter3 = (predicate) => (data) => {
|
|
2658
|
+
const recordKeys = Object.keys(data);
|
|
2659
|
+
const recordValues = Object.values(data);
|
|
2660
|
+
const result = Object.create(Object.getPrototypeOf(data));
|
|
2661
|
+
for (let i = 0; i < recordKeys.length; i++) {
|
|
2662
|
+
if (predicate(recordValues[i])) {
|
|
2663
|
+
_setKey(result, recordKeys[i], recordValues[i]);
|
|
1247
2664
|
}
|
|
1248
|
-
|
|
1249
|
-
|
|
1250
|
-
|
|
1251
|
-
|
|
1252
|
-
|
|
1253
|
-
|
|
1254
|
-
|
|
1255
|
-
|
|
2665
|
+
}
|
|
2666
|
+
return result;
|
|
2667
|
+
};
|
|
2668
|
+
var filterWithKey2 = (predicate) => (data) => {
|
|
2669
|
+
const result = {};
|
|
2670
|
+
for (const [k, v] of Object.entries(data)) {
|
|
2671
|
+
if (predicate(k, v)) {
|
|
2672
|
+
_setKey(result, k, v);
|
|
1256
2673
|
}
|
|
1257
|
-
|
|
1258
|
-
|
|
1259
|
-
|
|
1260
|
-
|
|
1261
|
-
|
|
1262
|
-
|
|
1263
|
-
|
|
1264
|
-
|
|
1265
|
-
|
|
1266
|
-
|
|
1267
|
-
|
|
1268
|
-
|
|
1269
|
-
|
|
1270
|
-
|
|
1271
|
-
|
|
1272
|
-
|
|
1273
|
-
|
|
1274
|
-
|
|
1275
|
-
|
|
2674
|
+
}
|
|
2675
|
+
return result;
|
|
2676
|
+
};
|
|
2677
|
+
var lookup2 = (key) => (data) => Object.hasOwn(data, key) ? { kind: "Some", value: data[key] } : { kind: "None" };
|
|
2678
|
+
var keys2 = (data) => Object.keys(data);
|
|
2679
|
+
var values2 = (data) => Object.values(data);
|
|
2680
|
+
var entries2 = (data) => Object.entries(data);
|
|
2681
|
+
var RecFrom = {
|
|
2682
|
+
/**
|
|
2683
|
+
* Creates a record from key-value pairs.
|
|
2684
|
+
*
|
|
2685
|
+
* @example
|
|
2686
|
+
* ```ts
|
|
2687
|
+
* Rec.from.entries([["a", 1], ["b", 2]]); // { a: 1, b: 2 }
|
|
2688
|
+
* ```
|
|
2689
|
+
*/
|
|
2690
|
+
entries: (data) => Object.fromEntries(data)
|
|
2691
|
+
};
|
|
2692
|
+
var groupBy3 = (keyFn) => (items) => {
|
|
2693
|
+
const result = {};
|
|
2694
|
+
for (const item of items) {
|
|
2695
|
+
const key = keyFn(item);
|
|
2696
|
+
if (Object.hasOwn(result, key)) {
|
|
2697
|
+
result[key].push(item);
|
|
2698
|
+
} else {
|
|
2699
|
+
_setKey(result, key, [item]);
|
|
1276
2700
|
}
|
|
1277
|
-
|
|
1278
|
-
|
|
1279
|
-
|
|
1280
|
-
|
|
1281
|
-
|
|
1282
|
-
|
|
1283
|
-
|
|
1284
|
-
|
|
2701
|
+
}
|
|
2702
|
+
return result;
|
|
2703
|
+
};
|
|
2704
|
+
var pick = (...pickedKeys) => (data) => {
|
|
2705
|
+
const result = {};
|
|
2706
|
+
for (const key of pickedKeys) {
|
|
2707
|
+
if (Object.hasOwn(data, key)) {
|
|
2708
|
+
_setKey(result, key, data[key]);
|
|
1285
2709
|
}
|
|
1286
|
-
|
|
1287
|
-
|
|
1288
|
-
|
|
1289
|
-
|
|
1290
|
-
|
|
1291
|
-
|
|
1292
|
-
|
|
1293
|
-
|
|
1294
|
-
|
|
2710
|
+
}
|
|
2711
|
+
return result;
|
|
2712
|
+
};
|
|
2713
|
+
var omit = (...omittedKeys) => (data) => {
|
|
2714
|
+
const omitSet = new Set(omittedKeys);
|
|
2715
|
+
const result = {};
|
|
2716
|
+
for (const key of Object.keys(data)) {
|
|
2717
|
+
if (!omitSet.has(key)) {
|
|
2718
|
+
_setKey(result, key, data[key]);
|
|
1295
2719
|
}
|
|
1296
|
-
|
|
1297
|
-
|
|
1298
|
-
|
|
1299
|
-
|
|
1300
|
-
|
|
1301
|
-
|
|
1302
|
-
|
|
1303
|
-
|
|
1304
|
-
|
|
1305
|
-
|
|
1306
|
-
|
|
1307
|
-
|
|
1308
|
-
|
|
1309
|
-
|
|
1310
|
-
|
|
1311
|
-
|
|
1312
|
-
|
|
1313
|
-
|
|
2720
|
+
}
|
|
2721
|
+
return result;
|
|
2722
|
+
};
|
|
2723
|
+
var merge = (other) => (data) => ({
|
|
2724
|
+
...data,
|
|
2725
|
+
...other
|
|
2726
|
+
});
|
|
2727
|
+
function mergeWith2(combine) {
|
|
2728
|
+
return (arg1, arg2) => {
|
|
2729
|
+
if (arg2 !== void 0) {
|
|
2730
|
+
const first = arg1;
|
|
2731
|
+
const second2 = arg2;
|
|
2732
|
+
const result = { ...first };
|
|
2733
|
+
for (const [k, v] of Object.entries(second2)) {
|
|
2734
|
+
if (Object.hasOwn(result, k)) {
|
|
2735
|
+
result[k] = combine(result[k], v);
|
|
2736
|
+
} else {
|
|
2737
|
+
result[k] = v;
|
|
1314
2738
|
}
|
|
1315
|
-
return result;
|
|
1316
2739
|
}
|
|
1317
|
-
|
|
1318
|
-
|
|
1319
|
-
|
|
1320
|
-
|
|
1321
|
-
|
|
1322
|
-
|
|
1323
|
-
|
|
1324
|
-
|
|
1325
|
-
|
|
2740
|
+
return result;
|
|
2741
|
+
}
|
|
2742
|
+
const second = arg1;
|
|
2743
|
+
return (first) => {
|
|
2744
|
+
const result = { ...first };
|
|
2745
|
+
for (const [k, v] of Object.entries(second)) {
|
|
2746
|
+
if (Object.hasOwn(result, k)) {
|
|
2747
|
+
result[k] = combine(result[k], v);
|
|
2748
|
+
} else {
|
|
2749
|
+
result[k] = v;
|
|
1326
2750
|
}
|
|
1327
|
-
return result;
|
|
1328
|
-
};
|
|
1329
|
-
};
|
|
1330
|
-
}
|
|
1331
|
-
Rec2.mergeWith = mergeWith;
|
|
1332
|
-
Rec2.size = (data) => Object.keys(data).length;
|
|
1333
|
-
Rec2.mapKeys = (f) => (data) => {
|
|
1334
|
-
const keys2 = Object.keys(data);
|
|
1335
|
-
if (keys2.length === 0) {
|
|
1336
|
-
return data;
|
|
1337
|
-
}
|
|
1338
|
-
const result = {};
|
|
1339
|
-
for (let i = 0; i < keys2.length; i++) {
|
|
1340
|
-
const k = keys2[i];
|
|
1341
|
-
_setKey(result, f(k), data[k]);
|
|
1342
|
-
}
|
|
1343
|
-
return result;
|
|
1344
|
-
};
|
|
1345
|
-
Rec2.compact = (data) => {
|
|
1346
|
-
const keys2 = Object.keys(data);
|
|
1347
|
-
if (keys2.length === 0) {
|
|
1348
|
-
return {};
|
|
1349
|
-
}
|
|
1350
|
-
const result = {};
|
|
1351
|
-
for (let i = 0; i < keys2.length; i++) {
|
|
1352
|
-
const k = keys2[i];
|
|
1353
|
-
const v = data[k];
|
|
1354
|
-
if (v.kind === "Some") {
|
|
1355
|
-
_setKey(result, k, v.value);
|
|
1356
2751
|
}
|
|
1357
|
-
|
|
1358
|
-
|
|
2752
|
+
return result;
|
|
2753
|
+
};
|
|
1359
2754
|
};
|
|
1360
|
-
|
|
1361
|
-
|
|
1362
|
-
|
|
1363
|
-
|
|
2755
|
+
}
|
|
2756
|
+
var size3 = (data) => Object.keys(data).length;
|
|
2757
|
+
var mapKeys2 = (f) => (data) => {
|
|
2758
|
+
const recKeys = Object.keys(data);
|
|
2759
|
+
if (recKeys.length === 0) {
|
|
2760
|
+
return data;
|
|
2761
|
+
}
|
|
2762
|
+
const result = {};
|
|
2763
|
+
for (let i = 0; i < recKeys.length; i++) {
|
|
2764
|
+
const k = recKeys[i];
|
|
2765
|
+
_setKey(result, f(k), data[k]);
|
|
2766
|
+
}
|
|
2767
|
+
return result;
|
|
2768
|
+
};
|
|
2769
|
+
var compact3 = (data) => {
|
|
2770
|
+
const recKeys = Object.keys(data);
|
|
2771
|
+
if (recKeys.length === 0) {
|
|
2772
|
+
return {};
|
|
2773
|
+
}
|
|
2774
|
+
const result = {};
|
|
2775
|
+
for (let i = 0; i < recKeys.length; i++) {
|
|
2776
|
+
const k = recKeys[i];
|
|
2777
|
+
const v = data[k];
|
|
2778
|
+
if (v.kind === "Some") {
|
|
2779
|
+
_setKey(result, k, v.value);
|
|
1364
2780
|
}
|
|
1365
|
-
|
|
1366
|
-
|
|
1367
|
-
|
|
1368
|
-
|
|
1369
|
-
|
|
2781
|
+
}
|
|
2782
|
+
return result;
|
|
2783
|
+
};
|
|
2784
|
+
var mapEntries2 = (f) => (data) => {
|
|
2785
|
+
const recKeys = Object.keys(data);
|
|
2786
|
+
if (recKeys.length === 0) {
|
|
2787
|
+
return {};
|
|
2788
|
+
}
|
|
2789
|
+
const result = {};
|
|
2790
|
+
for (let i = 0; i < recKeys.length; i++) {
|
|
2791
|
+
const k = recKeys[i];
|
|
2792
|
+
const [newKey, newVal] = f(k, data[k]);
|
|
2793
|
+
_setKey(result, newKey, newVal);
|
|
2794
|
+
}
|
|
2795
|
+
return result;
|
|
2796
|
+
};
|
|
2797
|
+
var updateIn = (path, f) => (data) => {
|
|
2798
|
+
const updateNode = (obj, pathKeys) => {
|
|
2799
|
+
const [head2, ...tail2] = pathKeys;
|
|
2800
|
+
if (tail2.length === 0) {
|
|
2801
|
+
return { ...obj, [head2]: f(obj?.[head2]) };
|
|
1370
2802
|
}
|
|
1371
|
-
|
|
2803
|
+
const child = obj && typeof obj === "object" && head2 in obj ? obj[head2] : {};
|
|
2804
|
+
return { ...obj, [head2]: updateNode(child, tail2) };
|
|
1372
2805
|
};
|
|
1373
|
-
|
|
1374
|
-
|
|
1375
|
-
|
|
1376
|
-
|
|
1377
|
-
|
|
1378
|
-
|
|
1379
|
-
|
|
1380
|
-
|
|
1381
|
-
|
|
1382
|
-
|
|
1383
|
-
|
|
1384
|
-
|
|
1385
|
-
(
|
|
1386
|
-
|
|
1387
|
-
|
|
1388
|
-
|
|
1389
|
-
|
|
1390
|
-
|
|
1391
|
-
|
|
1392
|
-
|
|
1393
|
-
|
|
1394
|
-
|
|
1395
|
-
|
|
2806
|
+
return updateNode(data, path);
|
|
2807
|
+
};
|
|
2808
|
+
var RecTo = {
|
|
2809
|
+
Dict: (data) => new globalThis.Map(Object.entries(data))
|
|
2810
|
+
};
|
|
2811
|
+
var RecNonEmpty = {
|
|
2812
|
+
singleton: (key, value) => ({ [key]: value }),
|
|
2813
|
+
from: {
|
|
2814
|
+
Record: (data) => _isNonEmpty(data) ? Maybe.make.some(data) : Maybe.make.none()
|
|
2815
|
+
},
|
|
2816
|
+
keys: (data) => keys2(data),
|
|
2817
|
+
values: (data) => values2(data),
|
|
2818
|
+
entries: (data) => entries2(data),
|
|
2819
|
+
reduce: (f) => (data) => values2(data).reduce(f),
|
|
2820
|
+
map: (f) => (data) => map3(f)(data),
|
|
2821
|
+
mapWithKey: (f) => (data) => mapWithKey2(f)(data)
|
|
2822
|
+
};
|
|
2823
|
+
var Rec = {
|
|
2824
|
+
is: RecIs,
|
|
2825
|
+
from: RecFrom,
|
|
2826
|
+
to: RecTo,
|
|
2827
|
+
map: map3,
|
|
2828
|
+
filterMap: filterMap3,
|
|
2829
|
+
mapWithKey: mapWithKey2,
|
|
2830
|
+
filter: filter3,
|
|
2831
|
+
filterWithKey: filterWithKey2,
|
|
2832
|
+
lookup: lookup2,
|
|
2833
|
+
keys: keys2,
|
|
2834
|
+
values: values2,
|
|
2835
|
+
entries: entries2,
|
|
2836
|
+
groupBy: groupBy3,
|
|
2837
|
+
pick,
|
|
2838
|
+
omit,
|
|
2839
|
+
merge,
|
|
2840
|
+
mergeWith: mergeWith2,
|
|
2841
|
+
size: size3,
|
|
2842
|
+
mapKeys: mapKeys2,
|
|
2843
|
+
compact: compact3,
|
|
2844
|
+
mapEntries: mapEntries2,
|
|
2845
|
+
updateIn,
|
|
2846
|
+
traverse: { Maybe: RecMaybe.traverse, Result: RecResult.traverse },
|
|
2847
|
+
sequence: { Maybe: RecMaybe.sequence, Result: RecResult.sequence },
|
|
2848
|
+
NonEmpty: RecNonEmpty
|
|
2849
|
+
};
|
|
1396
2850
|
|
|
1397
2851
|
// src/Data/Str.ts
|
|
1398
|
-
var
|
|
1399
|
-
|
|
1400
|
-
|
|
1401
|
-
|
|
1402
|
-
|
|
1403
|
-
|
|
1404
|
-
|
|
1405
|
-
|
|
1406
|
-
(
|
|
1407
|
-
|
|
1408
|
-
|
|
1409
|
-
|
|
1410
|
-
|
|
1411
|
-
}
|
|
1412
|
-
|
|
1413
|
-
|
|
1414
|
-
|
|
1415
|
-
|
|
1416
|
-
|
|
1417
|
-
|
|
1418
|
-
|
|
1419
|
-
|
|
1420
|
-
|
|
1421
|
-
|
|
1422
|
-
|
|
1423
|
-
|
|
1424
|
-
|
|
1425
|
-
|
|
1426
|
-
|
|
1427
|
-
|
|
1428
|
-
|
|
1429
|
-
|
|
2852
|
+
var StrNonEmptyConst = {
|
|
2853
|
+
// --- from ---
|
|
2854
|
+
from: {
|
|
2855
|
+
/**
|
|
2856
|
+
* Returns Some containing NonEmptyString if the string is not empty, None otherwise.
|
|
2857
|
+
*
|
|
2858
|
+
* @example
|
|
2859
|
+
* ```ts
|
|
2860
|
+
* Str.NonEmpty.from.String("hello"); // Some("hello")
|
|
2861
|
+
* Str.NonEmpty.from.String(""); // None
|
|
2862
|
+
* ```
|
|
2863
|
+
*/
|
|
2864
|
+
String: (s) => s.length > 0 ? Maybe.make.some(s) : Maybe.make.none()
|
|
2865
|
+
}
|
|
2866
|
+
};
|
|
2867
|
+
var isEmpty = (s) => s.length === 0;
|
|
2868
|
+
var isNonEmpty = (s) => s.length > 0;
|
|
2869
|
+
var Str = {
|
|
2870
|
+
is: {
|
|
2871
|
+
/**
|
|
2872
|
+
* Returns `true` when the string is empty.
|
|
2873
|
+
*
|
|
2874
|
+
* @example
|
|
2875
|
+
* ```ts
|
|
2876
|
+
* pipe("", Str.is.empty); // true
|
|
2877
|
+
* pipe("hi", Str.is.empty); // false
|
|
2878
|
+
* ```
|
|
2879
|
+
*/
|
|
2880
|
+
empty: isEmpty,
|
|
2881
|
+
/**
|
|
2882
|
+
* Type guard to check if a string is non-empty.
|
|
2883
|
+
*/
|
|
2884
|
+
nonEmpty: isNonEmpty
|
|
2885
|
+
},
|
|
2886
|
+
/**
|
|
2887
|
+
* Splits a string by a separator. Data-last: use in `pipe`.
|
|
2888
|
+
*
|
|
2889
|
+
* @example
|
|
2890
|
+
* ```ts
|
|
2891
|
+
* pipe("a,b,c", Str.split(",")); // ["a", "b", "c"]
|
|
2892
|
+
* ```
|
|
2893
|
+
*/
|
|
2894
|
+
split: (separator) => (s) => s.split(separator),
|
|
2895
|
+
/**
|
|
2896
|
+
* Removes leading and trailing whitespace from a string.
|
|
2897
|
+
*
|
|
2898
|
+
* @example
|
|
2899
|
+
* ```ts
|
|
2900
|
+
* pipe(" hello ", Str.trim); // "hello"
|
|
2901
|
+
* ```
|
|
2902
|
+
*/
|
|
2903
|
+
trim: (s) => s.trim(),
|
|
2904
|
+
/**
|
|
2905
|
+
* Returns `true` when the string contains the given substring.
|
|
2906
|
+
*
|
|
2907
|
+
* @example
|
|
2908
|
+
* ```ts
|
|
2909
|
+
* pipe("hello world", Str.includes("world")); // true
|
|
2910
|
+
* pipe("hello world", Str.includes("xyz")); // false
|
|
2911
|
+
* ```
|
|
2912
|
+
*/
|
|
2913
|
+
includes: (substring) => (s) => s.includes(substring),
|
|
2914
|
+
/**
|
|
2915
|
+
* Replaces the first occurrence of a pattern in a string. Data-last: use in `pipe`.
|
|
2916
|
+
*
|
|
2917
|
+
* @example
|
|
2918
|
+
* ```ts
|
|
2919
|
+
* pipe("foo foo foo", Str.replace("foo", "bar")); // "bar foo foo"
|
|
2920
|
+
* pipe("Hello World", Str.replace(/world/i, "Earth")); // "Hello Earth"
|
|
2921
|
+
* ```
|
|
2922
|
+
*/
|
|
2923
|
+
replace: (pattern, replacement) => (s) => s.replace(pattern, replacement),
|
|
2924
|
+
/**
|
|
2925
|
+
* Replaces all occurrences of a pattern in a string. Data-last: use in `pipe`.
|
|
2926
|
+
*
|
|
2927
|
+
* @example
|
|
2928
|
+
* ```ts
|
|
2929
|
+
* pipe("foo foo foo", Str.replaceAll("foo", "bar")); // "bar bar bar"
|
|
2930
|
+
* pipe("aAbBaA", Str.replaceAll(/a/gi, "x")); // "xxBBxx"
|
|
2931
|
+
* ```
|
|
2932
|
+
*/
|
|
2933
|
+
replaceAll: (pattern, replacement) => (s) => s.replaceAll(pattern, replacement),
|
|
2934
|
+
/**
|
|
2935
|
+
* Returns `true` when the string starts with the given prefix.
|
|
2936
|
+
*
|
|
2937
|
+
* @example
|
|
2938
|
+
* ```ts
|
|
2939
|
+
* pipe("hello world", Str.startsWith("hello")); // true
|
|
2940
|
+
* pipe("hello world", Str.startsWith("world")); // false
|
|
2941
|
+
* ```
|
|
2942
|
+
*/
|
|
2943
|
+
startsWith: (prefix) => (s) => s.startsWith(prefix),
|
|
2944
|
+
/**
|
|
2945
|
+
* Returns `true` when the string ends with the given suffix.
|
|
2946
|
+
*
|
|
2947
|
+
* @example
|
|
2948
|
+
* ```ts
|
|
2949
|
+
* pipe("hello world", Str.endsWith("world")); // true
|
|
2950
|
+
* pipe("hello world", Str.endsWith("hello")); // false
|
|
2951
|
+
* ```
|
|
2952
|
+
*/
|
|
2953
|
+
endsWith: (suffix) => (s) => s.endsWith(suffix),
|
|
2954
|
+
/**
|
|
2955
|
+
* Converts a string to uppercase.
|
|
2956
|
+
*
|
|
2957
|
+
* @example
|
|
2958
|
+
* ```ts
|
|
2959
|
+
* pipe("hello", Str.toUpperCase); // "HELLO"
|
|
2960
|
+
* ```
|
|
2961
|
+
*/
|
|
2962
|
+
toUpperCase: (s) => s.toUpperCase(),
|
|
2963
|
+
/**
|
|
2964
|
+
* Converts a string to lowercase.
|
|
2965
|
+
*
|
|
2966
|
+
* @example
|
|
2967
|
+
* ```ts
|
|
2968
|
+
* pipe("HELLO", Str.toLowerCase); // "hello"
|
|
2969
|
+
* ```
|
|
2970
|
+
*/
|
|
2971
|
+
toLowerCase: (s) => s.toLowerCase(),
|
|
2972
|
+
/**
|
|
2973
|
+
* Converts the first character of a string to uppercase.
|
|
2974
|
+
*
|
|
2975
|
+
* @example
|
|
2976
|
+
* ```ts
|
|
2977
|
+
* pipe("hello", Str.capitalize); // "Hello"
|
|
2978
|
+
* ```
|
|
2979
|
+
*/
|
|
2980
|
+
capitalize: (s) => s.length === 0 ? "" : s.charAt(0).toUpperCase() + s.slice(1),
|
|
2981
|
+
/**
|
|
2982
|
+
* Splits a string into lines, normalising `\r\n` and `\r` line endings.
|
|
2983
|
+
*
|
|
2984
|
+
* @example
|
|
2985
|
+
* ```ts
|
|
2986
|
+
* Str.lines("one\ntwo\nthree"); // ["one", "two", "three"]
|
|
2987
|
+
* Str.lines("a\r\nb"); // ["a", "b"]
|
|
2988
|
+
* ```
|
|
2989
|
+
*/
|
|
2990
|
+
lines: (s) => s.split(/\r?\n|\r/),
|
|
2991
|
+
/**
|
|
2992
|
+
* Splits a string into words on any whitespace boundary, filtering out empty strings.
|
|
2993
|
+
*
|
|
2994
|
+
* @example
|
|
2995
|
+
* ```ts
|
|
2996
|
+
* Str.words(" hello world "); // ["hello", "world"]
|
|
2997
|
+
* ```
|
|
2998
|
+
*/
|
|
2999
|
+
words: (s) => s.trim().split(/\s+/).filter(Boolean),
|
|
3000
|
+
/**
|
|
3001
|
+
* Returns `true` when the string is empty or contains only whitespace.
|
|
3002
|
+
*
|
|
3003
|
+
* @example
|
|
3004
|
+
* ```ts
|
|
3005
|
+
* pipe(" ", Str.isBlank); // true
|
|
3006
|
+
* pipe("hi", Str.isBlank); // false
|
|
3007
|
+
* ```
|
|
3008
|
+
*/
|
|
3009
|
+
isBlank: (s) => s.trim().length === 0,
|
|
3010
|
+
/**
|
|
3011
|
+
* Returns the length of the string.
|
|
3012
|
+
*
|
|
3013
|
+
* @example
|
|
3014
|
+
* ```ts
|
|
3015
|
+
* pipe("hello", Str.length); // 5
|
|
3016
|
+
* pipe("", Str.length); // 0
|
|
3017
|
+
* ```
|
|
3018
|
+
*/
|
|
3019
|
+
length: (s) => s.length,
|
|
3020
|
+
/**
|
|
3021
|
+
* Extracts a substring between two indices. Data-last: use in `pipe`.
|
|
3022
|
+
*
|
|
3023
|
+
* @example
|
|
3024
|
+
* ```ts
|
|
3025
|
+
* pipe("hello", Str.slice(1, 3)); // "el"
|
|
3026
|
+
* pipe("hello", Str.slice(2)); // "llo"
|
|
3027
|
+
* ```
|
|
3028
|
+
*/
|
|
3029
|
+
slice: (start, end) => (s) => s.slice(start, end),
|
|
3030
|
+
/**
|
|
3031
|
+
* Pads the start of a string to a specified length. Data-last: use in `pipe`.
|
|
3032
|
+
*
|
|
3033
|
+
* @example
|
|
3034
|
+
* ```ts
|
|
3035
|
+
* pipe("5", Str.padStart(3, "0")); // "005"
|
|
3036
|
+
* pipe("hi", Str.padStart(5)); // " hi"
|
|
3037
|
+
* ```
|
|
3038
|
+
*/
|
|
3039
|
+
padStart: (maxLength, fillString) => (s) => s.padStart(maxLength, fillString),
|
|
3040
|
+
/**
|
|
3041
|
+
* Pads the end of a string to a specified length. Data-last: use in `pipe`.
|
|
3042
|
+
*
|
|
3043
|
+
* @example
|
|
3044
|
+
* ```ts
|
|
3045
|
+
* pipe("hi", Str.padEnd(5, ".")); // "hi..."
|
|
3046
|
+
* pipe("hi", Str.padEnd(5)); // "hi "
|
|
3047
|
+
* ```
|
|
3048
|
+
*/
|
|
3049
|
+
padEnd: (maxLength, fillString) => (s) => s.padEnd(maxLength, fillString),
|
|
3050
|
+
/**
|
|
3051
|
+
* Safe number parsers that return `Maybe` instead of `NaN`.
|
|
3052
|
+
*/
|
|
3053
|
+
parse: {
|
|
1430
3054
|
/**
|
|
1431
3055
|
* Parses a string as an integer (base 10). Returns `None` if the result is `NaN`.
|
|
1432
3056
|
*
|
|
@@ -1461,16 +3085,45 @@ var Str;
|
|
|
1461
3085
|
const n = Number.parseFloat(s);
|
|
1462
3086
|
return Number.isNaN(n) ? Maybe.make.none() : Maybe.make.some(n);
|
|
1463
3087
|
}
|
|
1464
|
-
}
|
|
1465
|
-
|
|
3088
|
+
},
|
|
3089
|
+
/**
|
|
3090
|
+
* Safely parses a JSON string, returning a `Result<SyntaxError, unknown>`.
|
|
3091
|
+
*
|
|
3092
|
+
* @example
|
|
3093
|
+
* ```ts
|
|
3094
|
+
* Str.parseJson('{"a": 1}'); // Ok({ a: 1 })
|
|
3095
|
+
* Str.parseJson('invalid'); // Err(SyntaxError)
|
|
3096
|
+
* ```
|
|
3097
|
+
*/
|
|
3098
|
+
parseJson: (s) => {
|
|
1466
3099
|
try {
|
|
1467
3100
|
return Result.make.ok(JSON.parse(s));
|
|
1468
3101
|
} catch (error) {
|
|
1469
3102
|
return Result.make.err(error);
|
|
1470
3103
|
}
|
|
1471
|
-
}
|
|
1472
|
-
|
|
1473
|
-
|
|
3104
|
+
},
|
|
3105
|
+
/**
|
|
3106
|
+
* Converts the first character of a string to lower case.
|
|
3107
|
+
*
|
|
3108
|
+
* @example
|
|
3109
|
+
* ```ts
|
|
3110
|
+
* Str.uncapitalize("Hello"); // "hello"
|
|
3111
|
+
* Str.uncapitalize(""); // ""
|
|
3112
|
+
* ```
|
|
3113
|
+
*/
|
|
3114
|
+
uncapitalize: (s) => s.length === 0 ? "" : s.charAt(0).toLowerCase() + s.slice(1),
|
|
3115
|
+
/**
|
|
3116
|
+
* Truncates a string to a maximum length, appending an optional suffix (default `"..."`).
|
|
3117
|
+
* Data-last curried signature.
|
|
3118
|
+
*
|
|
3119
|
+
* @example
|
|
3120
|
+
* ```ts
|
|
3121
|
+
* pipe("Hello, world!", Str.truncate({ length: 8 })); // "Hello..."
|
|
3122
|
+
* pipe("Hello", Str.truncate({ length: 10 })); // "Hello"
|
|
3123
|
+
* pipe("Hello, world!", Str.truncate({ length: 8, suffix: "…" })); // "Hello, w…"
|
|
3124
|
+
* ```
|
|
3125
|
+
*/
|
|
3126
|
+
truncate: (options) => (s) => {
|
|
1474
3127
|
const { length: targetLength, suffix = "..." } = options;
|
|
1475
3128
|
if (s.length <= targetLength) {
|
|
1476
3129
|
return s;
|
|
@@ -1479,134 +3132,157 @@ var Str;
|
|
|
1479
3132
|
return suffix.slice(0, targetLength);
|
|
1480
3133
|
}
|
|
1481
3134
|
return s.slice(0, targetLength - suffix.length) + suffix;
|
|
1482
|
-
}
|
|
1483
|
-
|
|
1484
|
-
}
|
|
3135
|
+
},
|
|
3136
|
+
NonEmpty: StrNonEmptyConst
|
|
3137
|
+
};
|
|
1485
3138
|
|
|
1486
3139
|
// src/Data/Uniq.ts
|
|
1487
|
-
var
|
|
1488
|
-
(
|
|
1489
|
-
|
|
1490
|
-
|
|
1491
|
-
|
|
1492
|
-
|
|
1493
|
-
|
|
1494
|
-
|
|
1495
|
-
|
|
1496
|
-
|
|
1497
|
-
|
|
1498
|
-
|
|
1499
|
-
|
|
1500
|
-
|
|
1501
|
-
|
|
1502
|
-
((
|
|
1503
|
-
|
|
1504
|
-
|
|
1505
|
-
|
|
1506
|
-
|
|
1507
|
-
|
|
1508
|
-
|
|
1509
|
-
|
|
1510
|
-
|
|
1511
|
-
(
|
|
1512
|
-
|
|
1513
|
-
})(from = Uniq2.from || (Uniq2.from = {}));
|
|
1514
|
-
Uniq2.has = (item) => (s) => s.has(item);
|
|
1515
|
-
Uniq2.size = (s) => s.size;
|
|
1516
|
-
Uniq2.isSubsetOf = (other) => (s) => {
|
|
1517
|
-
const set = s;
|
|
1518
|
-
if (typeof set.isSubsetOf === "function") {
|
|
1519
|
-
return set.isSubsetOf(other);
|
|
1520
|
-
}
|
|
1521
|
-
for (const item of s) {
|
|
1522
|
-
if (!other.has(item)) {
|
|
1523
|
-
return false;
|
|
1524
|
-
}
|
|
1525
|
-
}
|
|
1526
|
-
return true;
|
|
1527
|
-
};
|
|
1528
|
-
Uniq2.insert = (item) => (s) => {
|
|
1529
|
-
if (s.has(item)) {
|
|
1530
|
-
return s;
|
|
1531
|
-
}
|
|
1532
|
-
const result = new globalThis.Set(s);
|
|
1533
|
-
result.add(item);
|
|
1534
|
-
return result;
|
|
1535
|
-
};
|
|
1536
|
-
Uniq2.remove = (item) => (s) => {
|
|
1537
|
-
if (!s.has(item)) {
|
|
1538
|
-
return s;
|
|
1539
|
-
}
|
|
1540
|
-
const result = new globalThis.Set(s);
|
|
3140
|
+
var isEmpty2 = (s) => s.size === 0;
|
|
3141
|
+
var isNonEmpty2 = (s) => s.size > 0;
|
|
3142
|
+
var empty2 = () => new globalThis.Set();
|
|
3143
|
+
var singleton2 = (item) => new globalThis.Set([item]);
|
|
3144
|
+
var fromArray = (arr) => new globalThis.Set(arr);
|
|
3145
|
+
var has2 = (item) => (s) => s.has(item);
|
|
3146
|
+
var size4 = (s) => s.size;
|
|
3147
|
+
var add = (item) => (s) => {
|
|
3148
|
+
if (s.has(item)) {
|
|
3149
|
+
return s;
|
|
3150
|
+
}
|
|
3151
|
+
const result = new globalThis.Set(s);
|
|
3152
|
+
result.add(item);
|
|
3153
|
+
return result;
|
|
3154
|
+
};
|
|
3155
|
+
var remove2 = (item) => (s) => {
|
|
3156
|
+
if (!s.has(item)) {
|
|
3157
|
+
return s;
|
|
3158
|
+
}
|
|
3159
|
+
const result = new globalThis.Set(s);
|
|
3160
|
+
result.delete(item);
|
|
3161
|
+
return result;
|
|
3162
|
+
};
|
|
3163
|
+
var toggle = (item) => (s) => {
|
|
3164
|
+
const result = new globalThis.Set(s);
|
|
3165
|
+
if (result.has(item)) {
|
|
1541
3166
|
result.delete(item);
|
|
1542
|
-
|
|
1543
|
-
|
|
1544
|
-
|
|
1545
|
-
|
|
1546
|
-
|
|
1547
|
-
|
|
1548
|
-
|
|
1549
|
-
|
|
1550
|
-
|
|
1551
|
-
|
|
1552
|
-
|
|
1553
|
-
|
|
1554
|
-
|
|
1555
|
-
|
|
1556
|
-
|
|
3167
|
+
} else {
|
|
3168
|
+
result.add(item);
|
|
3169
|
+
}
|
|
3170
|
+
return result;
|
|
3171
|
+
};
|
|
3172
|
+
var map4 = (f) => (s) => {
|
|
3173
|
+
const result = new globalThis.Set();
|
|
3174
|
+
for (const item of s) {
|
|
3175
|
+
result.add(f(item));
|
|
3176
|
+
}
|
|
3177
|
+
return result;
|
|
3178
|
+
};
|
|
3179
|
+
var filter4 = (predicate) => (s) => {
|
|
3180
|
+
const result = new globalThis.Set();
|
|
3181
|
+
for (const item of s) {
|
|
3182
|
+
if (predicate(item)) {
|
|
3183
|
+
result.add(item);
|
|
1557
3184
|
}
|
|
1558
|
-
|
|
1559
|
-
|
|
1560
|
-
|
|
1561
|
-
|
|
1562
|
-
|
|
1563
|
-
|
|
3185
|
+
}
|
|
3186
|
+
return result;
|
|
3187
|
+
};
|
|
3188
|
+
var filterMap4 = (f) => (s) => {
|
|
3189
|
+
const result = new globalThis.Set();
|
|
3190
|
+
for (const item of s) {
|
|
3191
|
+
const mb = f(item);
|
|
3192
|
+
if (mb.kind === "Some") {
|
|
3193
|
+
result.add(mb.value);
|
|
1564
3194
|
}
|
|
1565
|
-
|
|
1566
|
-
|
|
3195
|
+
}
|
|
3196
|
+
return result;
|
|
3197
|
+
};
|
|
3198
|
+
var union2 = (other) => (s) => {
|
|
3199
|
+
const set = s;
|
|
3200
|
+
if (typeof set.union === "function") {
|
|
3201
|
+
return set.union(other);
|
|
3202
|
+
}
|
|
3203
|
+
const result = new globalThis.Set(s);
|
|
3204
|
+
for (const item of other) {
|
|
3205
|
+
result.add(item);
|
|
3206
|
+
}
|
|
3207
|
+
return result;
|
|
3208
|
+
};
|
|
3209
|
+
var intersection2 = (other) => (s) => {
|
|
3210
|
+
const set = s;
|
|
3211
|
+
if (typeof set.intersection === "function") {
|
|
3212
|
+
return set.intersection(other);
|
|
3213
|
+
}
|
|
3214
|
+
const result = new globalThis.Set();
|
|
3215
|
+
for (const item of s) {
|
|
3216
|
+
if (other.has(item)) {
|
|
1567
3217
|
result.add(item);
|
|
1568
3218
|
}
|
|
1569
|
-
|
|
1570
|
-
|
|
1571
|
-
|
|
1572
|
-
|
|
1573
|
-
|
|
1574
|
-
|
|
1575
|
-
|
|
1576
|
-
|
|
1577
|
-
|
|
1578
|
-
|
|
1579
|
-
|
|
1580
|
-
|
|
1581
|
-
}
|
|
1582
|
-
return result;
|
|
1583
|
-
};
|
|
1584
|
-
Uniq2.difference = (other) => (s) => {
|
|
1585
|
-
const set = s;
|
|
1586
|
-
if (typeof set.difference === "function") {
|
|
1587
|
-
return set.difference(other);
|
|
1588
|
-
}
|
|
1589
|
-
const result = new globalThis.Set();
|
|
1590
|
-
for (const item of s) {
|
|
1591
|
-
if (!other.has(item)) {
|
|
1592
|
-
result.add(item);
|
|
1593
|
-
}
|
|
3219
|
+
}
|
|
3220
|
+
return result;
|
|
3221
|
+
};
|
|
3222
|
+
var difference2 = (other) => (s) => {
|
|
3223
|
+
const set = s;
|
|
3224
|
+
if (typeof set.difference === "function") {
|
|
3225
|
+
return set.difference(other);
|
|
3226
|
+
}
|
|
3227
|
+
const result = new globalThis.Set();
|
|
3228
|
+
for (const item of s) {
|
|
3229
|
+
if (!other.has(item)) {
|
|
3230
|
+
result.add(item);
|
|
1594
3231
|
}
|
|
1595
|
-
|
|
1596
|
-
|
|
1597
|
-
|
|
1598
|
-
|
|
1599
|
-
|
|
1600
|
-
|
|
3232
|
+
}
|
|
3233
|
+
return result;
|
|
3234
|
+
};
|
|
3235
|
+
var isSubsetOf = (other) => (s) => {
|
|
3236
|
+
const set = s;
|
|
3237
|
+
if (typeof set.isSubsetOf === "function") {
|
|
3238
|
+
return set.isSubsetOf(other);
|
|
3239
|
+
}
|
|
3240
|
+
for (const item of s) {
|
|
3241
|
+
if (!other.has(item)) {
|
|
3242
|
+
return false;
|
|
1601
3243
|
}
|
|
1602
|
-
|
|
1603
|
-
|
|
1604
|
-
|
|
1605
|
-
|
|
1606
|
-
|
|
1607
|
-
|
|
1608
|
-
|
|
1609
|
-
}
|
|
3244
|
+
}
|
|
3245
|
+
return true;
|
|
3246
|
+
};
|
|
3247
|
+
var reduce3 = (init2, f) => (s) => {
|
|
3248
|
+
let acc = init2;
|
|
3249
|
+
for (const item of s) {
|
|
3250
|
+
acc = f(acc, item);
|
|
3251
|
+
}
|
|
3252
|
+
return acc;
|
|
3253
|
+
};
|
|
3254
|
+
var toArray = (s) => [...s];
|
|
3255
|
+
var UniqNonEmptyConst = {
|
|
3256
|
+
singleton: (item) => new globalThis.Set([item]),
|
|
3257
|
+
from: {
|
|
3258
|
+
Set: (s) => s.size > 0 ? Maybe.make.some(s) : Maybe.make.none()
|
|
3259
|
+
},
|
|
3260
|
+
reduce: (f) => (s) => toArray(s).reduce(f),
|
|
3261
|
+
map: (f) => (s) => map4(f)(s),
|
|
3262
|
+
to: { Array: (s) => toArray(s) }
|
|
3263
|
+
};
|
|
3264
|
+
var Uniq = {
|
|
3265
|
+
is: { empty: isEmpty2, nonEmpty: isNonEmpty2 },
|
|
3266
|
+
empty: empty2,
|
|
3267
|
+
singleton: singleton2,
|
|
3268
|
+
from: { Array: fromArray },
|
|
3269
|
+
has: has2,
|
|
3270
|
+
size: size4,
|
|
3271
|
+
add,
|
|
3272
|
+
insert: add,
|
|
3273
|
+
remove: remove2,
|
|
3274
|
+
toggle,
|
|
3275
|
+
map: map4,
|
|
3276
|
+
filter: filter4,
|
|
3277
|
+
filterMap: filterMap4,
|
|
3278
|
+
union: union2,
|
|
3279
|
+
intersection: intersection2,
|
|
3280
|
+
difference: difference2,
|
|
3281
|
+
isSubsetOf,
|
|
3282
|
+
reduce: reduce3,
|
|
3283
|
+
to: { Array: toArray },
|
|
3284
|
+
NonEmpty: UniqNonEmptyConst
|
|
3285
|
+
};
|
|
1610
3286
|
// Annotate the CommonJS export names for ESM import in node:
|
|
1611
3287
|
0 && (module.exports = {
|
|
1612
3288
|
Arr,
|