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