@prismicio/types-internal
Version:
Prismic types for Custom Types and Prismic Data
23 lines (20 loc) • 511 B
text/typescript
import { chain } from "fp-ts/Either"
import { pipe } from "fp-ts/function"
import * as t from "io-ts"
export type IntFromNumberC = t.Type<t.Int, number, unknown>
/** A codec that succeeds if a number can be parsed to an integer */
export default new t.Type<t.Int, number, unknown>(
"IntFromNumber",
t.Int.is,
(u, c) =>
pipe(
t.number.validate(u, c),
chain((n) => {
if (t.Int.is(n)) return t.success(n)
else {
return t.success(Math.round(n) as t.Int)
}
}),
),
t.Int.encode,
)