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

nevernull

v1.3.0

Published

Safe navigation of objects (avoid null pointer exceptions / avoid TypeError cannot access property 'x' of undefined)

Downloads

132

Readme

nevernull

NeverNull provides the ability to safely navigate an object tree, regardless if the object, its properties, or nested properties exist.

The browser version of nevernull can be found here

Examples

To easily try out nevernull, you can fork the codio project & box here

Avoid "Uncaught TypeError: Cannot read property x of undefined"

The function-object returned from nn guarantees safe navigation of its object tree. This allows us to avoid boilerplate value checking.

const emptyObject = {};
const nnEmptyObject = nn(emptyObject);

//this will not throw any errors.
nnEmptyObject.property.that.doesnt.exist;

Easily Gain Access to Property Values

All properties accessed on a never null function-object are functions. Executing the function property gives you the underlying value of proxied object, should the value exist.

let person = {
    name: {
        first: 'jason'
    }
};

let nnPerson = nn(person);

nnPerson.name();            // == { first: 'jason'}
nnPerson.name.first();      // == 'jason'
nnPerson.name.last();       // == undefined
nnPerson.employer.name();   // == undefined

nn(person).address.city()   // == undefined

Easily Set Property Values

Property values can be set in several ways.

Assign Property to Raw Target

To assign a property named 'first' to the 'name' target, we can simply access and assign to the raw 'name' object.

nnPerson.name().first = 'jason';

But what if name is undefined? We'd end up with an error.

Safely Assign Property Values

To avoid the above error, nevernull allows us to avoid boiler plate checking and assign value to the target only if the target is not undefined.

nnPerson.name.first = 'jason';
//since the address object is undefined, setting properties on it will have no effect, and no error will be thrown.
nnPerson.address.city = 'salt lake city';

nnPerson.name.first();      // == 'jason'
nnPerson.address.city();    // == undefined 

See the test spec for more examples

Install

Node

npm install nevernull
const nn = require('nevernull');

Existential Operator / Safe Navigation Operator / Optional Chaining

Nevernull provides you with the same functionality the Existential Operator provides in various other languages (Ruby, Groovy, Dart, CoffeeScript, etc)

e.g.

person?.name?.first; //safe navigation operator in groovy

nn(person).name.first(); //nevernull usage in js

Further Reading

Wikipedia - Save Navigation Operator

Groovy's Safe Navigation Operator

CoffeeScript's Existential Operator

Ruby's Safe Navigation Operator

tc39 Proposals

The proposal for the Existential Operator is currently in Stage 0 draft status.

Stage 0 Draft - Optional Chaining - Spec Text

Stage 0 Draft - Optional Chaining - Github

Existential Operator Strawman

ES Discussions

Optional Chaining aka Existential Operator Null Propagation

Existential Operator Null Propogation Operator

Existential Operator

Requirements

Node >= 6.0

Node 7 provides native Proxy, but 6.x will use a polyfill to emulate Proxy.

Performance

Performance is acceptable for most situations, but it should be noted that there can be a performance penalty using nevernull over traditional safeguarded access.

Detailed performance reports can be found here:

Node v7.1.0 Using Native Proxy

Node v6.0.0 Using Proxy Polyfill

Performance tests that generate the reports can be found here

Release Notes

1.0.0

Old API has been deprecated and is no longer supported. String selectors no longer are needed.

e.g.

//deprecated syntax
nn(person)('name.first');
nn(person)('name')('first');

Old API can be viewed and/or forked here

1.2.0

Thanks to inlineblock for their pull request to optimize performance by returning a cached nn(undefined) when the property value is undefined.

1.3.0

API now provides the ability to safely set property values. If the target is undefined, set has no effect, and no error is thrown.