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

nullable

v2.0.0

Published

plain implementation of Maybe (Optional, Nullable) monad

Downloads

6

Readme

nullable

plain implementation of Maybe (Optional, Nullable) monad

JavaScript Style Guide Build Status

example usage & motivation

Can you spot the error?

const userA = {name: 'Basha', pet: null}
const userB = {name: 'Cheyenne', pet: {name: 'Adonis', species: 'cat'}}
const users = [userA, userB]

users.forEach(user => {
  console.log(`Hi ${user.name} and ${user.pet.name} the ${user.pet.species}!`)
})
// * this will give us a null reference error!
// TypeError: Cannot read property 'name' of null

Instead, we could enclose the assumption that the user has a pet using Nullable:

const Nullable = require('nullable')

users.forEach(user => {
  console.log(`Hi ${user.name}${
  Nullable(user.pet)
    .map({name, species} => ` and ${name} the ${species}`)
    .orDefault('')
  }!`)
})
// => 'Hi Basha!'
// => 'Hi Cheyenne and Adonis the cat!'

We can do some other things with Nullables, too:

const foo = Nullable({a: {b: c: 5}, bar: () => 23})
console.log(
foo.get('a').get('b').get('c').map((x) => x + 5)
  .orDefault('??')
)
// => 10

foo.get('c').orDefault('??')
// => '??'

foo.call('bar').orDefault('??')
// => 23

Depending on your background, you may also know this as Maybe or Option or Nullable Structs.

Nullable works similar to Promises, in that it is a container datatype which wraps a value which may be null, or else have a value.

The idea is that all operations and transformations that are done on this value should only happen once and if the value is not null - and if it is, we can avoid explicit and error-prone manual null checking.

If you're sick of seeing errors like Uncaught TypeError: Cannot read property 'name' of null in your programs, I encourage you to use Nullables.

You may use Nullables as a functional utility library, like you might lodash, or you can model your objects with nullables so you may call methods directly.

The general pattern is:

  1. enclose the part of your program that depends on certain data always being there
  2. do any operations or transformations using Nullable#map(). Calling this always returns another Nullalbe, like how Promise#then() always returns another Promise.
  3. When you're done, get at the value by calling orDefault(val), providing a default value.

This follows the same general pattern as Promises: wrap - map - ** unwrap**

api

Nullable : (T) => Nullable<T>

Constructor, may be called with or without new

** also aliased as Nullable.of and Nullable#of (both static and instance methods) **

Nullable<T>#map : (fn: (T)=>T2) => Nullable<T2>

Map a function fn to the value if available. Returns a Nullable of the return type of fn. Analogous to Array#map

Nullable<T>#call : (String, ...Any) => Nullable<T2>

Call a method on the underlying value if available. Returns a Nullable of the return type of the method. Additional parameters are optional and work like Function#call

Nullable<T>#orDefault : (Any) => T | Any

Returns the underlying value if available, or the default argument specified.

** this method is also aliased as Nullable<T>#orElse, which may feel more intuitive to you **

additional exposed properties

It is preferable to interact with Nullable via the map, call, and orDefault methods, but the following properties are exposed. Using them, and especially changing them, can make your programs more error-prone and harder to understand and reason about. Caveat utilitor.

Nullable<T>#value : T?

Access the underlying value, if available. May be null or undefined

Nullable<T>#hasValue : Boolean

Nullable<T>#isNull : Boolean

Nullable<T>#isUndefined : Boolean

installation

$ npm install nullable

running the tests

From package root:

$ npm install
$ npm test

contributors

license

ISC. (c) MMXVI [email protected]. See LICENSE.md