Skip to content

Commit

Permalink
Add schema support to chainCodec
Browse files Browse the repository at this point in the history
  • Loading branch information
airtoxin committed Mar 6, 2021
1 parent 69b95ba commit f345b96
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,8 @@ ThreeLengthString.decode(1); // Left("[error message]")
#### chainCodec

Utility for composing multiple Codecs.
This function accepts up to 9 Codecs.
This function accepts up to 9 Codecs.
⚠️ `schema` only uses last one codec schema.

```typescript
const ThreeDigitIntegerFromString = chainCodec(
Expand Down
8 changes: 8 additions & 0 deletions src/Codec/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,12 +108,14 @@ export function chainCodec<T1, T2, T3, T4, T5, T6, T7, T8, T9>(
return Codec.custom<T2>({
decode: (value) => c1.decode(value).chain(c2.decode),
encode: (value) => c1.encode(c2.encode(value) as any),
schema: c2.schema,
});
}
if (!c4) {
return Codec.custom<T3>({
decode: (value) => c1.decode(value).chain(c2.decode).chain(c3.decode),
encode: (value) => c1.encode(c2.encode(c3.encode(value) as any) as any),
schema: c3.schema,
});
}
if (!c5) {
Expand All @@ -122,6 +124,7 @@ export function chainCodec<T1, T2, T3, T4, T5, T6, T7, T8, T9>(
c1.decode(value).chain(c2.decode).chain(c3.decode).chain(c4.decode),
encode: (value) =>
c1.encode(c2.encode(c3.encode(c4.encode(value) as any) as any) as any),
schema: c4.schema,
});
}
if (!c6) {
Expand All @@ -139,6 +142,7 @@ export function chainCodec<T1, T2, T3, T4, T5, T6, T7, T8, T9>(
c3.encode(c4.encode(c5.encode(value) as any) as any) as any
) as any
),
schema: c5.schema,
});
}
if (!c7) {
Expand All @@ -159,6 +163,7 @@ export function chainCodec<T1, T2, T3, T4, T5, T6, T7, T8, T9>(
) as any
) as any
),
schema: c6.schema,
});
}
if (!c8) {
Expand All @@ -182,6 +187,7 @@ export function chainCodec<T1, T2, T3, T4, T5, T6, T7, T8, T9>(
) as any
) as any
),
schema: c7.schema,
});
}
if (!c9) {
Expand All @@ -208,6 +214,7 @@ export function chainCodec<T1, T2, T3, T4, T5, T6, T7, T8, T9>(
) as any
) as any
),
schema: c8.schema,
});
}

Expand Down Expand Up @@ -237,5 +244,6 @@ export function chainCodec<T1, T2, T3, T4, T5, T6, T7, T8, T9>(
) as any
) as any
),
schema: c9.schema,
});
}

0 comments on commit f345b96

Please sign in to comment.