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

dot-guard

v1.0.0

Published

Guard against dot property undefined errors

Downloads

10

Readme

Dot Guard CircleCI Build Status GitHub license

Safely access properites of an object via dot notation without worrying about cannot read property 'x' of undefined errors.

  • get - retrieve value of a property
  • set - set value of a property
  • has - check for existence of property
  • del - delete a property

All functions require minimum parameters of object, and propPath. (Set also requires a 'value' to set the property to).

Typical usage :

dotGuard.get({ foo: { bar: { val: 42 } } }, 'foo.bar.val'); would return 42.

dotGuard.get({ foo: { bar: { val: 42 } }, 'foo.baz.val'); would return undefined.

The propPath to the object property can be passed as a string, or an array of strings depending on requirements.

  1. 'prop1.prop2.prop3'
  2. ['prop1', 'prop2', 'prop3']

N.B. passing propPath as an array of strings, allows use of dot (.) in property keys e.g. ['prop.one', 'prop.two']

Err... don't pass an array, unless you've been decorating it with properties!

Requiring / Importing

Both formats below are supported :

  • var dotGuard = require('guard-dot'); // dotGuard.get(...)
  • import { dgGet, dgSet, dgHas, dgDel } from 'guard-dot'; // dgGet(...)

dotGuard.get / dgGet

dotGuard.get(obj, propPath[, fallBack])

Parameters :

  • obj : object you wish to retrieve value from e.g. { foo: { bar: 3 } }
  • propPath : string OR array of poperties to traverse e.g. 'foo.bar' OR ['foo', 'bar']
  • fallBack : [optional] default value to return if the value can not be retrieved from the object
import dotGuard from 'guard-dot';

dotGuard.get({ car: { make: 'Mercedes' } }, 'car.make')
//>> 'Mercedes'

dotGuard.get({ car: { make: 'Mercedes' } }, 'car.manufacturer);
//>> undefined

dotGuard.get({ car: { make: 'Mercedes' } }, 'car.model', 'Trabant')
//>> 'trabant'

dotGuard.get({ 'car.info': { make: 'Mercedes', model: 'C-class' } }, ['car.info', 'model']);
//>> 'C-class'

Returns value of fallBack, or undefined if parameters passed are invalid, or it fails.

dotGuard.set / dgSet

dotGuard.set(obj, propPath, value)

Will overwrite a pre-existing value, or create a path to a new value.

Parameters :

  • obj : object you wish to set value on e.g. { foo: { bar: 3 } }
  • propPath : string OR array of poperties to traverse e.g. 'foo.bar' OR ['foo', 'bar']
  • value : default value to return if the value can not be retrieved from the object
import { dgSet } from 'guard-dot';

let obj;

obj = {};
dgSet(obj, 'foo.bar', 42);
console.log(obj);
//>> { foo: { bar: 42 } }

obj = { prop1: {} };
dgSet(obj, ['prop1', 'prop2'], 42);
console.log(obj);
//>> { prop1: { prop2: 42 } }

obj = { key1: 0 }
dgSet(obj, 'key1', 42);
console.log(obj);
//>> { key1: 42 }

obj = 'wibble';
const result = dgSet(obj, 'key1', 'val1');
console.log(result);
//>> 'wibble'

Returns original object passed, if parameters are invalid.

dotGuard.has / dgHas

Checks for existence of a property and returns a boolean.

Can be used in conjunction with dgGet to distinguish between a non-existant property, and a property that exists with a value of undefined.

dotGuard.has(obj, propPath)

Parameters :

  • obj : object you wish to has value on e.g. { foo: { bar: 3 } }
  • propPath : string OR array of poperties to traverse e.g. 'foo.bar' OR ['foo', 'bar']
  • value : default value to return if the value can not be retrieved from the object
const dgHas = require('guard-dot').dgHas;

const obj = { foo: { bar: {} } };

dgHas(obj, 'foo.bar');
//>> true

dgHas(obj, 'foo.bar.prop');
//>> false

Returns boolean false if parameters passed are invalid, or it fails.

dotGuard.del / dgDel

dotGuard.del(obj, propPath, value)

Parameters :

  • obj : object you wish to del value on e.g. { foo: { bar: 3 } }
  • propPath : string OR array of poperties to traverse e.g. 'foo.bar' OR ['foo', 'bar']
import { dgDel } from 'guard-dot';

const obj = { prop1: { prop2: { prop3: 42 } } };

dgDel(obj, 'prop1.prop2.prop3');
console.log(obj);
//>> { prop1: { props2 {} } }

dgDel(obj, 'prop1.prop2');
console.log(obj);
//>> { prop1: {} }

const result = dgDel(obj, 'prop1.prop2');
console.log(result);
//>> { prop1: {} }
console.log(obj);
//>> { prop1: {} }

Returns original object passed, if parameters are invalid.