Skip to content
This repository has been archived by the owner on May 3, 2021. It is now read-only.

Commit

Permalink
fix: go back to variadic literal for schemable
Browse files Browse the repository at this point in the history
  • Loading branch information
baetheus committed Sep 29, 2020
1 parent 95e2635 commit e38046a
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 8 deletions.
8 changes: 4 additions & 4 deletions decode.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { _ } from "./hkts.ts";
import type * as TC from "./type_classes.ts";

import { createPipeableMonad } from "./derivations.ts";
import { pipe, Refinement } from "./fns.ts";
import * as S from "./schemable.ts";
import * as G from "./guard.ts";
Expand All @@ -9,7 +10,6 @@ import * as R from "./record.ts";
import * as T from "./tree.ts";
import * as DE from "./decode_error.ts";
import * as FS from "./free_semigroup.ts";
import { createPipeableMonad } from "./derivations.ts";

/***************************************************************************************************
* @section Types
Expand Down Expand Up @@ -50,7 +50,7 @@ const traverse = R.indexedTraverse(Applicative);
**************************************************************************************************/

const mapLeft = (fef: (e: DecodeError) => DecodeError) =>
<A>(ta: Decoded<A>): Decoded<A> => E.isLeft(ta) ? E.left(ta.left) : ta;
<A>(ta: Decoded<A>): Decoded<A> => E.isLeft(ta) ? E.left(fef(ta.left)) : ta;

const bimap = <A, B>(fef: (e: DecodeError) => DecodeError, fab: (a: A) => B) =>
(ta: Decoded<A>): Decoded<B> =>
Expand Down Expand Up @@ -176,10 +176,10 @@ export const stringify: <A>(e: E.Either<DecodeError, A>) => string = E.fold(
**************************************************************************************************/

export const literal = <A extends readonly [S.Literal, ...S.Literal[]]>(
values: A,
...values: A
): Decoder<unknown, A[number]> => ({
decode: (i) =>
G.literal(values).is(i) ? E.right(i) : E.left(
G.literal(...values).is(i) ? E.right(i) : E.left(
error(i, values.map((value) => JSON.stringify(value)).join(" | ")),
),
});
Expand Down
2 changes: 1 addition & 1 deletion guard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export type TypeOf<D> = D extends Guard<any, infer A> ? A : never;
**************************************************************************************************/

export const literal = <A extends readonly [S.Literal, ...S.Literal[]]>(
values: A,
...values: A
): Guard<unknown, A[number]> => ({
is: (u: unknown): u is A[number] => values.findIndex((a) => a === u) !== -1,
});
Expand Down
8 changes: 5 additions & 3 deletions schemable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@ export type Literal = string | number | boolean | null;
**************************************************************************************************/

export type LiteralSchemable<S, L extends LS> = {
1: <A extends [Literal, ...Literal[]]>(s: A) => $<S, [A[number]]>;
2: <E, A extends [Literal, ...Literal[]]>(s: A) => $<S, [E, A[number]]>;
3: <R, E, A extends [Literal, ...Literal[]]>(s: A) => $<S, [R, E, A[number]]>;
1: <A extends [Literal, ...Literal[]]>(...s: A) => $<S, [A[number]]>;
2: <E, A extends [Literal, ...Literal[]]>(...s: A) => $<S, [E, A[number]]>;
3: <R, E, A extends [Literal, ...Literal[]]>(
...s: A
) => $<S, [R, E, A[number]]>;
}[L];

export type StringSchemable<S, L extends LS> = {
Expand Down

0 comments on commit e38046a

Please sign in to comment.