Created
July 11, 2024 18:42
-
-
Save wegry/bf38ed942d13de7d549a482761273deb to your computer and use it in GitHub Desktop.
Codec to parse create dayjs instances from strings
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
import * as t from 'io-ts' | |
import dayjs, { Dayjs } from 'dayjs' | |
import { pipe } from 'fp-ts/lib/function' | |
import { chain } from 'fp-ts/lib/Either' | |
import customParseFormat from 'dayjs/plugin/customParseFormat' | |
dayjs.extend(customParseFormat) | |
export const isoDate = new t.Type<Dayjs, string, unknown>( | |
'isoDate', | |
(u): u is Dayjs => dayjs.isDayjs(u), | |
(u, c) => | |
pipe( | |
t.string.validate(u, c), | |
chain((d) => { | |
return t.success(dayjs(d, 'YYYY-MM-DD')) | |
}) | |
), | |
(u) => u.toISOString() | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment