@nunofyobiz/effect-extras 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (47) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +140 -0
  3. package/dist/index.d.ts +3703 -0
  4. package/dist/index.js +1006 -0
  5. package/dist/index.js.map +1 -0
  6. package/package.json +103 -0
  7. package/src/ArrayX/ArrayX.ts +818 -0
  8. package/src/ArrayX/index.ts +1 -0
  9. package/src/BigIntX/BigIntX.ts +35 -0
  10. package/src/BigIntX/index.ts +1 -0
  11. package/src/BooleanX/BooleanX.ts +24 -0
  12. package/src/BooleanX/index.ts +1 -0
  13. package/src/DurationX/DurationX.ts +178 -0
  14. package/src/DurationX/index.ts +1 -0
  15. package/src/EffectX/EffectX.ts +183 -0
  16. package/src/EffectX/index.ts +1 -0
  17. package/src/FormDataX/FormDataX.ts +57 -0
  18. package/src/FormDataX/index.ts +1 -0
  19. package/src/MapX/MapX.ts +54 -0
  20. package/src/MapX/index.ts +1 -0
  21. package/src/NonNullableX/NonNullableX.ts +290 -0
  22. package/src/NonNullableX/index.ts +2 -0
  23. package/src/NumberX/NumberX.ts +282 -0
  24. package/src/NumberX/index.ts +1 -0
  25. package/src/OptionX/OptionX.ts +234 -0
  26. package/src/OptionX/index.ts +1 -0
  27. package/src/OrderX/OrderX.ts +35 -0
  28. package/src/OrderX/index.ts +1 -0
  29. package/src/PredicateX/PredicateX.ts +98 -0
  30. package/src/PredicateX/index.ts +1 -0
  31. package/src/PromiseX/PromiseX.ts +32 -0
  32. package/src/PromiseX/index.ts +1 -0
  33. package/src/RecordX/RecordX.ts +478 -0
  34. package/src/RecordX/index.ts +1 -0
  35. package/src/ResultX/ResultX.ts +53 -0
  36. package/src/ResultX/index.ts +1 -0
  37. package/src/SchemaX/SchemaX.ts +324 -0
  38. package/src/SchemaX/index.ts +1 -0
  39. package/src/SetX/SetX.ts +160 -0
  40. package/src/SetX/index.ts +1 -0
  41. package/src/StringX/StringX.ts +97 -0
  42. package/src/StringX/index.ts +1 -0
  43. package/src/StructX/StructX.ts +310 -0
  44. package/src/StructX/index.ts +1 -0
  45. package/src/These/These.ts +1173 -0
  46. package/src/These/index.ts +1 -0
  47. package/src/index.ts +20 -0
