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

optzione

v1.3.0

Published

Option monadic implementation in Typescript

Downloads

2

Readme

opztione

Monadic Option implementation in TS

Static methods

Option.none()

Returns a None representing the absence of value (undefined, null etc.).

const none = Option.none();

Option.some()

Returns a Some wrapping the provided value.

const stringOption = Option.some('hello');

Option.of()

Returns a Some if the provided value is truthy, returns None otherwise.

const stringOption = Option.of('hello');
const none = Option.of(null);

Example

const actual = Option.of('myString')
    .map((stringValue) => stringValue.length)
    .flatMap((stringLength) => Option.of(!!stringLength))
    .peek((value) => {
        console.log('hello world', value)
    })
    .get(); // will return true

Optional API

.get()

Returns the wrapped value if this is a Some instance, will throw if this is a None.

const stringOption = Option.some('hello');
const stringValue = Option.get(); // will be 'hello'

const none = Option.none();
const value = Option.get() // will throw;

.getOrElse()

Returns the wrapped value or use the provided fallback value

const none = Option.none();
const value = Option.getOrElse("hello") // will be 'hello'

.getOrElseThrow()

Returns the wrapped value or throws if this is a None or if the wrapped value is undefined, null etc.

const stringOption = Option.some('hello');
const stringValue = Option.getOrElseThrow(new MyCustomError('oops')); // won't throw, will be 'hello'

const nullableOption = Option.some(null);
nullableOption.getOrElseThrow(new MyCustomError('oops')); // will throw

const none = Option.none();
none.getOrElseThrow(new MyCustomError('oops')); // will throw

.isEmpty()

Returns true if this is a None, false if this is a Some.

const stringOption = Option.some('hello');
const stringIsEmpty = stringOption.isEmpty() // will be false

const nullableOption = Option.none();
const isEmpty = nullableOption.isEmpty() // will be true

.isDefined()

Returns false if this is a None, true if this is a Some.

const stringOption = Option.some('hello');
const stringIsEmpty = stringOption.isEmpty() // will be false

const nullableOption = Option.none();
const isEmpty = nullableOption.isEmpty() // will be true

.flatMap()

Maps the value to a new Option if this is a Some, otherwise returns None.

const stringOption = Option.some('hello');
const newOption = stringOption.flatMap((param) => {
    return Option.of({
        some: 'value'
    });
});

newOption.get() // will return Optional<{some: string}>

const nullableOption = Option.none();
const newNoneOption = nullableOption.flatMap((param) => {
    return Option.of({
        some: 'value'
    });
});

newNoneOption.get() // will return None

.map()

Maps the value and wraps it in a new Some if this is a Some, returns None.

const stringOption = Option.some('hello');
const newOption = stringOption.map((param) => {
    return {
        some: 'value'
    };
});

newOption.get() // will return Optional<{some: string}>

const nullableOption = Option.none();
const newNoneOption = nullableOption.map((param) => {
    return {
        some: 'value'
    };
});

newNoneOption.get() // will return Optional<Nullable>

.peek()

Applies an action to this value, if this option is a Some, otherwise does nothing.

const stringOption = Option.some('hello');
const newOption = stringOption.peek((value) => {
    console.log('bye') // will be executed
});

const nullableOption = Option.none();
const newNoneOption = nullableOption.peek((value) => {
    console.log('oops') // won't be executed
});