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-option

v1.1.0

Published

Option / Maybe implementation for union-type

Downloads

27

Readme

union-type-option

Option / Maybe implementation for union-type. See also union-type-either.

Implemented interfaces:

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

Documentation

Like Ramda, the functions in this lib take the Opt 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 Opt = require('union-type-option')
var Some = Opt.Some
var None = Opt.None

Some :: a -> Opt a

Create an instance of Opt with a non-null value.

Some(1) // Some(1)

None :: Unit -> Opt

Create an instance of Opt with a null value.

None() // None()

none

Alias to get the instance of None()

Opt.none // None()

equals :: Opt a -> Opt b -> Boolean

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

Opt.equals(Some(1), Some(1)) //true
Opt.equals(Some({}), Some({})) //false
Opt.equals(None(), None()) //true

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

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

Opt.map(a => a + 3, Some(1)) // Some(4)

filter :: (a -> Boolean) -> Opt a -> Opt a

Run a predicate on a value in Opt, if true the Some() is returned, else None()

Opt.filter(a => a > 3, Some(2)) // None()
Opt.filter(a => a > 3, Some(4)) // Some(4)

extract :: Opt a -> a

Get the value out of an Opt. May be null!

Opt.extract(Some(1)) // 1
Opt.extract(None()) // null

of :: a -> Opt b -> a

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

Opt.of(1, None()) // Some(1)
Opt.of(1, Some(999)) // Some(1)

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

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

var validLength = str => str.length < 8 ? None() : Some(str)
var validHasCapitals = str => (/[A-Z]/).test(str) ? Some(str) : None()
var validateUsername = username => Opt.chain(validHasCapitals, validLength(username))

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

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

Opt.ap(Some(2), Some(a => a * 2)) // Some(4)

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

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

Opt.reduce((a, b) => a + b, 1, Some(2)) // Some(3)

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

Run a function on a Opt and wrap result in another Opt.

Opt.extend(Opt.map(a => a + 1), Some(1)) // Some(Some(2))