@morphism/utils
A re-export of fp-ts-contrib with some additional convenience functions/domains:
JSON: an FP wrapper around the native JSON.stringify and JSON.parse methods
Do: Haskell-inspired do-notation for Either, TaskEither, Option, and Task
Examples
Do
const addTen = ({ a }:{ a: number }) => Either.right(a + 10)
const doNotation = Do.forEither()
.let('a', 10)
.bindL('b', addTen)
.return(({ a }) => `${a} dollars`)
const result = pipe(
doNotation,
Either.getOrElse(() => `0 dollars`)
)
expect(result).toEqual('20 dollars')
JSON
Converting to JSON:
import { Either } from "@morphism/fp";
import { JSON } from "@morphism/utils";
const short = pipe(
{ someField: "someValue" },
JSON.Stringify.short,
Either.fold(
() => "{}",
(json) => json
)
)
expect(short).toEqual('{ "someField": "someValue" }')
Converting to pretty JSON:
const pretty = pipe(
{ someField: "someValue" },
JSON.Stringify.pretty,
Either.fold(
() => "{}",
(json) => json
)
)
expect(pretty).toEqual('{\n "someField": "someValue"\n}')
Converting to pretty JSON and never failing:
const result = JSON.Stringify.Always.pretty({ someField: "someValue" })
expect(result).toEqual('{\n "someField": "someValue"\n}')