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

objdive

v0.1.4

Published

Lookup object properties deeply.

Downloads

5

Readme

objdive

Lookup object properties deeply.

API

dive(data[, path[, fallback]])

If path is false-y or missing, returns data. Otherwise, path is converted to a string and split into steps (details below) and dive() descends into deeper levels of data, using each step as a property name to lead its way.

There are two empty values in JavaScript for which property lookup will fail: null and undefined. In case the current level is one of them, dive() will instead use the fallback. If fallback is missing or undefined, it defaults to false in order to make any property lookup succeed (i.e. not crash) and yield undefined. If you want an Error thrown instead, give null (see examples below).

Path syntax

Path splitting has two modes, based on the first character in path (let's call it "p0"):

  • If p0 is a letter (A..Z, a..z), digit (0..9) or _, steps will be split by ..
  • Otherwise, steps will be split by p0, starting from the 2nd character of path.
  • In case you intend to use non-ASCII step separators, keep in mind that JavaScript uses UCS-2 characters. If path begins with a high unicode character (above U+FFFF), it will most likely be represented as a surrogate pair, so p0 will be an unpaired high surrogate character.

Usage

from test/usage.js:

var dive = require('objdive'), x = {
  '': { empty: 'key' },
  foo: 42,
  bar: {
    baz: 23,
    qux: [ 'hello', 'world', 'how', 'are', 'you?' ],
    './|': true
  },
  'bar.baz': 9999,
  'bar.qux': [ 0.1, 0.2, 0.3, 0.4 ],
  '::.@': { hi: 'hello' }
};

equal(dive(x),      x);
equal(dive(x, ''),  x);

equal(dive(x, 'foo'),         x.foo);
equal(dive(x, 'foo.bar'),     x.foo.bar);
equal(dive(x, 'bar'),         x.bar);
equal(dive(x, 'bar.baz'),     x.bar.baz);
equal(dive(x, 'bar.qux.2'),   x.bar.qux[2]);

equal(dive(x, '.bar.baz'),    x.bar.baz);
equal(dive(x, '/bar/baz'),    x.bar.baz);
equal(dive(x, ':bar:baz'),    x.bar.baz);
equal(dive(x, '|bar|qux|2'),  x.bar.qux[2]);
equal(dive(x, '/bar.baz'),    x['bar.baz']);
equal(dive(x, '/bar.qux/2'),  x['bar.qux'][2]);

equal(dive(x, '.'),           x['']);
equal(dive(x, '@'),           x['']);
equal(dive(x, '@bar@./|'),    x.bar['./|']);
equal(dive(x, '?::.@?hi'),    x['::.@'].hi);

equal(dive(x, '.no.such.key'),    undefined);
equal(dive(x, '.toString'),       undefined);
equal(dive(x, '.hasOwnProperty'), undefined);
// Prefer a crash?
fails(function () { dive(x, '.no.such.key', null); },
  /^TypeError: [\S\s]+ null\b/);

// You can also use fancier defaults:
function fancy(path) { return dive(x, path, fancy.fallbackObj); }
fancy.fallbackObj = {
  '': 'index.html',
  '?limit': 9000,
  crash: null
};

equal(fancy('/'),           x['']);
equal(fancy('/foo'),        x.foo);
equal(fancy('/foo/bar'),    undefined);
equal(fancy('/foo/bar/'),   'index.html');
equal(fancy('/foo/?limit'), undefined);
equal(fancy('/foo/?limit'), x.foo['?limit']);
equal(fancy('/404'),        undefined);
equal(fancy('/404/'),       'index.html');
equal(fancy('/404/?limit'), 9000);
equal(fancy('/404/crash'),        null);
equal(fancy('/404/crash/noes'),   undefined);

Known issues

  • Versions before 0.1.3 had a flawed fallback implementation that would return methods of false for some keys.

 

License

ISC