0.1.9 • Published 3 years ago

@morphism/utils v0.1.9

Weekly downloads
-
License
MIT
Repository
github
Last release
3 years ago

@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()        // Initiates do notation context for `Either`
  .let('a', 10)                          // Set 'a' to 10 in the context
  .bindL('b', addTen)                    // Lazily passes in 'a' to 'addTen' & binds result to context under 'b'
  .return(({ a }) => `${a} dollars`)     // Returns a value derived from the context

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}')
0.1.8

3 years ago

0.1.9

3 years ago

0.1.7-alpha.2

4 years ago

0.1.7-alpha.0

4 years ago

0.1.7-alpha.1

4 years ago

0.1.6

4 years ago

0.1.6-alpha.1

4 years ago

0.1.6-alpha.0

4 years ago

0.1.6-alpha.5

4 years ago

0.1.6-alpha.4

4 years ago

0.1.6-alpha.3

4 years ago

0.1.6-alpha.2

4 years ago

0.1.6-alpha.6

4 years ago

0.1.2

4 years ago

0.1.1

4 years ago

0.1.4

4 years ago

0.1.3

4 years ago

0.1.5

4 years ago

0.1.0

4 years ago

0.0.2-alpha.58

4 years ago

0.0.2-alpha.57

4 years ago

0.0.2-alpha.56

4 years ago

0.0.2-alpha.55

4 years ago

0.0.2-alpha.53

4 years ago