Last active
December 5, 2021 23:47
-
-
Save tonyonodi/d6955bb8a54339f2265285d070f970c5 to your computer and use it in GitHub Desktop.
Compose io-ts Codecs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export const compose = <Name extends string, I2, A extends I2, B, I1, O1>( | |
name: Name, | |
Codec1: t.Type<A, O1, I1>, | |
Codec2: t.Type<B, A, I2> | |
): t.Type<B, O1, I1> => { | |
return new t.Type<B, O1, I1>( | |
name, | |
(u): u is B => Codec2.is(u), | |
(a, c) => | |
pipe( | |
a, | |
(a) => Codec1.validate(a, c), | |
chain((b) => Codec2.validate(b, c)) | |
), | |
(b) => pipe(b, Codec2.encode, Codec1.encode) | |
); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment