UNPKG

fp-ts-std

Version:

The missing pseudo-standard library for fp-ts.

14 lines (13 loc) 808 B
import * as E from "fp-ts/Either"; import * as O from "fp-ts/Option"; import { flow, identity, pipe } from "fp-ts/function"; import { isString } from "fp-ts/string"; import { pack, unpack } from "./Newtype"; const mkJSONString = pack; export const unJSONString = unpack; export const stringify = (f) => (x) => pipe(E.tryCatch(() => JSON.stringify(x), e => f(e)), E.filterOrElse(isString, () => f(TypeError("Stringify output not a string"))), E.map(mkJSONString)); export const stringifyO = flow(stringify(identity), O.fromEither); export const stringifyPrimitive = (x) => pipe(x, JSON.stringify, mkJSONString); export const unstringify = flow(unJSONString, JSON.parse); export const parse = (f) => (x) => E.tryCatch(() => JSON.parse(x), e => f(e)); export const parseO = flow(parse(identity), O.fromEither);