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

@geronimus/option

v0.1.0

Published

Creates Some and None objects that you can use in functions that may or may not return a value.

Downloads

2

Readme

Option

Contains functions to create Some and None objects, which you can return from function calls that may or may not return a result.

The idea is to avoid returning null or undefined - whether intentionally, accidentally, or by default - and to make explicit the idea that giving no result is a legitimate possibility, which any code that calls the function must be prepared to deal with.

When the invocation should return a result, you can "wrap" it in a Some.

When there is no result, you return the None singleton object.

Both objects share this common interface:

Properties

isEmpty boolean

true if this is the None object, or a Some object that holds the value undefined or null. false if it is a Some object that holds a value.

Methods

map( transformation ) function

If called on the None object, this method always returns None, no matter what function you pass to it.

If called on a Some object, this method returns another Some object containing the result of applying the transformation function to the original Some's value.

None

None is not a function. It is a singleton object that you may return instead of a call to Some. (Some is a function.)

The None object adheres to to the Option interface described above.

As a simple object, None is not an instanceof anything. You cannot use the new keyword with None, nor does it have any meaning to call None with parentheses.

There is only one None object. As such, None === None returns true.

Some( value )

Creates an instance of a Some object.

The returned object adheres to to the Option interface described above and contains the additional property value, which references the value that the Some object holds.

You can use the keyword new to create a new Some instance, but you do not have to.

Parameters

value any

The value that you want to wrap in a Some object.

Examples

const { None, Some } = require( "@geronimus/option" );

function getFirstItem( array ) {

  if ( !Array.isArray( array ) || array.length === 0 )
    return None;
  else
    return Some( array[ 0 ] );
}

Notes

  • If you forget to provide an argument to Some, it will contain the value undefined, which seems like a contradiction. In this case, its isEmpty value will be true. We leave this possibility open ( - rather than, say, returning None when a value is not provided - ) in order not to be too opinionated about its usage.
  • These objects provide a (much) simplified implementation of a pattern popularized by the Scala programming language.