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-navigator

v1.1.7

Published

safe navigator for javascript

Downloads

8

Readme

safe-navigator

A safe navigation operator for javascript that allows you to access nested objects without throwing an error if you try to access the key of an undefined value.

Install with npm i safe-navigator

How does it work ?

If you want to access obj.key1.key2.key3, you can import the package as safe and try safe(obj, "key1.key2.key3") and even if key1 does not contain a key called key2 and you try to access key3 in an undefined value of key2, the function will return the value of undefined without throwing an error. If you pass a third argument to safe(obj, "key1.key2.key3", []), incase the value is undefined, the fallback will be returned by safe.

If you don't pass a second parameter to the function, the object passed as first parameter will be returned.

If you dont return any parameters, the function will return undefined.

#Updates v1.1.0 : Added support for accessing array in a nested javascript object. v1.1.2 : Added a fallback as third argument to safe function

Example:

const farm = { farmer: { name: 'Steve' } };
console.log(farm.farmer.name); //will return 'Steve'
console.log(farm.farmer.name.age.old); //will throw an error

Now lets try it with safe

const safe = require('safe-navigator');
const farm = { farmer: { name: 'Steve' } };
console.log(safe(farm, 'farmer.name.age.old')); // undefined.
console.log(safe(farm, 'farmer.name')); // will return 'Steve'

Update v1.1.1: Fallback
console.log(safe(farm, 'farmer.name.age.old', "fallback")); // will return 'fallback'.

Update v1.1.0: Arrays inside nested Objects

const farm = { 
    farmer: { 
        name: 'Steve', 
        farmAnimals: ['Horse', 'Cow'], 
        animalDetails: [ {horse: { name: 'john'} }, { cow: { name: 'sam' } }],
        pairs: [['john','michelle'], ['sam','edna']]
        } 
    };

    console.log(safe(farm, 'farmer.farmAnimals[0]')); // will return 'Horse'
    console.log(safe(farm, 'farmer.animalDetails[0].horse.name')); // will return 'john';
    
    Note: 2D or multi-dmensional arrays are not natively supported in the string passed to safe.

    To work with multi-dimensional arrays, use [] after the safe fn returns the parent array.
    console.log(safe(farm, 'farmer.pairs[0]')[1]); // returns 'michelle'