package/dist/index.js ADDED
@@ -0,0 +1,1006 @@
1
+ import { pipe, Record, Option, Array, Data, Duration, DateTime, Match, Effect, Function, Schema, Predicate, Number as Number$1, SchemaGetter, Struct, Result, BigInt as BigInt$1, Cause, String as String$1 } from 'effect';
2
+ import { dual, identity, pipe as pipe$1, constUndefined } from 'effect/Function';
3
+
4
+ var __defProp = Object.defineProperty;
5
+ var __export = (target, all) => {
6
+ for (var name in all)
7
+ __defProp(target, name, { get: all[name], enumerable: true });
8
+ };
9
+
10
+ // src/ArrayX/ArrayX.ts
11
+ var ArrayX_exports = {};
12
+ __export(ArrayX_exports, {
13
+ categorize: () => categorize,
14
+ chunkBy: () => chunkBy,
15
+ compactNullable: () => compactNullable,
16
+ filterHead: () => filterHead,
17
+ filterMapNullable: () => filterMapNullable,
18
+ filterTail: () => filterTail,
19
+ findFirstWithIndex2d: () => findFirstWithIndex2d,
20
+ insertUniq: () => insertUniq,
21
+ mapRightAccum: () => mapRightAccum,
22
+ maxOption: () => maxOption,
23
+ slice: () => slice,
24
+ takeFirstWhere: () => takeFirstWhere2,
25
+ takeLastWhere: () => takeLastWhere2,
26
+ zipWithThese: () => zipWithThese
27
+ });
28
+
29
+ // src/RecordX/RecordX.ts
30
+ var RecordX_exports = {};
31
+ __export(RecordX_exports, {
32
+ collectBy: () => collectBy,
33
+ getOrThrow: () => getOrThrow,
34
+ getOrThrowWith: () => getOrThrowWith,
35
+ isNonEmptyRecord: () => isNonEmptyRecord,
36
+ keysAs: () => keysAs,
37
+ modifyIfExists: () => modifyIfExists,
38
+ takeFirstWhere: () => takeFirstWhere,
39
+ takeLast: () => takeLast,
40
+ takeLastWhere: () => takeLastWhere,
41
+ upsert: () => upsert
42
+ });
43
+ var isNonEmptyRecord = (record) => pipe(record, Predicate.not(Record.isEmptyRecord));
44
+ var modifyIfExists = dual(
45
+ 3,
46
+ (self, key, f) => pipe(
47
+ Record.modify(self, key, f),
48
+ Option.getOrElse(() => self)
49
+ )
50
+ );
51
+ var takeFirstWhere = dual(
52
+ 3,
53
+ (record, predicate, order) => ArrayX_exports.takeFirstWhere(Record.values(record), predicate, order)
54
+ );
55
+ var takeLastWhere = dual(
56
+ 3,
57
+ (record, predicate, order) => ArrayX_exports.takeLastWhere(Record.values(record), predicate, order)
58
+ );
59
+ var takeLast = dual(
60
+ 2,
61
+ (record, order) => pipe(Record.values(record), Array.sort(order), Array.last)
62
+ );
63
+ var keysAs = () => (record) => record;
64
+ var getOrThrow = dual(
65
+ 2,
66
+ (record, key) => getOrThrowWith(
67
+ record,
68
+ key,
69
+ (key2) => new Error(
70
+ `Key ${String(key2)} not found in record
71
+ Existing keys=${String(Record.keys(record))}`
72
+ )
73
+ )
74
+ );
75
+ var getOrThrowWith = dual(
76
+ 3,
77
+ (record, key, onNone) => Record.get(record, key).pipe(Option.getOrThrowWith(() => onNone(key)))
78
+ );
79
+ var upsert = dual(
80
+ 3,
81
+ (record, key, upsert2) => {
82
+ const existingValue = Record.get(record, key);
83
+ const updatedValue = upsert2(existingValue);
84
+ return Record.set(record, key, updatedValue);
85
+ }
86
+ );
87
+ var collectBy = dual(
88
+ 2,
89
+ (values, identify) => Array.reduce(
90
+ values,
91
+ {},
92
+ (accumulator, value) => Record.set(accumulator, identify(value), value)
93
+ )
94
+ );
95
+
96
+ // src/These/These.ts
97
+ var These_exports = {};
98
+ __export(These_exports, {
99
+ LeftAndRight: () => LeftAndRight,
100
+ LeftOnly: () => LeftOnly,
101
+ RightOnly: () => RightOnly,
102
+ WithLeft: () => WithLeft,
103
+ WithRight: () => WithRight,
104
+ flatMapLeft: () => flatMapLeft,
105
+ flatMapLeftEffect: () => flatMapLeftEffect,
106
+ flatMapRight: () => flatMapRight,
107
+ flatMapRightEffect: () => flatMapRightEffect,
108
+ fromNullables: () => fromNullables,
109
+ is: () => is,
110
+ leftOption: () => leftOption,
111
+ leftOrElse: () => leftOrElse,
112
+ leftOrUndefined: () => leftOrUndefined,
113
+ mapBoth: () => mapBoth,
114
+ mapBothEffect: () => mapBothEffect,
115
+ mapLeft: () => mapLeft,
116
+ mapLeftEffect: () => mapLeftEffect,
117
+ mapRight: () => mapRight,
118
+ mapRightEffect: () => mapRightEffect,
119
+ match: () => match,
120
+ matchLeft: () => matchLeft,
121
+ matchRight: () => matchRight,
122
+ optionFromNullables: () => optionFromNullables,
123
+ orElse: () => orElse,
124
+ orUndefined: () => orUndefined,
125
+ rightOption: () => rightOption,
126
+ rightOrElse: () => rightOrElse,
127
+ rightOrUndefined: () => rightOrUndefined
128
+ });
129
+ var taggedEnum = Data.taggedEnum();
130
+ var LeftOnly = taggedEnum.LeftOnly;
131
+ var RightOnly = taggedEnum.RightOnly;
132
+ var LeftAndRight = taggedEnum.LeftAndRight;
133
+ var is = taggedEnum.$is;
134
+ var match = taggedEnum.$match;
135
+ var WithLeft = ({
136
+ left,
137
+ right
138
+ }) => Predicate.isNotNullish(right) ? LeftAndRight({ left, right }) : LeftOnly({ left });
139
+ var WithRight = ({
140
+ left,
141
+ right
142
+ }) => Predicate.isNotNullish(left) ? LeftAndRight({ left, right }) : RightOnly({ right });
143
+ var optionFromNullables = ({
144
+ left,
145
+ right
146
+ }) => {
147
+ if (Predicate.isNotNullish(left) && Predicate.isNotNullish(right)) {
148
+ return Option.some(LeftAndRight({ left, right }));
149
+ }
150
+ if (Predicate.isNotNullish(left)) {
151
+ return Option.some(LeftOnly({ left }));
152
+ }
153
+ if (Predicate.isNotNullish(right)) {
154
+ return Option.some(RightOnly({ right }));
155
+ }
156
+ return Option.none();
157
+ };
158
+ var fromNullables = ({
159
+ left,
160
+ right,
161
+ orElse: orElse2 = () => {
162
+ throw new Error("Both left and right are nullable");
163
+ }
164
+ }) => pipe(optionFromNullables({ left, right }), Option.getOrElse(orElse2));
165
+ var matchLeft = ({
166
+ Left,
167
+ RightOnly: RightOnly2
168
+ }) => (these) => pipe(
169
+ these,
170
+ match({
171
+ LeftOnly: ({ left }) => Left(left),
172
+ RightOnly: ({ right }) => RightOnly2(right),
173
+ LeftAndRight: ({ left }) => Left(left)
174
+ }),
175
+ // Make Typescript happy
176
+ (a) => a
177
+ );
178
+ var matchRight = ({
179
+ LeftOnly: LeftOnly2,
180
+ Right
181
+ }) => (these) => pipe(
182
+ these,
183
+ match({
184
+ LeftOnly: ({ left }) => LeftOnly2(left),
185
+ RightOnly: ({ right }) => Right(right),
186
+ LeftAndRight: ({ right }) => Right(right)
187
+ }),
188
+ // Make Typescript happy
189
+ (a) => a
190
+ );
191
+ var orElse = ({
192
+ orElseLeft,
193
+ orElseRight
194
+ }) => match({
195
+ LeftOnly: ({ left }) => LeftAndRight({ left, right: orElseRight() }),
196
+ RightOnly: ({ right }) => LeftAndRight({ left: orElseLeft(), right }),
197
+ LeftAndRight: ({ left, right }) => LeftAndRight({ left, right })
198
+ });
199
+ var orUndefined = orElse({
200
+ orElseLeft: () => void 0,
201
+ orElseRight: () => void 0
202
+ });
203
+ var leftOrElse = (orElseReturn) => (these) => pipe(
204
+ these,
205
+ orElse({
206
+ orElseLeft: orElseReturn,
207
+ orElseRight: constUndefined
208
+ }),
209
+ Struct.get("left")
210
+ );
211
+ var leftOrUndefined = leftOrElse(() => void 0);
212
+ var rightOrElse = (orElseReturn) => (these) => pipe(
213
+ these,
214
+ orElse({
215
+ orElseLeft: constUndefined,
216
+ orElseRight: orElseReturn
217
+ }),
218
+ Struct.get("right")
219
+ );
220
+ var rightOrUndefined = rightOrElse(() => void 0);
221
+ var rightOption = (these) => pipe(
222
+ these,
223
+ matchRight({
224
+ LeftOnly: () => Option.none(),
225
+ Right: Option.some
226
+ })
227
+ );
228
+ var leftOption = (these) => pipe(
229
+ these,
230
+ matchLeft({
231
+ Left: Option.some,
232
+ RightOnly: () => Option.none()
233
+ })
234
+ );
235
+ var mapBoth = ({
236
+ mapLeft: mapLeft2,
237
+ mapRight: mapRight2
238
+ }) => match({
239
+ LeftOnly: ({ left }) => LeftOnly({ left: mapLeft2(left) }),
240
+ RightOnly: ({ right }) => RightOnly({ right: mapRight2(right) }),
241
+ LeftAndRight: ({ left, right }) => LeftAndRight({ left: mapLeft2(left), right: mapRight2(right) })
242
+ });
243
+ var mapBothEffect = ({
244
+ mapLeft: mapLeft2,
245
+ mapRight: mapRight2
246
+ }) => match({
247
+ LeftOnly: ({ left }) => pipe(
248
+ mapLeft2(left),
249
+ Effect.map((left2) => LeftOnly({ left: left2 }))
250
+ ),
251
+ RightOnly: ({ right }) => pipe(
252
+ mapRight2(right),
253
+ Effect.map((right2) => RightOnly({ right: right2 }))
254
+ ),
255
+ LeftAndRight: ({ left, right }) => pipe(
256
+ Effect.all({ left: mapLeft2(left), right: mapRight2(right) }),
257
+ Effect.map(
258
+ ({ left: left2, right: right2 }) => LeftAndRight({ left: left2, right: right2 })
259
+ )
260
+ )
261
+ });
262
+ var mapLeft = (mapLeft2) => mapBoth({ mapLeft: mapLeft2, mapRight: identity });
263
+ var flatMapLeft = (mapLeft2) => match({
264
+ LeftOnly: ({ left }) => mapLeft2(left),
265
+ RightOnly: ({ right }) => RightOnly({ right }),
266
+ LeftAndRight: ({ left }) => mapLeft2(left)
267
+ });
268
+ var mapLeftEffect = (mapLeft2) => mapBothEffect({ mapLeft: mapLeft2, mapRight: Effect.succeed });
269
+ var flatMapLeftEffect = (mapLeft2) => match({
270
+ LeftOnly: ({ left }) => mapLeft2(left),
271
+ RightOnly: ({ right }) => Effect.succeed(RightOnly({ right })),
272
+ LeftAndRight: ({ left }) => mapLeft2(left)
273
+ });
274
+ var mapRight = (mapRight2) => mapBoth({ mapLeft: identity, mapRight: mapRight2 });
275
+ var flatMapRight = (mapRight2) => match({
276
+ LeftOnly: ({ left }) => LeftOnly({ left }),
277
+ RightOnly: ({ right }) => mapRight2(right),
278
+ LeftAndRight: ({ right }) => mapRight2(right)
279
+ });
280
+ var mapRightEffect = (mapRight2) => mapBothEffect({ mapLeft: Effect.succeed, mapRight: mapRight2 });
281
+ var flatMapRightEffect = (mapRight2) => match({
282
+ LeftOnly: ({ left }) => Effect.succeed(LeftOnly({ left })),
283
+ RightOnly: ({ right }) => mapRight2(right),
284
+ LeftAndRight: ({ right }) => mapRight2(right)
285
+ });
286
+
287
+ // src/ResultX/ResultX.ts
288
+ var ResultX_exports = {};
289
+ __export(ResultX_exports, {
290
+ fromOption: () => fromOption
291
+ });
292
+ var fromOption = (option) => Option.isSome(option) ? Result.succeed(option.value) : Result.failVoid;
293
+
294
+ // src/ArrayX/ArrayX.ts
295
+ var slice = dual(
296
+ 3,
297
+ (array, start, end) => array.slice(start, end)
298
+ );
299
+ var zipWithThese = dual(
300
+ 3,
301
+ (array1, array2, f) => {
302
+ const newLength = Math.max(array1.length, array2.length);
303
+ if (newLength === 0) {
304
+ return [];
305
+ }
306
+ return Array.makeBy(newLength, (index) => {
307
+ if (index < array1.length && index < array2.length) {
308
+ return f(
309
+ These_exports.LeftAndRight({
310
+ left: array1[index],
311
+ right: array2[index]
312
+ })
313
+ );
314
+ }
315
+ if (index < array1.length) {
316
+ return f(
317
+ These_exports.LeftOnly({
318
+ left: array1[index]
319
+ })
320
+ );
321
+ }
322
+ if (index < array2.length) {
323
+ return f(
324
+ These_exports.RightOnly({
325
+ right: array2[index]
326
+ })
327
+ );
328
+ }
329
+ throw new Error(`Index ${index} is out of bounds for array1 and array2`);
330
+ });
331
+ }
332
+ );
333
+ var moveUniqWith = dual(
334
+ 2,
335
+ (inputArray, {
336
+ identify,
337
+ sourceId,
338
+ moveToBeLeftOfId
339
+ }) => {
340
+ const array = [...inputArray];
341
+ const sourceIndex = array.findIndex((item) => identify(item) === sourceId);
342
+ if (sourceIndex < 0) {
343
+ return array;
344
+ }
345
+ const sourceItem = array[sourceIndex];
346
+ const arrayWithoutSource = array.filter(
347
+ (item) => identify(item) !== sourceId
348
+ );
349
+ if (moveToBeLeftOfId === null) {
350
+ return [...arrayWithoutSource, sourceItem];
351
+ }
352
+ const destinationIndex = arrayWithoutSource.findIndex(
353
+ (item) => identify(item) === moveToBeLeftOfId
354
+ );
355
+ if (destinationIndex < 0) {
356
+ return array;
357
+ }
358
+ return [
359
+ ...slice(arrayWithoutSource, 0, destinationIndex),
360
+ sourceItem,
361
+ ...slice(arrayWithoutSource, destinationIndex, arrayWithoutSource.length)
362
+ ];
363
+ }
364
+ );
365
+ var insertUniq = dual(
366
+ 2,
367
+ (array, { item, insertToBeLeftOf }) => {
368
+ const arrayWithNewItem = pipe(
369
+ array,
370
+ Array.filter((existingItem) => existingItem !== item),
371
+ Array.append(item)
372
+ );
373
+ return moveUniqWith(arrayWithNewItem, {
374
+ identify: identity,
375
+ sourceId: item,
376
+ moveToBeLeftOfId: insertToBeLeftOf
377
+ });
378
+ }
379
+ );
380
+ var mapRightAccum = dual(
381
+ 3,
382
+ (array, initialAccumulator, f) => {
383
+ const [accumulator, result] = pipe(
384
+ array,
385
+ Array.reverse,
386
+ Array.mapAccum(initialAccumulator, f)
387
+ );
388
+ return [accumulator, Array.reverse(result)];
389
+ }
390
+ );
391
+ var maxOption = dual(
392
+ 2,
393
+ (array, order) => pipe(
394
+ // If the array is empty, there is no max
395
+ array,
396
+ Option.liftPredicate(Array.isArrayNonEmpty),
397
+ // If it is non-empty, get the max
398
+ Option.map(Array.max(order))
399
+ )
400
+ );
401
+ var takeFirstOrLastWhere = dual(
402
+ 3,
403
+ (array, predicate, takeOne) => pipe(
404
+ // Keep only the items that match
405
+ array,
406
+ Array.filter(predicate),
407
+ // If there is anything left, take one
408
+ Option.liftPredicate(Array.isArrayNonEmpty),
409
+ Option.map(takeOne)
410
+ )
411
+ );
412
+ var takeFirstWhere2 = dual(
413
+ 3,
414
+ (array, predicate, order) => takeFirstOrLastWhere(array, predicate, Array.min(order))
415
+ );
416
+ var takeLastWhere2 = dual(
417
+ 3,
418
+ (array, predicate, order) => takeFirstOrLastWhere(array, predicate, Array.max(order))
419
+ );
420
+ var categorize = (items, categorize2) => Array.reduce(
421
+ items,
422
+ // Start with an empty record of categorized items. `Record.empty()`
423
+ // returns a `NonLiteralKey<C>`-keyed record, which is structurally
424
+ // equivalent to `Partial<Record<C, A[]>>`; the cast tells TypeScript
425
+ // we'll be writing typed keys back via the reducer below.
426
+ Record.empty(),
427
+ // For each item, add it to the appropriate category
428
+ (categorizedItems, item) => RecordX_exports.upsert(
429
+ categorizedItems,
430
+ categorize2(item),
431
+ // This is the next item's category
432
+ Option.match({
433
+ // This is the first item in this category, so create a new array
434
+ onNone: () => Array.of(item),
435
+ // Append the item to the existing array
436
+ onSome: Array.append(item)
437
+ })
438
+ )
439
+ );
440
+ var compactNullable = (array) => Array.filter(array, Predicate.isNotNullish);
441
+ var filterHead = dual(2, (array, predicate) => {
442
+ const firstMatchingIndex = Array.findFirstIndex(array, predicate);
443
+ return Option.match(firstMatchingIndex, {
444
+ onSome: (index) => slice(array, index, array.length),
445
+ onNone: () => []
446
+ });
447
+ });
448
+ var filterTail = dual(2, (array, predicate) => {
449
+ const lastMatchingIndex = Array.findLastIndex(array, predicate);
450
+ return Option.match(lastMatchingIndex, {
451
+ onSome: (index) => slice(array, 0, index + 1),
452
+ onNone: () => []
453
+ });
454
+ });
455
+ var filterMapNullable = dual(
456
+ 2,
457
+ (array, f) => pipe(
458
+ array,
459
+ Array.filterMap(
460
+ (value) => pipe(f(value), Option.fromNullishOr, ResultX_exports.fromOption)
461
+ )
462
+ )
463
+ );
464
+ var findFirstWithIndex2d = dual(
465
+ 2,
466
+ (array, predicate) => Array.findFirstWithIndex(
467
+ array,
468
+ (row) => Array.findFirstWithIndex(row, predicate)
469
+ ).pipe(
470
+ Option.map(([[value, secondIndex], firstIndex]) => [
471
+ value,
472
+ firstIndex,
473
+ secondIndex
474
+ ])
475
+ )
476
+ );
477
+ var chunkBy = dual(
478
+ 3,
479
+ (array, chunk, chunkEquals) => {
480
+ if (array.length === 0) {
481
+ return [];
482
+ }
483
+ const result = [];
484
+ for (const item of array) {
485
+ const groupValue = chunk(item);
486
+ if (result.length > 0) {
487
+ const lastGroup = result.at(-1);
488
+ if (lastGroup && chunkEquals(lastGroup.group, groupValue)) {
489
+ lastGroup.values.push(item);
490
+ continue;
491
+ }
492
+ }
493
+ result.push({ group: groupValue, values: Array.of(item) });
494
+ }
495
+ return result;
496
+ }
497
+ );
498
+
499
+ // src/BigIntX/BigIntX.ts
500
+ var BigIntX_exports = {};
501
+ __export(BigIntX_exports, {
502
+ toNumberOrThrow: () => toNumberOrThrow
503
+ });
504
+ var toNumberOrThrow = (value) => BigInt$1.toNumber(value).pipe(
505
+ Option.getOrThrowWith(
506
+ () => new Error(`Value ${value} is outside safe integer range`)
507
+ )
508
+ );
509
+
510
+ // src/BooleanX/BooleanX.ts
511
+ var BooleanX_exports = {};
512
+ __export(BooleanX_exports, {
513
+ toBinary: () => toBinary
514
+ });
515
+ var toBinary = (value) => value ? 1 : 0;
516
+
517
+ // src/DurationX/DurationX.ts
518
+ var DurationX_exports = {};
519
+ __export(DurationX_exports, {
520
+ diff: () => diff,
521
+ mapAsUnit: () => mapAsUnit
522
+ });
523
+ var MICROS_PER_MILLI = 1e3;
524
+ var diff = dual(
525
+ 2,
526
+ (self, that) => Duration.millis(
527
+ // Clamp at 0 — "diff" represents elapsed time since `that`; if `that`
528
+ // is in the future relative to `self`, the elapsed time is zero
529
+ // (not negative).
530
+ Math.max(0, DateTime.toEpochMillis(self) - DateTime.toEpochMillis(that))
531
+ )
532
+ );
533
+ var toUnit = dual(
534
+ 2,
535
+ (duration, unit) => Match.value(unit).pipe(
536
+ Match.whenOr("week", "weeks", () => Duration.toWeeks(duration)),
537
+ Match.whenOr("day", "days", () => Duration.toDays(duration)),
538
+ Match.whenOr("hour", "hours", () => Duration.toHours(duration)),
539
+ Match.whenOr("minute", "minutes", () => Duration.toMinutes(duration)),
540
+ Match.whenOr("second", "seconds", () => Duration.toSeconds(duration)),
541
+ Match.whenOr("milli", "millis", () => Duration.toMillis(duration)),
542
+ Match.whenOr(
543
+ "micro",
544
+ "micros",
545
+ () => Duration.toMillis(duration) * MICROS_PER_MILLI
546
+ ),
547
+ Match.whenOr(
548
+ "nano",
549
+ "nanos",
550
+ () => pipe$1(
551
+ Duration.toNanos(duration),
552
+ Option.getOrThrowWith(
553
+ () => new Error("Duration.toNanos returned None")
554
+ ),
555
+ BigIntX_exports.toNumberOrThrow
556
+ )
557
+ ),
558
+ Match.exhaustive
559
+ )
560
+ );
561
+ var fromUnit = dual(
562
+ 2,
563
+ (value, unit) => Match.value(unit).pipe(
564
+ Match.whenOr("week", "weeks", () => Duration.weeks(value)),
565
+ Match.whenOr("day", "days", () => Duration.days(value)),
566
+ Match.whenOr("hour", "hours", () => Duration.hours(value)),
567
+ Match.whenOr("minute", "minutes", () => Duration.minutes(value)),
568
+ Match.whenOr("second", "seconds", () => Duration.seconds(value)),
569
+ Match.whenOr("milli", "millis", () => Duration.millis(value)),
570
+ Match.whenOr("micro", "micros", () => Duration.micros(BigInt(value))),
571
+ Match.whenOr("nano", "nanos", () => Duration.nanos(BigInt(value))),
572
+ Match.exhaustive
573
+ )
574
+ );
575
+ var mapAsUnit = dual(
576
+ 3,
577
+ (duration, unit, map2) => pipe$1(
578
+ duration,
579
+ // Convert to that unit
580
+ toUnit(unit),
581
+ // Truncate to the requested number of digits
582
+ map2,
583
+ // Convert back to a duration
584
+ fromUnit(unit)
585
+ )
586
+ );
587
+
588
+ // src/EffectX/EffectX.ts
589
+ var EffectX_exports = {};
590
+ __export(EffectX_exports, {
591
+ flattenOption: () => flattenOption,
592
+ fromOptionOrElse: () => fromOptionOrElse,
593
+ tryUntil: () => tryUntil
594
+ });
595
+ var USER_INSTANT_DURATION = Duration.millis(200);
596
+ var flattenOption = dual(
597
+ 2,
598
+ (effect, onNone) => Effect.flatMap(
599
+ effect,
600
+ (option) => pipe(Effect.fromOption(option), Effect.mapError(onNone))
601
+ )
602
+ );
603
+ var fromOptionOrElse = dual(
604
+ 2,
605
+ (option, onNone) => pipe(Effect.fromOption(option), Effect.mapError(onNone))
606
+ );
607
+ var tryUntil = ({
608
+ try: doTry,
609
+ until: isDone,
610
+ sleepDuration = USER_INSTANT_DURATION,
611
+ maxDuration
612
+ }) => {
613
+ const immediateValue = doTry();
614
+ if (isDone(immediateValue)) {
615
+ return Effect.succeed(immediateValue);
616
+ }
617
+ return Effect.sync(doTry).pipe(
618
+ // Sleep in between each attempt
619
+ Effect.delay(sleepDuration),
620
+ // Keep doing this until the predicate passes
621
+ Effect.repeat({ until: isDone }),
622
+ // Until a timeout occurs. In v4, `Effect.timeout` raises `TimeoutError`
623
+ // on its own — no separate `timeoutFail` overload is needed.
624
+ Effect.timeout(maxDuration),
625
+ Effect.catchTag(
626
+ "TimeoutError",
627
+ () => Effect.fail(
628
+ new Cause.TimeoutError(
629
+ `Timed out after ${Duration.format(maxDuration)} waiting for value to pass predicate`
630
+ )
631
+ )
632
+ )
633
+ );
634
+ };
635
+
636
+ // src/FormDataX/FormDataX.ts
637
+ var FormDataX_exports = {};
638
+ __export(FormDataX_exports, {
639
+ decodeSync: () => decodeSync
640
+ });
641
+ var decodeSync = Function.dual(
642
+ 2,
643
+ (formData, schema) => Schema.decodeUnknownSync(Schema.fromFormData(schema))(formData)
644
+ );
645
+
646
+ // src/MapX/MapX.ts
647
+ var MapX_exports = {};
648
+ __export(MapX_exports, {
649
+ getOrElseSetGet: () => getOrElseSetGet
650
+ });
651
+ var getOrElseSetGet = dual(3, (map2, key, fallbackIfNotFound) => {
652
+ if (!map2.has(key)) {
653
+ map2.set(key, fallbackIfNotFound());
654
+ return fallbackIfNotFound();
655
+ }
656
+ const existingValue = map2.get(key);
657
+ if (Predicate.isNullish(existingValue)) {
658
+ throw new Error(`Value is nullable: ${String(key)}`);
659
+ }
660
+ return existingValue;
661
+ });
662
+
663
+ // src/NonNullableX/NonNullableX.ts
664
+ var NonNullableX_exports = {};
665
+ __export(NonNullableX_exports, {
666
+ fromNullableOrThrow: () => fromNullableOrThrow,
667
+ lift: () => lift,
668
+ map: () => map,
669
+ match: () => match2,
670
+ nullableOrder: () => nullableOrder
671
+ });
672
+ var fromNullableOrThrow = (value, variableName) => {
673
+ if (Predicate.isNotNullish(value)) {
674
+ return value;
675
+ }
676
+ throw new Error(
677
+ `Value is nullable: ${String(value)}${Predicate.isNotNullish(variableName) ? ` (variable name: ${variableName})` : ""}`
678
+ );
679
+ };
680
+ var match2 = dual(
681
+ 2,
682
+ (value, {
683
+ whenNullable,
684
+ whenNotNullable
685
+ }) => Predicate.isNotNullish(value) ? whenNotNullable(value) : whenNullable()
686
+ );
687
+ var map = dual(
688
+ 2,
689
+ (a, map2) => {
690
+ if (Predicate.isNotNullish(a)) {
691
+ return map2(a);
692
+ }
693
+ if (Predicate.isNullish(a)) {
694
+ return a;
695
+ }
696
+ throw new Error(`Value is neither nullable nor non-nullable: ${String(a)}`);
697
+ }
698
+ );
699
+ var lift = (map2) => (a) => {
700
+ if (Predicate.isNullish(a)) {
701
+ return a;
702
+ }
703
+ return map2(a);
704
+ };
705
+ var nullableOrder = dual(
706
+ 2,
707
+ (order, behavior) => {
708
+ const { nullableSortCategory, valueSortCategory } = Match.value(
709
+ behavior
710
+ ).pipe(
711
+ Match.when("value-null", () => ({
712
+ nullableSortCategory: 1,
713
+ valueSortCategory: 0
714
+ })),
715
+ Match.when("null-value", () => ({
716
+ nullableSortCategory: 0,
717
+ valueSortCategory: 1
718
+ })),
719
+ Match.exhaustive
720
+ );
721
+ return (a, b) => {
722
+ if (Predicate.isNotNullish(a) && Predicate.isNotNullish(b)) {
723
+ return order(a, b);
724
+ }
725
+ const aCategory = Predicate.isNotNullish(a) ? valueSortCategory : nullableSortCategory;
726
+ const bCategory = Predicate.isNotNullish(b) ? valueSortCategory : nullableSortCategory;
727
+ return Number$1.sign(aCategory - bCategory);
728
+ };
729
+ }
730
+ );
731
+
732
+ // src/NumberX/NumberX.ts
733
+ var NumberX_exports = {};
734
+ __export(NumberX_exports, {
735
+ indexToExcel: () => indexToExcel,
736
+ indexToRank: () => indexToRank,
737
+ padLeftZeroes: () => padLeftZeroes,
738
+ roundToDigits: () => roundToDigits,
739
+ toFixed: () => toFixed,
740
+ unsafeLogBase: () => unsafeLogBase,
741
+ unsafeToPercentOf: () => unsafeToPercentOf
742
+ });
743
+ var logBase = dual(2, (number, base) => {
744
+ if (number <= 0) {
745
+ return Option.none();
746
+ }
747
+ if (base <= 0 || base === 1) {
748
+ return Option.none();
749
+ }
750
+ if (base < 1 && number >= 1) {
751
+ return Option.none();
752
+ }
753
+ if (base >= 1 && number < 1) {
754
+ return Option.none();
755
+ }
756
+ return Option.some(Math.log(number) / Math.log(base));
757
+ });
758
+ var unsafeLogBase = dual(
759
+ 2,
760
+ (number, base) => Option.getOrThrowWith(
761
+ logBase(number, base),
762
+ () => new Error(`Error calculating log base ${base} of ${number}`)
763
+ )
764
+ );
765
+ var toPercentOf = dual(
766
+ 2,
767
+ (numerator, total) => pipe(
768
+ Number$1.divide(numerator, total),
769
+ Option.map((ratio) => ratio * 100)
770
+ )
771
+ );
772
+ var unsafeToPercentOf = dual(
773
+ 2,
774
+ (numerator, total) => Option.getOrThrowWith(
775
+ toPercentOf(numerator, total),
776
+ () => new Error(`Division by zero when dividing ${numerator} by ${total}`)
777
+ )
778
+ );
779
+ var toFixed = dual(
780
+ 2,
781
+ (number, numberDigits) => number.toFixed(numberDigits)
782
+ );
783
+ var roundToDigits = dual(
784
+ 2,
785
+ (number, numberDigits) => Number(number.toFixed(numberDigits))
786
+ );
787
+ var padLeftZeroes = dual(
788
+ 2,
789
+ (number, numberDigits) => number.toString().padStart(numberDigits, "0")
790
+ );
791
+ var indexToRank = (index) => index + 1;
792
+ var EXCEL_COLUMNS_BASE_CHARS = [..."ABCDEFGHIJKLMNOPQRSTUVWXYZ"];
793
+ var indexToExcel = (index) => {
794
+ if (index < 0) {
795
+ return Option.none();
796
+ }
797
+ const baseChars = EXCEL_COLUMNS_BASE_CHARS;
798
+ let excel = "";
799
+ const base = baseChars.length;
800
+ do {
801
+ excel = baseChars[index % base] + excel;
802
+ index = Math.floor(index / base) - 1;
803
+ } while (index >= 0);
804
+ return Option.some(excel);
805
+ };
806
+
807
+ // src/OptionX/OptionX.ts
808
+ var OptionX_exports = {};
809
+ __export(OptionX_exports, {
810
+ fromNullableOption: () => fromNullableOption,
811
+ ifSome: () => ifSome,
812
+ inspectSome: () => inspectSome,
813
+ mapSomeOrNull: () => mapSomeOrNull,
814
+ mapSomeOrUndefined: () => mapSomeOrUndefined,
815
+ tupleOf: () => tupleOf
816
+ });
817
+ var tupleOf = dual(
818
+ 2,
819
+ (a, b) => Option.flatMap(a, (a2) => Option.map(b, (b2) => [a2, b2]))
820
+ );
821
+ var ifSome = dual(2, (self, ifSome2) => {
822
+ Option.match(self, {
823
+ onSome: (value) => {
824
+ ifSome2(value);
825
+ },
826
+ onNone: () => {
827
+ }
828
+ });
829
+ });
830
+ var inspectSome = dual(
831
+ 2,
832
+ (self, function_) => {
833
+ ifSome(self, function_);
834
+ return self;
835
+ }
836
+ );
837
+ var fromNullableOption = (nullableOption) => Predicate.isNotNullish(nullableOption) ? nullableOption : Option.none();
838
+ var mapSomeOrNull = dual(
839
+ 2,
840
+ (self, map2) => pipe(self, Option.map(map2), Option.getOrNull)
841
+ );
842
+ var mapSomeOrUndefined = dual(
843
+ 2,
844
+ (self, map2) => pipe(self, Option.map(map2), Option.getOrUndefined)
845
+ );
846
+
847
+ // src/OrderX/OrderX.ts
848
+ var OrderX_exports = {};
849
+ __export(OrderX_exports, {
850
+ rankedEnum: () => rankedEnum
851
+ });
852
+ var rankedEnum = (ranks) => (self, that) => Number$1.sign(ranks[self] - ranks[that]);
853
+
854
+ // src/PredicateX/PredicateX.ts
855
+ var PredicateX_exports = {};
856
+ __export(PredicateX_exports, {
857
+ isNonEmptyString: () => isNonEmptyString,
858
+ matchRefine: () => matchRefine
859
+ });
860
+ var matchRefine = dual(
861
+ 3,
862
+ (value, predicate, handlers) => predicate(value) ? handlers.whenTrue(value) : handlers.whenFalse()
863
+ );
864
+ function isNonEmptyString(value) {
865
+ return Predicate.isNotNullish(value) && Predicate.isString(value) && String$1.isNonEmpty(value);
866
+ }
867
+
868
+ // src/PromiseX/PromiseX.ts
869
+ var PromiseX_exports = {};
870
+ __export(PromiseX_exports, {
871
+ asVoid: () => asVoid
872
+ });
873
+ var asVoid = (promise) => promise.then(() => void 0);
874
+
875
+ // src/SchemaX/SchemaX.ts
876
+ var SchemaX_exports = {};
877
+ __export(SchemaX_exports, {
878
+ TrimmedNonEmptyString: () => TrimmedNonEmptyString,
879
+ URLSafeFilePath: () => URLSafeFilePath,
880
+ nonNegativeBigInt: () => nonNegativeBigInt,
881
+ omit: () => omit,
882
+ partial: () => partial,
883
+ pick: () => pick,
884
+ pickPartial: () => pickPartial
885
+ });
886
+ var TrimmedNonEmptyString = Schema.NonEmptyString.pipe(
887
+ Schema.decode({
888
+ decode: SchemaGetter.transform((s) => s.trim()),
889
+ encode: SchemaGetter.transform((s) => s.trim())
890
+ })
891
+ );
892
+ var URLSafeFilePath = TrimmedNonEmptyString.pipe(
893
+ Schema.decode({
894
+ decode: SchemaGetter.transform((path) => decodeURIComponent(path)),
895
+ encode: SchemaGetter.transform((path) => encodeURIComponent(path))
896
+ })
897
+ );
898
+ var clampMinBigInt = (min) => (schema) => schema.pipe(
899
+ Schema.decode({
900
+ decode: SchemaGetter.transform((value) => BigInt$1.max(value, min)),
901
+ encode: SchemaGetter.transform((value) => BigInt$1.max(value, min))
902
+ })
903
+ );
904
+ var nonNegativeBigInt = clampMinBigInt(0n);
905
+ var pick = (schema, ...keys) => schema.mapFields(
906
+ (fields) => Struct.pick(fields, keys)
907
+ );
908
+ var omit = (schema, ...keys) => schema.mapFields(
909
+ (fields) => Struct.omit(fields, keys)
910
+ );
911
+ var partial = (schema) => schema.mapFields((fields) => {
912
+ const result = {};
913
+ for (const key of Object.keys(fields)) {
914
+ result[key] = Schema.optional(fields[key]);
915
+ }
916
+ return result;
917
+ });
918
+ var pickPartial = (schema, ...keys) => partial(pick(schema, ...keys));
919
+
920
+ // src/SetX/SetX.ts
921
+ var SetX_exports = {};
922
+ __export(SetX_exports, {
923
+ add: () => add,
924
+ remove: () => remove,
925
+ safelyMutate: () => safelyMutate,
926
+ toggle: () => toggle
927
+ });
928
+ var safelyMutate = dual(2, (set, mutate) => {
929
+ const copy = new Set(set);
930
+ return mutate(copy);
931
+ });
932
+ var add = dual(
933
+ 2,
934
+ (set, value) => set.has(value) ? set : new Set(set).add(value)
935
+ );
936
+ var remove = dual(2, (set, value) => {
937
+ if (set.has(value)) {
938
+ const newSet = new Set(set);
939
+ newSet.delete(value);
940
+ return newSet;
941
+ }
942
+ return set;
943
+ });
944
+ var toggle = dual(
945
+ 2,
946
+ (set, value) => set.has(value) ? remove(set, value) : add(set, value)
947
+ );
948
+
949
+ // src/StringX/StringX.ts
950
+ var StringX_exports = {};
951
+ __export(StringX_exports, {
952
+ ensurePrepend: () => ensurePrepend,
953
+ prepend: () => prepend,
954
+ surround: () => surround
955
+ });
956
+ var prepend = dual(2, (string_, start) => `${start}${string_}`);
957
+ var surround = dual(
958
+ 3,
959
+ (string_, start, end) => `${start}${string_}${end}`
960
+ );
961
+ var ensurePrepend = dual(2, (string_, start) => {
962
+ if (string_.startsWith(start)) {
963
+ return string_;
964
+ }
965
+ return `${start}${string_}`;
966
+ });
967
+
968
+ // src/StructX/StructX.ts
969
+ var StructX_exports = {};
970
+ __export(StructX_exports, {
971
+ defined: () => defined,
972
+ filterDefined: () => filterDefined,
973
+ hasNotNullableProperty: () => hasNotNullableProperty,
974
+ pickSome: () => pickSome,
975
+ some: () => some,
976
+ truthy: () => truthy
977
+ });
978
+ var defined = (name, value) => Predicate.isUndefined(value) ? {} : Record.singleton(name, value);
979
+ var filterDefined = (record) => Object.entries(record).reduce(
980
+ (accumulator, [key, value]) => ({ ...accumulator, ...defined(key, value) }),
981
+ {}
982
+ );
983
+ var some = (name, value) => Option.match(value, {
984
+ onSome: (someValue) => Record.singleton(name, someValue),
985
+ onNone: () => ({})
986
+ });
987
+ var someSingleton = dual(
988
+ 2,
989
+ (value, name) => some(name, value)
990
+ );
991
+ function pickSome(record, key, renameKeyTo) {
992
+ return pipe(
993
+ Record.get(record, key),
994
+ Option.flatten,
995
+ someSingleton(renameKeyTo ?? key)
996
+ );
997
+ }
998
+ var truthy = (name, value) => Predicate.isTruthy(value) ? Record.singleton(name, value) : {};
999
+ var hasNotNullableProperty = dual(
1000
+ 2,
1001
+ (object, key) => Predicate.hasProperty(object, key) && Predicate.isNotNullish(object[key])
1002
+ );
1003
+
1004
+ export { ArrayX_exports as ArrayX, BigIntX_exports as BigIntX, BooleanX_exports as BooleanX, DurationX_exports as DurationX, EffectX_exports as EffectX, FormDataX_exports as FormDataX, MapX_exports as MapX, NonNullableX_exports as NonNullableX, NumberX_exports as NumberX, OptionX_exports as OptionX, OrderX_exports as OrderX, PredicateX_exports as PredicateX, PromiseX_exports as PromiseX, RecordX_exports as RecordX, ResultX_exports as ResultX, SchemaX_exports as SchemaX, SetX_exports as SetX, StringX_exports as StringX, StructX_exports as StructX, These_exports as These, fromNullableOrThrow as nn };
1005
+ //# sourceMappingURL=index.js.map
1006
+ //# sourceMappingURL=index.js.map