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

has-deep

v1.0.1

Published

A way to validate the properties of an object without having to walk the tree.

Downloads

4

Readme

Has Deep Build Status Slides

NPM

This module is designed to simplify the painful process of validating the deep properties of an object.

About

var a = {
  b: {
    c: {
      d: {
        e: true
      }
    }
  }
};

In JavaScript, if you try to find a property on an undefined, the script will throw an error.

This sucks.

// this throws your app
var x = a.b.c.d.e.f.g;

The most common way to solve the problem is to have repetitious blocks of if-then code to slowly validate the property path.

This also sucks, but it works.

var x = (a && a.b && a.b.c && a.b.c.d && a.b.c.d.e && a.b.c.d.e.f && a.b.c.d.e.f.g) ? a.b.c.d.e.f.g : 'winning';
console.log(x); // => winning

I'm writing this module to help make it suck less.

var has = require('has-deep');

var x = (has(a, 'b.c.d.e.f.g')) ? a.b.c.d.e.f.g : 'winning';
console.log(x); // => winning

Yeah, the result is the same, but look at how much less validation there is!

has(object, path)

  • object - Object This is the source object that you want to validate.
  • path - String This is a period-delimited string that is the property path you want to validate.
  • Returns the value at the end of the path or undefined if the path doesn't exist.

Example

var has = require('has-deep');

var a = {
  b: {
    c: {
      d: {
        e: 'winning!',
        f: [{
          g: false
        }, {
          g: true
        }]
      }
    }
  }
};

console.log(has(a, 'b.c.d.e.f.g')); // => undefined
console.log(has(a, 'b.c.d.e.f')); // => undefined
console.log(has(a, 'b.c.d.e')); // => "winning!"
console.log(has(a, 'b.c.d')); // => { e: "winning!" }
console.log(has(a, 'b.c.d.f[0].g')); // => false
console.log(has(a, 'b.c.d.f[1].g')); // => true

Install

npm install has-deep

-or-

npm install ben-bradley/has-deep

Test

npm test

-or-

mocha -R spec

Versions

  • 1.0.1 - Catch when first property is bracketed
  • 1.0.0 - Able to handle dotted properties
  • 0.0.6 - Using Object.prototype.toString.call(object) === '[object Object]' for when toString() is overwritten =(
  • 0.0.4 - Providing a non-Object variabl for evaluation will return undefined instead of Throwing
  • 0.0.3 - Refactored lib to be more functional, modified deep-array valitaion to detect falsey values, added comments
  • 0.0.2 - Added deep-array validation
  • 0.0.1 - Inital commit