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

safe-access

v0.1.0

Published

A utility to allow for safe accessing of nested properties

Downloads

9,988

Readme

Safe Access

safe-access is a Javascript utility to allow for safe accessing of nested properties by soaking up nulls, inspired by Coffeescript's existential operator.

I know Coffeescript. Why should I use this?

Are you writing Javascript and miss doing this in Coffeescript?

very?.nested?.property?.and?.array?[0]?.func?()

Well, now you can do that without all the question marks:

var access = require('safe-access');
access(very, 'nested.property.and.array[0].func()');

I don't know Coffeescript. Why should I use this?

When accessing deeply nested properties in Javascript, it's important to guard against accessing non-existent properties in the middle of a chain. For example, obj.that.is.very.nested will throw an error if the property that doesn't exist. This is bad because it halts your program altogether (unless you have a try/catch in place). In Javascript, one way to guard against this is with long && chains:

var nestedThang = obj.that && obj.that.is && obj.that.is.very && obj.that.is.very.nested;

nestedThang will simply be undefined if that doesn't exist (instead of throwing an error). But, this gets quite messy (and annoying to type out).

The equivalent, using safe-access:

var access = require('safe-access');
var nestedThang = access(obj, 'that.is.very.nested');

safe-access can even be used to safely access arrays and call functions:

var obscenelyNested = access(obj, 'leading.to.array[0].andFunc()');

which is the equivalent of this charming thing in Javascript:

var obscenelyNested = obj &&
  obj.leading &&
  obj.leading.to &&
  obj.leading.to.array &&
  obj.leading.to.array[0] &&
  obj.leading.to.array[0].andFunc &&
  (typeof obj.leading.to.array[0].andFunc === 'function' ?
  obj.leading.to.array[0].andFunc() :
  undefined);

Calling functions with arguments

Sometimes, it's necessary to call functions with some arguments. Every argument after the accessor string (3rd argument and beyond) will be used as the arguments to each function call in the accessor string. Like this:

// equivalent of `obj.thing.add(1, 2);`
access(obj, 'thing.add()', [1, 2]);

Or maybe you have multiple function calls that receive arguments:

// equivalent of `thing.add(1, 2).toFixed(1).substr(2);`
access(obj, 'thing.add().toFixed().substr()', [1, 2], 1, 2);

Notice that if you need to pass in multiple arguments (like in the add function), you'll need to pass the arguments as an array. The caveat is if you need to pass in an array as an argument, you'll need to pass in a nested array.

An example, passing in an array as an argument:

access(window._, 'compact()', [[ false, 'boop', 'beep', '', 'meep' ]]);
// returns [ 'boop', 'beep', 'meep' ] OR undefined if window._ doesn't exist

Automatic Currying

safe-access auto-curries, which means omitting the second argument will return a function that you can use to access the same object over and over again. This can be useful if you are accessing many different nested properties on an object.

var objDot = access(obj);
objDot('nested.thing'); // obj.nested.thing
objDot('other.nested.thing'); // obj.other.nested.thing