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

maybe.flow

v0.3.0

Published

Library for representing values that may or may not exist

Downloads

19

Readme

maybe.flow

npm travis downloads styled with prettier

This library provides means for representing values that may or may not exist. This can come handy for representing optional arguments, error handling, and records with optional fields.

Usage

Library represents values that may or may not exist via Maybe<a> type. Existing value is repsented as Just<a> and absense as Nothing:

type Maybe <a> =
  | Nothing
  | Just <a>

Given javascript / typescript semantics Nothing is just a type alias for union of primitives representing absence of value:

type Nothing = null | undefiend | void

For the same reason Just<a> is just a type alias for a:

type Just<a> = a

Chosen approach makes optional function argument & optional record fields of type a be fully compatible with Maybe<a>. There for all of the functions exposed are applicable.

This also avoids necessity to box existing values for the sake of typing.

Import

import * as Maybe from "maybe.flow"

toValue(fallback:a, maybe:Maybe<a>):a

Provide a fallback value, turning an optional value into a normal value.

Maybe.toValue(5, Maybe.just(9)) // => 9
Maybe.toValue(6, Maybe.nothing) // => 6

map(f:(input:a) => b, maybe:Maybe<a>):Maybe<b>

Transform a Maybe value with a given function:

Maybe.map(Math.sqrt, Maybe.just(9)) // => Maybe.just(3)
Maybe.map(Math.sqrt, Maybe.nothing) // => Maybe.nothing

chain(then:(input:a) => Maybe<b>, maybe:Maybe<a>):Maybe<b>

Utility to chain together two computations that may fail (return Nothing).

const makeGreeting = (name:string):string =>
  `Hello ${name}!`
 
const greet = (name:Maybe<string>):Maybe<string> =>
  Maybe.chain(makeGreeting, name)
 
greet(Maybe.nothing) // => Maybe.nothing
greet(Maybe.just('world')) // => Maybe.just('Hello world!')

and(left:Maybe<a>, right:Maybe<b>):Maybe<b>

Returns Nothing if the left Maybe is Nothing, otherwise returns the right Maybe.

Maybe.and(Maybe.just(2), Maybe.nothing) // => Maybe.nothing
Maybe.and(Maybe.nothing, Maybe.just('foo')) // => Maybe.nothing
Maybe.and(Maybe.just(2), Maybe.just('foo')) // => Maybe.just('foo')
Maybe.and(Maybe.nothing, Maybe.nothing) // => Maybe.nothing

or(left:Maybe<a>, right:Maybe<a>):Maybe<a>

Returns the left Maybe if it is a Just value, otherwise returns right Maybe.

Maybe.or(Maybe.just(2), Maybe.nothing) // => Maybe.just(2)
Maybe.or(Maybe.nothing, Maybe.just('foo')) // => Maybe.just('foo')
Maybe.or(Maybe.just(2), Maybe.just(100)) // => Maybe.just(100)
Maybe.or(Maybe.nothing, Maybe.nothing) // => Maybe.nothing

just(value:a) => Maybe<a>

Turns arbitrary value of type a into Maybe<a>. Not that in most cases you dont need to do this and value a can be passed in place for Maybe<a>. Still sometimes it's convinent to downcast Just<a> (which is alias for a) to Maybe<a> and this function does just that:

Maybe.just(4) // => 4

nothing:Maybe<*>

This is just an exported null and you can use null, or undefined or null|undefined|void|a in place for Maybe<a> but sometimes code will read and communiacte intent a lot better when you do use explicit names.

Install

yarn

yarn add --save maybe.flow

npm

npm install --save maybe.flow