UNPKG

fp-ts-std

Version:

The missing pseudo-standard library for fp-ts.

20 lines (19 loc) 1.26 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.parseO = exports.parse = exports.unstringify = exports.stringifyPrimitive = exports.stringifyO = exports.stringify = exports.unJSONString = void 0; const E = require("fp-ts/Either"); const O = require("fp-ts/Option"); const function_1 = require("fp-ts/function"); const string_1 = require("fp-ts/string"); const Newtype_1 = require("./Newtype"); const mkJSONString = Newtype_1.pack; exports.unJSONString = Newtype_1.unpack; const stringify = (f) => (x) => (0, function_1.pipe)(E.tryCatch(() => JSON.stringify(x), e => f(e)), E.filterOrElse(string_1.isString, () => f(TypeError("Stringify output not a string"))), E.map(mkJSONString)); exports.stringify = stringify; exports.stringifyO = (0, function_1.flow)((0, exports.stringify)(function_1.identity), O.fromEither); const stringifyPrimitive = (x) => (0, function_1.pipe)(x, JSON.stringify, mkJSONString); exports.stringifyPrimitive = stringifyPrimitive; exports.unstringify = (0, function_1.flow)(exports.unJSONString, JSON.parse); const parse = (f) => (x) => E.tryCatch(() => JSON.parse(x), e => f(e)); exports.parse = parse; exports.parseO = (0, function_1.flow)((0, exports.parse)(function_1.identity), O.fromEither);