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

@mrkutly/null-safe

v1.2.2

Published

Null Safety Utility for nested Objects and Arrays

Downloads

6

Readme

NullSafe

A null safety utility for objects and nested arrays in Javascript

This package will allow you to safely access and set properties on nested objects and arrays with throwing everyone"s favorite error: "Cannot read property ___ of undefined"

To Install

  1. install the package: npm install @mrkutly/null-safe

  2. require it in your package:

    const ns = require('@mrkutly/null-safe');

To Use

Note: in versions prior to 1.1.0, ns was just a getter function. In 1.1.0 and above, ns is an object with a get() function and a set() function.

Getting values

The ns.get() function takes in 2 arguments: the object you want to access and a string of accessors.

ns.get(object, accessors);

For example:

const butter = {
	milk: 'cow',
	colors: [['yellow'], 'white', 'cream'],
	price: {
		denomination: 'USD',
		amount: 200,
		tax: {
			state: 10,
			federal: 20,
		},
	},
	info: {
		'date-churned': 'October 1st',
	},
};

ns.get(butter, 'price.tax.state'); // = 1;
ns.get(butter, 'colors[0][0]'); // => "yellow";
ns.get(butter, 'some.null.value'); // => undefined;

The accessors you pass are just whatever accessors you would normally use in js assuming all of the values in the chain are not undefined or null. If any of the values in the chain evaluate to undefined, ns.get() will return undefined rather than throwing an error.

If using a key with a "-", do not wrap it in quotes like you normally would. Instead just do this:

ns.get(butter, 'info[date-churned]'); // => October 1st;

Setting Values

The ns.get() function takes in 3 arguments: the object you want to access, a string of accessors, and the value you want to set the last accessor key to.

const butter = {
	milk: 'cow',
	colors: [['yellow'], 'white', 'cream'],
	price: {
		denomination: 'USD',
		amount: 200,
		tax: {
			state: 10,
			federal: 20,
		},
	},
	info: {
		'date-churned': 'October 1st',
	},
};

ns.set(butter, 'price.tax.state', 'one billion dollars');
// => 'one billion dollars'
// butter.price.tax.state => "one billion dollars";
ns.set(butter, 'colors[0][0]', 'marigold');
// => "marigold";
// butter.colors[0][0] => "marigold";
ns.set(butter, 'some.null.value', 'something');
// => undefined
// => nothing happens;

The accessors you pass are just whatever accessors you would normally use in js assuming all of the values in the chain are not undefined or null. If any of the values in the chain evaluate to undefined, ns.set() will return undefined and will not have a side effect on the object.

If using a key with a "-", do not wrap it in quotes like you normally would. Instead just do this:

ns.set(butter, 'info[date-churned]', 'January 1st');
// => January 1st;
// butter.info["date-churned"] => "January 1st"

Calling Methods

The ns.call() function takes in 2 required arguments and any number of optional arguments: the object you want to call the method on, a string of accessors to the method you want to call, and (optional) any arguments you want to call the method with.

function sayPhrase(exclamation = '', extra = '') {
	return this.phrase + exclamation + extra;
}

const person = {
	name: 'mark',
	sayHello() {
		return 'hello';
	},
	sayHelloWithName(name) {
		return 'hello ' + name;
	},
	sayHelloWithThis() {
		return "hi i'm " + this.name;
	},
	birthplace: {
		city: 'Dallas',
		state: 'Texas',
		introduce() {
			return `My name is mark and I was born in ${this.city}, ${this.state}.`;
		},
	},
	favoritePhrases: [
		{
			phrase: 'I love dogs',
			sayPhrase,
			variations: [
				{
					phrase: 'I love doggos',
					sayPhrase,
				},
			],
		},
		{
			phrase: 'Pizza is great',
			sayPhrase,
			variations: [
				{
					phrase: 'L&B is the best',
					sayPhrase,
				},
			],
		},
		{
			phrase: 'Sick!',
			sayPhrase,
			variations: [
				{
					phrase: "That's so sick",
					sayPhrase,
				},
			],
		},
	],
};

ns.call(person, 'sayHelloWithThis'); // => "hi i'm mark"
ns.call(person, 'favoritePhrases[0].variations[0].sayPhrase'); // => 'I love doggos'
ns.call(person, 'favoritePhrases[1].sayPhrase', '!'); // => "Pizza is great!";
ns.call(person, 'someFakeMethod'); // => undefined

The accessors you pass are just whatever accessors you would normally use in js assuming all of the values in the chain are not undefined or null. If any of the values in the chain evaluate to undefined, ns.call() will return undefined and will not have a side effect on the object.

If using a key with a "-", do not wrap it in quotes like you normally would. Instead just do this:

ns.call(person, '[some-key]', 'some argument');