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

optionull

v1.1.6

Published

An implementation for the nullable design pattern in javascript

Downloads

4

Readme

Optionull

Build Status

Basic Implementation for Optional/Nullables in Javascript

Table of Contents

Installation

npm install --save optionull

Optional Class

<!-- In the browser -->
<script src="https://unpkg.com/optionull"></script>
const Optional = require('optionull').Optional;
// or
const { Optional } = require('optionull');
// or
import { Optional } from 'optionull';

ofNullable(value)

Creates and returns a new optional with the value provided.

const opt = Optional.ofNullable('somevalue');

//this is basically an empty optional.
const nullOpt = Optional.ofNullable(null);

empty()

Creates and returns a new optional with empty value.

const emptyOpt = Optional.empty();

isDefined()

Returns true if the value is defined.

opt.isDefined(); // true
nullOpt.isDefined(); // also true

emptyOpt.isDefined(); // false
Optional.ofNullable(undefined).isDefined(); // false

isNotNull()

Returns true if the value is not null.

opt.isNotNull(); // true
nullOpt.isNotNull(); // false

emptyOpt.isNotNull(); // true
Optional.ofNullable(undefined).isNotNull(); // true

isPresent()

Returns true if the value defined and not null.

opt.isPresent(); // true
nullOpt.isPresent(); // false

emptyOpt.isPresent(); // false
Optional.ofNullable(undefined).isPresent(); // false

ifPresent(fn)

Calls the function fn if the value is present.

opt.ifPresent(val => console.log(val)); // prints 'somevalue' in the console

emptyOpt.ifPresent(val => console.log(val)); // nothing happens

ifPresentOrElse(fn, elseFn)

Calls the function fn if the value is present, otherwise calls the function elseFn.

opt.ifPresentOrElse(
    val => console.log('found'),
    () => console.log('not found')
); // prints 'found' in the console

emptyOpt.ifPresentOrElse(
    val => console.log('found'),
    () => console.log('not found')
); // prints 'not found' in the console

getOrElse(fnOrVal)

Returns the value if present. Otherwise returns the value of fnOrVal.

opt.getOrElse(() => 'someother value'); // returns 'somevalue'

emptyOpt.getOrElse('someother value'); // returns 'someother value'
emptyOpt.getOrElse(() => 'someother value'); // returns 'someother value'

get()

Returns the value of the optional. (Could be null or undefined)

opt.get(); // 'somevalue'
emptyOpt.get(); // undefined
nullOpt.get(); // null

map(fn)

If the value is present, applies the mapping function and returns its result wrapped in a new optional. Returns empty optional otherwise.

opt.map(val => val.length).get(); // 9
opt.get(); // 'somevalue' (note the original optional did not change)

emptyOpt.map(val => val.length).isPresent(); // false 

flatMap(fn)

If the value is present, applies the mapping function then if the function returns optional the optional is returned else the result is wrapped in a new optional and returned. Return empty optional if the value is not present.

opt.flatMap(val => val.length).get(); // 9
opt.flatMap(val => Optional.ofNullable(val.length)).get(); // 9

filter(fnOrVal)

Returns the value wrapped in a new optional if the value is preset and the value matches the parameter or the parameter function returns truthy value. Returns an empty optional if the value is not present or the parameter function returns a falsy value or the value did not match the paramter.

opt.filter('somevalue').get(); // somevalue
opt.filter(val => val === 'somevalue').get(); // somevalue
opt.filter('something else').isPresent(); // false
opt.filter(val => val.length === 1).isPresent(); //false