npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

union-type-either

v1.0.0

Published

Either implementation for union-type

Downloads

3

Readme

union-type-either

Either implementation for union-type. See also union-type-option.

Implemented interfaces:

  • Setoid
  • Foldable
  • Functor
  • Apply
  • Chain
  • Applicative
  • Monad
  • Extract

Documentation

Like Ramda, the functions in this lib take the Either instance as the final argument. All functions with more than one argument are auto-curried using ramda.

This library is written in node-supported es2015 (4.0+) so if you're running in an old environment you may need to transpile to es5.

var Either = require('union-type-either')
var Right = Either.Right
var Left = Either.Left

Right :: a -> Either a

Create an instance of Either with a valid value.

Right(1) // Right(1)

Left :: a -> Either a

Create an instance of Either with a default value.

Left('Danger Will Robinson')

equals :: Either a -> Either b -> Boolean

Compare the contained value of one Either against another using ===.

Either.equals(Right(1), Right(1)) //true
Either.equals(Right({}), Right({})) //false
Either.equals(Left('Doh'), Left('Doh')) //true

map :: (a -> b) -> Either a -> Either b

Run a function on a value in an Either and return new Either with the result.

Either.map(a => a + 3, Right(1)) // Right(4)

extract :: Either a -> a

Get the value out of an Either. Could be Left or Right.

Either.extract(Right(1)) // 1
Either.extract(Left('Doh')) // 'Doh'

of :: a -> Either b -> a

Put a value in an Either. Mostly useful for higher level operations.

Either.of(1, Left('Doh')) // Right(1)
Either.of(1, Right(999)) // Right(1)

chain :: (a -> Either b) -> Either a -> Either b

Run a function that returns an Either on the value in another Either.

var validLength = str => str.length < 8 ? Left('Passwords must contain at least 8 characters') : Right(str)
var validHasCapitals = str => (/[A-Z]/).test(str) ? Right(str) : Left('Password must contain at least one capital')
var validateUsername = username => Either.chain(validHasCapitals, validLength(username))

chainLeft :: (a -> Either b) -> Either a -> Either b

Like chain but only applies to Left values.

bichain :: (a -> Either b) -> (b -> Either c) -> Either a b -> Either c

Like chain but takes two functions and applies one of them to Left or Right.

ap :: Either a -> Either (a -> b) -> Either b

Run a function inside an Either on the value in another Either

Either.ap(Right(2), Right(a => a * 2)) // Right(4)

reduce :: (b -> a -> b) -> b -> Either a -> b

Turn an option into something else by combining its right value with a seed and a reducing function.

Either.reduce((a, b) => a + b, 1, Right(2)) // Right(3)

extend :: Either a => (a -> b) -> a -> Either b

Run a function on an Either and wrap with another Either.

Either.extend(a => a.extract() + 1, Right(1)) // 2

cata :: (a -> c) -> (b -> c) -> Either a b -> c

Catamorphism. Run a function on the value of both branches of an Either

Either.cata(a => a + 1, Right(1)) // 2
Either.cata(a => a + 1, Left(1)) // 2

bimap :: (a -> b) -> (c -> d) -> Either a c -> Either b d

Run a function on the value of both branches of an Either and return an Either

Either.bimap(a => a + 1, Right(1)) // Right(2)
Either.bimap(a => a + 1, Left(1)) // Left(2)