UNPKG

@prismicio/types-internal

Version:
18 lines (16 loc) 422 B
import { chain } from "fp-ts/Either" import { pipe } from "fp-ts/function" import * as t from "io-ts" export default new t.Type<Date, Date, unknown>( "dateFromStringOrNumber", (u): u is Date => u instanceof Date, (u, c) => pipe( t.union([t.number, t.string]).validate(u, c), chain((s) => { const d = new Date(s) return isNaN(d.getTime()) ? t.failure(u, c) : t.success(d) }), ), (date) => date, )