UNPKG

@prismicio/types-internal

Version:
34 lines (30 loc) 828 B
import { chain } from "fp-ts/Either" import { pipe } from "fp-ts/function" import * as t from "io-ts" export type TrimmedStringC = t.Type<string, string, unknown> function trimScalaStyle(s: string): string { let startAt = 0 /* eslint-disable @typescript-eslint/no-non-null-assertion */ while (startAt < s.length && s[startAt]! <= " ") { startAt++ } let endAt = s.length /* eslint-disable @typescript-eslint/no-non-null-assertion */ while (startAt < endAt && s[endAt - 1]! <= " ") { endAt-- } return s.substring(startAt, endAt) } /** A codec that validates a Boolean and convert it as a string */ export default new t.Type<string, string, unknown>( "TrimmedString", t.string.is, (u, c) => pipe( t.string.validate(u, c), chain((s) => { return t.success(trimScalaStyle(s)) }), ), (i) => i, )