Big News: Socket raises $60M Series C at a $1B valuation to secure software supply chains for AI-driven development.Announcement
Sign In

@morphism/utils

Package Overview
Dependencies
Maintainers
2
Versions
24
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@morphism/utils

@morphism/utils

latest
Source
npmnpm
Version
0.1.9
Version published
Maintainers
2
Created
Source

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

FAQs

Package last updated on 28 Apr 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts