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

v0.0.4

Published

Typescript implementation of Maybe

Downloads

2

Readme

Typescript Maybe Implementation

Description

A simple implementation of the Maybe monad in Typescript. Useful for optional values / fields, error handling etc, while keeping strong typing.

Documentation

The Maybe type is a union of Some and None. To create a Some
or None use the functions provided, Maybe.Some(value) and Maybe.None()

Example

import * as Maybe from "maybe-ts"

const some: Maybe<number> = Maybe.Some(5) // Some<number> with value 5
const none: Maybe<number> = Maybe.None() // None

We can use Maybe.isSome() and Maybe.isNone() to check and
(if using typescript) infer the type of the Maybe


if (Maybe.isSome(some)) { // some: Some<number>
    // do something
}

if (Maybe.isNone(none)) { // none: None
    // handle error or whatever
}

Checking whether a Maybe is Some or None however is usually
only done at the end of a pipeline or even not at all. Maybes
are monads and so can be mapped to other Maybes.
This is done using Maybe.map() and Maybe.andThen().

Maybe.map takes a map function of T => U for Maybe<T> while
Maybe.andThen takes a map function of T => Maybe<U> for Maybe<T>

use Maybe.map for simple transformations and Maybe.andThen for when
the function may returns a Maybe for some reason such as parsing error or
an optional property


const maybe_double = Maybe.map((x: number) => x * 2)
const doubledMaybe = maybe_double(some) // Some<number> with value 10
const doubledNone = maybe_double(none) // None


// parseInt: string => Maybe<number>
// numberToMonth: number => Maybe<string>

const parseMonth: string => Maybe<string>
    = input => {
        Maybe.andThen(numberToMonth)(parseInt(string))
    }

After chaining Maybe.map and Maybe.andThen calls (it's useful and much
cleaner to have a pipe() implementation) you can either use isNone or isSome
to decide how to continue or use Maybe.withDefault to return either the
T contained by the Maybe or a default T in the case of a None


const default = Maybe.withDefault("Not a valid month")
const value = default((parseMonth("12"))) // "December"
const none = default((parseMonth("1typo2"))) // "Not a valid month"

Maybe allows you to compose functions that may fail or deal with data
that have optional values while still keeping a declarative functional style