Created
February 28, 2020 17:50
-
-
Save rjhilgefort/8b35e63e2ef38b7eb389a7ba919fe3cb to your computer and use it in GitHub Desktop.
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 NonEmptyStrT = new t.Type<string, string, unknown>( | |
'NonEmptyStrT', | |
(input): input is string => typeof input === 'string', | |
(input, context) => | |
typeof input === 'string' && | |
pipeVal(input, trim, allPass([isString, isNotEmpty])) | |
? right(input) | |
: t.failure(input, context, 'string cannot be empty'), | |
t.identity, | |
) | |
export const ContT = (items: Array<string>) => | |
new t.Type<string, string, unknown>( | |
'ContT', | |
(u): u is string => typeof u === 'string', | |
(u, c) => | |
typeof u === 'string' && allPass([contained(items)])(u) | |
? right(u as string) | |
: t.failure(u, c, `${u} not in list [${join(',', items)}]`), | |
t.identity, | |
) | |
export const NEmptyArrT = <C extends t.Mixed>(codec: C): t.ArrayC<C> => | |
new t.ArrayType<C>( | |
'NEmptyArrT', | |
(u): u is Array<t.TypeOf<C>> => t.UnknownArray.is(u) && u.every(codec.is), | |
(u, c) => | |
isNotEmpty(u) ? right(u) : t.failure(u, c, 'Array cannot be empty'), | |
codec.encode === t.identity ? t.identity : a => a.map(codec.encode), | |
codec, | |
) |
They're all from ramda
and pipeVal
is pipe
from fp-ts
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks, please add imports.