effect 3.0.1 → 3.0.3
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/dist/cjs/Match.js +6 -1
- package/dist/cjs/Match.js.map +1 -1
- package/dist/cjs/internal/matcher.js +8 -3
- package/dist/cjs/internal/matcher.js.map +1 -1
- package/dist/cjs/internal/version.js +1 -1
- package/dist/dts/Match.d.ts +35 -36
- package/dist/dts/Match.d.ts.map +1 -1
- package/dist/esm/Match.js +5 -0
- package/dist/esm/Match.js.map +1 -1
- package/dist/esm/internal/matcher.js +6 -2
- package/dist/esm/internal/matcher.js.map +1 -1
- package/dist/esm/internal/version.js +1 -1
- package/package.json +5 -2
- package/src/Match.ts +97 -109
- package/src/internal/matcher.ts +95 -68
- package/src/internal/version.ts +1 -1
package/src/internal/matcher.ts
CHANGED
|
@@ -26,7 +26,8 @@ const TypeMatcherProto: Omit<TypeMatcher<any, any, any, any>, "cases"> = {
|
|
|
26
26
|
_input: identity,
|
|
27
27
|
_filters: identity,
|
|
28
28
|
_remaining: identity,
|
|
29
|
-
_result: identity
|
|
29
|
+
_result: identity,
|
|
30
|
+
_return: identity
|
|
30
31
|
},
|
|
31
32
|
_tag: "TypeMatcher",
|
|
32
33
|
add<I, R, RA, A>(
|
|
@@ -55,7 +56,8 @@ const ValueMatcherProto: Omit<
|
|
|
55
56
|
[TypeId]: {
|
|
56
57
|
_input: identity,
|
|
57
58
|
_filters: identity,
|
|
58
|
-
_result: identity
|
|
59
|
+
_result: identity,
|
|
60
|
+
_return: identity
|
|
59
61
|
},
|
|
60
62
|
_tag: "ValueMatcher",
|
|
61
63
|
add<I, R, RA, A, Pr>(
|
|
@@ -217,7 +219,7 @@ export const valueTags = <
|
|
|
217
219
|
>(
|
|
218
220
|
fields: P
|
|
219
221
|
) => {
|
|
220
|
-
const match: any = tagsExhaustive(fields)(makeTypeMatcher([]))
|
|
222
|
+
const match: any = tagsExhaustive(fields as any)(makeTypeMatcher([]))
|
|
221
223
|
return (input: I): Unify<ReturnType<P[keyof P]>> => match(input)
|
|
222
224
|
}
|
|
223
225
|
|
|
@@ -232,27 +234,36 @@ export const typeTags = <I>() =>
|
|
|
232
234
|
>(
|
|
233
235
|
fields: P
|
|
234
236
|
) => {
|
|
235
|
-
const match: any = tagsExhaustive(fields)(makeTypeMatcher([]))
|
|
237
|
+
const match: any = tagsExhaustive(fields as any)(makeTypeMatcher([]))
|
|
236
238
|
return (input: I): Unify<ReturnType<P[keyof P]>> => match(input)
|
|
237
239
|
}
|
|
238
240
|
|
|
241
|
+
/** @internal */
|
|
242
|
+
export const withReturnType =
|
|
243
|
+
<Ret>() =>
|
|
244
|
+
<I, F, R, A, Pr, _>(self: Matcher<I, F, R, A, Pr, _>): Ret extends ([A] extends [never] ? any
|
|
245
|
+
: A) ? Matcher<I, F, R, A, Pr, Ret>
|
|
246
|
+
: "withReturnType constraint does not extend Result type" => self as any
|
|
247
|
+
|
|
239
248
|
/** @internal */
|
|
240
249
|
export const when = <
|
|
241
250
|
R,
|
|
242
251
|
const P extends Types.PatternPrimitive<R> | Types.PatternBase<R>,
|
|
243
|
-
|
|
252
|
+
Ret,
|
|
253
|
+
Fn extends (_: Types.WhenMatch<R, P>) => Ret
|
|
244
254
|
>(
|
|
245
255
|
pattern: P,
|
|
246
256
|
f: Fn
|
|
247
257
|
) =>
|
|
248
258
|
<I, F, A, Pr>(
|
|
249
|
-
self: Matcher<I, F, R, A, Pr>
|
|
259
|
+
self: Matcher<I, F, R, A, Pr, Ret>
|
|
250
260
|
): Matcher<
|
|
251
261
|
I,
|
|
252
262
|
Types.AddWithout<F, Types.PForExclude<P>>,
|
|
253
263
|
Types.ApplyFilters<I, Types.AddWithout<F, Types.PForExclude<P>>>,
|
|
254
264
|
A | ReturnType<Fn>,
|
|
255
|
-
Pr
|
|
265
|
+
Pr,
|
|
266
|
+
Ret
|
|
256
267
|
> => (self as any).add(makeWhen(makePredicate(pattern), f as any))
|
|
257
268
|
|
|
258
269
|
/** @internal */
|
|
@@ -261,18 +272,20 @@ export const whenOr = <
|
|
|
261
272
|
const P extends ReadonlyArray<
|
|
262
273
|
Types.PatternPrimitive<R> | Types.PatternBase<R>
|
|
263
274
|
>,
|
|
264
|
-
|
|
275
|
+
Ret,
|
|
276
|
+
Fn extends (_: Types.WhenMatch<R, P[number]>) => Ret
|
|
265
277
|
>(
|
|
266
278
|
...args: [...patterns: P, f: Fn]
|
|
267
279
|
) =>
|
|
268
280
|
<I, F, A, Pr>(
|
|
269
|
-
self: Matcher<I, F, R, A, Pr>
|
|
281
|
+
self: Matcher<I, F, R, A, Pr, Ret>
|
|
270
282
|
): Matcher<
|
|
271
283
|
I,
|
|
272
284
|
Types.AddWithout<F, Types.PForExclude<P[number]>>,
|
|
273
285
|
Types.ApplyFilters<I, Types.AddWithout<F, Types.PForExclude<P[number]>>>,
|
|
274
286
|
A | ReturnType<Fn>,
|
|
275
|
-
Pr
|
|
287
|
+
Pr,
|
|
288
|
+
Ret
|
|
276
289
|
> => {
|
|
277
290
|
const onMatch = args[args.length - 1] as any
|
|
278
291
|
const patterns = args.slice(0, -1) as unknown as P
|
|
@@ -285,12 +298,13 @@ export const whenAnd = <
|
|
|
285
298
|
const P extends ReadonlyArray<
|
|
286
299
|
Types.PatternPrimitive<R> | Types.PatternBase<R>
|
|
287
300
|
>,
|
|
288
|
-
|
|
301
|
+
Ret,
|
|
302
|
+
Fn extends (_: Types.WhenMatch<R, Types.ArrayToIntersection<P>>) => Ret
|
|
289
303
|
>(
|
|
290
304
|
...args: [...patterns: P, f: Fn]
|
|
291
305
|
) =>
|
|
292
306
|
<I, F, A, Pr>(
|
|
293
|
-
self: Matcher<I, F, R, A, Pr>
|
|
307
|
+
self: Matcher<I, F, R, A, Pr, Ret>
|
|
294
308
|
): Matcher<
|
|
295
309
|
I,
|
|
296
310
|
Types.AddWithout<F, Types.PForExclude<Types.ArrayToIntersection<P>>>,
|
|
@@ -307,41 +321,43 @@ export const whenAnd = <
|
|
|
307
321
|
}
|
|
308
322
|
|
|
309
323
|
/** @internal */
|
|
310
|
-
export const discriminator =
|
|
311
|
-
<
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
324
|
+
export const discriminator =
|
|
325
|
+
<D extends string>(field: D) =>
|
|
326
|
+
<R, P extends Types.Tags<D, R> & string, Ret, B extends Ret>(
|
|
327
|
+
...pattern: [
|
|
328
|
+
first: P,
|
|
329
|
+
...values: Array<P>,
|
|
330
|
+
f: (_: Extract<R, Record<D, P>>) => B
|
|
331
|
+
]
|
|
332
|
+
) => {
|
|
333
|
+
const f = pattern[pattern.length - 1]
|
|
334
|
+
const values: Array<P> = pattern.slice(0, -1) as any
|
|
335
|
+
const pred = values.length === 1
|
|
336
|
+
? (_: any) => _[field] === values[0]
|
|
337
|
+
: (_: any) => values.includes(_[field])
|
|
338
|
+
|
|
339
|
+
return <I, F, A, Pr>(
|
|
340
|
+
self: Matcher<I, F, R, A, Pr, Ret>
|
|
341
|
+
): Matcher<
|
|
342
|
+
I,
|
|
343
|
+
Types.AddWithout<F, Extract<R, Record<D, P>>>,
|
|
344
|
+
Types.ApplyFilters<I, Types.AddWithout<F, Extract<R, Record<D, P>>>>,
|
|
345
|
+
A | B,
|
|
346
|
+
Pr,
|
|
347
|
+
Ret
|
|
348
|
+
> => (self as any).add(makeWhen(pred, f as any)) as any
|
|
349
|
+
}
|
|
334
350
|
|
|
335
351
|
/** @internal */
|
|
336
352
|
export const discriminatorStartsWith = <D extends string>(field: D) =>
|
|
337
|
-
<R, P extends string, B>(
|
|
353
|
+
<R, P extends string, Ret, B extends Ret>(
|
|
338
354
|
pattern: P,
|
|
339
355
|
f: (_: Extract<R, Record<D, `${P}${string}`>>) => B
|
|
340
356
|
) => {
|
|
341
357
|
const pred = (_: any) => typeof _[field] === "string" && _[field].startsWith(pattern)
|
|
342
358
|
|
|
343
359
|
return <I, F, A, Pr>(
|
|
344
|
-
self: Matcher<I, F, R, A, Pr>
|
|
360
|
+
self: Matcher<I, F, R, A, Pr, Ret>
|
|
345
361
|
): Matcher<
|
|
346
362
|
I,
|
|
347
363
|
Types.AddWithout<F, Extract<R, Record<D, `${P}${string}`>>>,
|
|
@@ -350,7 +366,8 @@ export const discriminatorStartsWith = <D extends string>(field: D) =>
|
|
|
350
366
|
Types.AddWithout<F, Extract<R, Record<D, `${P}${string}`>>>
|
|
351
367
|
>,
|
|
352
368
|
A | B,
|
|
353
|
-
Pr
|
|
369
|
+
Pr,
|
|
370
|
+
Ret
|
|
354
371
|
> => (self as any).add(makeWhen(pred, f as any)) as any
|
|
355
372
|
}
|
|
356
373
|
|
|
@@ -358,11 +375,14 @@ export const discriminatorStartsWith = <D extends string>(field: D) =>
|
|
|
358
375
|
export const discriminators = <D extends string>(field: D) =>
|
|
359
376
|
<
|
|
360
377
|
R,
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
378
|
+
Ret,
|
|
379
|
+
P extends
|
|
380
|
+
& {
|
|
381
|
+
readonly [Tag in Types.Tags<D, R> & string]?:
|
|
382
|
+
| ((_: Extract<R, Record<D, Tag>>) => Ret)
|
|
383
|
+
| undefined
|
|
384
|
+
}
|
|
385
|
+
& { readonly [Tag in Exclude<keyof P, Types.Tags<D, R>>]: never }
|
|
366
386
|
>(
|
|
367
387
|
fields: P
|
|
368
388
|
) => {
|
|
@@ -372,13 +392,14 @@ export const discriminators = <D extends string>(field: D) =>
|
|
|
372
392
|
)
|
|
373
393
|
|
|
374
394
|
return <I, F, A, Pr>(
|
|
375
|
-
self: Matcher<I, F, R, A, Pr>
|
|
395
|
+
self: Matcher<I, F, R, A, Pr, Ret>
|
|
376
396
|
): Matcher<
|
|
377
397
|
I,
|
|
378
398
|
Types.AddWithout<F, Extract<R, Record<D, keyof P>>>,
|
|
379
399
|
Types.ApplyFilters<I, Types.AddWithout<F, Extract<R, Record<D, keyof P>>>>,
|
|
380
400
|
A | ReturnType<P[keyof P] & {}>,
|
|
381
|
-
Pr
|
|
401
|
+
Pr,
|
|
402
|
+
Ret
|
|
382
403
|
> => (self as any).add(predicate)
|
|
383
404
|
}
|
|
384
405
|
|
|
@@ -387,15 +408,18 @@ export const discriminatorsExhaustive: <D extends string>(
|
|
|
387
408
|
field: D
|
|
388
409
|
) => <
|
|
389
410
|
R,
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
411
|
+
Ret,
|
|
412
|
+
P extends
|
|
413
|
+
& {
|
|
414
|
+
readonly [Tag in Types.Tags<D, R> & string]: (
|
|
415
|
+
_: Extract<R, Record<D, Tag>>
|
|
416
|
+
) => Ret
|
|
417
|
+
}
|
|
418
|
+
& { readonly [Tag in Exclude<keyof P, Types.Tags<D, R>>]: never }
|
|
395
419
|
>(
|
|
396
420
|
fields: P
|
|
397
421
|
) => <I, F, A, Pr>(
|
|
398
|
-
self: Matcher<I, F, R, A, Pr>
|
|
422
|
+
self: Matcher<I, F, R, A, Pr, Ret>
|
|
399
423
|
) => [Pr] extends [never] ? (u: I) => Unify<A | ReturnType<P[keyof P]>>
|
|
400
424
|
: Unify<A | ReturnType<P[keyof P]>> = (field: string) => (fields: object) => {
|
|
401
425
|
const addCases = discriminators(field)(fields)
|
|
@@ -403,20 +427,21 @@ export const discriminatorsExhaustive: <D extends string>(
|
|
|
403
427
|
}
|
|
404
428
|
|
|
405
429
|
/** @internal */
|
|
406
|
-
export const tag: <R, P extends Types.Tags<"_tag", R> & string, B>(
|
|
430
|
+
export const tag: <R, P extends Types.Tags<"_tag", R> & string, Ret, B extends Ret>(
|
|
407
431
|
...pattern: [
|
|
408
432
|
first: P,
|
|
409
433
|
...values: Array<P>,
|
|
410
434
|
f: (_: Extract<R, Record<"_tag", P>>) => B
|
|
411
435
|
]
|
|
412
436
|
) => <I, F, A, Pr>(
|
|
413
|
-
self: Matcher<I, F, R, A, Pr>
|
|
437
|
+
self: Matcher<I, F, R, A, Pr, Ret>
|
|
414
438
|
) => Matcher<
|
|
415
439
|
I,
|
|
416
440
|
Types.AddWithout<F, Extract<R, Record<"_tag", P>>>,
|
|
417
441
|
Types.ApplyFilters<I, Types.AddWithout<F, Extract<R, Record<"_tag", P>>>>,
|
|
418
442
|
B | A,
|
|
419
|
-
Pr
|
|
443
|
+
Pr,
|
|
444
|
+
Ret
|
|
420
445
|
> = discriminator("_tag")
|
|
421
446
|
|
|
422
447
|
/** @internal */
|
|
@@ -432,19 +457,21 @@ export const tagsExhaustive = discriminatorsExhaustive("_tag")
|
|
|
432
457
|
export const not = <
|
|
433
458
|
R,
|
|
434
459
|
const P extends Types.PatternPrimitive<R> | Types.PatternBase<R>,
|
|
435
|
-
|
|
460
|
+
Ret,
|
|
461
|
+
Fn extends (_: Types.NotMatch<R, P>) => Ret
|
|
436
462
|
>(
|
|
437
463
|
pattern: P,
|
|
438
464
|
f: Fn
|
|
439
465
|
) =>
|
|
440
466
|
<I, F, A, Pr>(
|
|
441
|
-
self: Matcher<I, F, R, A, Pr>
|
|
467
|
+
self: Matcher<I, F, R, A, Pr, Ret>
|
|
442
468
|
): Matcher<
|
|
443
469
|
I,
|
|
444
470
|
Types.AddOnly<F, Types.WhenMatch<R, P>>,
|
|
445
471
|
Types.ApplyFilters<I, Types.AddOnly<F, Types.WhenMatch<R, P>>>,
|
|
446
472
|
A | ReturnType<Fn>,
|
|
447
|
-
Pr
|
|
473
|
+
Pr,
|
|
474
|
+
Ret
|
|
448
475
|
> => (self as any).add(makeNot(makePredicate(pattern), f as any))
|
|
449
476
|
|
|
450
477
|
/** @internal */
|
|
@@ -485,9 +512,9 @@ export const instanceOfUnsafe: <A extends abstract new(...args: any) => any>(
|
|
|
485
512
|
) => SafeRefinement<InstanceType<A>, InstanceType<A>> = instanceOf
|
|
486
513
|
|
|
487
514
|
/** @internal */
|
|
488
|
-
export const orElse = <RA, B>(f: (b: RA) => B) =>
|
|
515
|
+
export const orElse = <RA, Ret, B extends Ret>(f: (b: RA) => B) =>
|
|
489
516
|
<I, R, A, Pr>(
|
|
490
|
-
self: Matcher<I, R, RA, A, Pr>
|
|
517
|
+
self: Matcher<I, R, RA, A, Pr, Ret>
|
|
491
518
|
): [Pr] extends [never] ? (input: I) => Unify<A | B> : Unify<A | B> => {
|
|
492
519
|
const result = either(self)
|
|
493
520
|
|
|
@@ -504,16 +531,16 @@ export const orElse = <RA, B>(f: (b: RA) => B) =>
|
|
|
504
531
|
}
|
|
505
532
|
|
|
506
533
|
/** @internal */
|
|
507
|
-
export const orElseAbsurd = <I, R, RA, A, Pr>(
|
|
508
|
-
self: Matcher<I, R, RA, A, Pr>
|
|
534
|
+
export const orElseAbsurd = <I, R, RA, A, Pr, Ret>(
|
|
535
|
+
self: Matcher<I, R, RA, A, Pr, Ret>
|
|
509
536
|
): [Pr] extends [never] ? (input: I) => Unify<A> : Unify<A> =>
|
|
510
537
|
orElse(() => {
|
|
511
538
|
throw new Error("effect/Match/orElseAbsurd: absurd")
|
|
512
539
|
})(self)
|
|
513
540
|
|
|
514
541
|
/** @internal */
|
|
515
|
-
export const either: <I, F, R, A, Pr>(
|
|
516
|
-
self: Matcher<I, F, R, A, Pr>
|
|
542
|
+
export const either: <I, F, R, A, Pr, Ret>(
|
|
543
|
+
self: Matcher<I, F, R, A, Pr, Ret>
|
|
517
544
|
) => [Pr] extends [never] ? (input: I) => Either.Either<Unify<A>, R>
|
|
518
545
|
: Either.Either<Unify<A>, R> = (<I, R, RA, A>(self: Matcher<I, R, RA, A, I>) => {
|
|
519
546
|
if (self._tag === "ValueMatcher") {
|
|
@@ -547,8 +574,8 @@ export const either: <I, F, R, A, Pr>(
|
|
|
547
574
|
}) as any
|
|
548
575
|
|
|
549
576
|
/** @internal */
|
|
550
|
-
export const option: <I, F, R, A, Pr>(
|
|
551
|
-
self: Matcher<I, F, R, A, Pr>
|
|
577
|
+
export const option: <I, F, R, A, Pr, Ret>(
|
|
578
|
+
self: Matcher<I, F, R, A, Pr, Ret>
|
|
552
579
|
) => [Pr] extends [never] ? (input: I) => Option.Option<Unify<A>>
|
|
553
580
|
: Option.Option<Unify<A>> = (<I, A>(self: Matcher<I, any, any, A, I>) => {
|
|
554
581
|
const toEither = either(self)
|
|
@@ -568,8 +595,8 @@ export const option: <I, F, R, A, Pr>(
|
|
|
568
595
|
const getExhaustiveAbsurdErrorMessage = "effect/Match/exhaustive: absurd"
|
|
569
596
|
|
|
570
597
|
/** @internal */
|
|
571
|
-
export const exhaustive: <I, F, A, Pr>(
|
|
572
|
-
self: Matcher<I, F, never, A, Pr>
|
|
598
|
+
export const exhaustive: <I, F, A, Pr, Ret>(
|
|
599
|
+
self: Matcher<I, F, never, A, Pr, Ret>
|
|
573
600
|
) => [Pr] extends [never] ? (u: I) => Unify<A> : Unify<A> = (<I, F, A>(
|
|
574
601
|
self: Matcher<I, F, never, A, I>
|
|
575
602
|
) => {
|
package/src/internal/version.ts
CHANGED