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

node-digger

v1.0.14

Published

Digs nested objects out using string or/and array notation

Downloads

16

Readme

node-digger

Digs nested objects out using string or/and array notation

Installation:

Node:

npm install node-digger
var digger = require('node-digger');

Browser:

See Using in browser section

Usage examples:

  1. Extract nested object
  2. Extract nested object using array input
  3. Using constructor to pass some of the inputs
  4. Using one-shot mode (less code)
  5. Providing default value
  6. Error handling

Extract nested object

var data = {a: {b: {c: {d: 10}}}};

console.log(new digger()
    .object(data)
    .level('a.b')
    .or(100)
    .dig());

// => { c: { d: 10 } }

Extract nested object using array input

var data = {a: {b: {c: {d: 10}}}};

console.log(new digger()
    .object(data)
    .level(['a', 'b'])
    .or(100)
    .dig());

// => { c: { d: 10 } }

Using constructor to pass some of the inputs

var data = {a: {b: {c: {d: 10}}}};

console.log(new digger(data, 'a.b')
    .or(100)
    .dig());

// => { c: { d: 10 } }

Note: constructor parameter sequence is: data, level, orValue, errorValue

All constructor parameters can also be specified using chained methods

Providing default value

var data = {a: {b: {c: {d: 10}}}};

console.log(new digger(data, 'a.b.c.d.e', 100)
    .dig());

// => 100   // default value

Using one-shot mode (less code)

var data = {a: {b: {c: {d: 10}}}};

console.log(digger.dig(data, 'a.b.c.d', 100));

// => 10
var data = {a: {b: {c: {d: 10}}}};

console.log(digger.dig(data, null, 100, "some error value"));

// => "some error value"

Error handling

// provide default value on error

var data = {a: {b: {c: {d: 10}}}};

console.log(digger.dig(data, null, 100, "some error value"));

// => "some error value"
// using method chaining

var data = {a: {b: {c: {d: 10}}}};

console.log(new digger()
    .object(data)
    .level(null)    // makes error condition true
    .or(100)
    .onError("some error value")
    .dig());

// => "some error value"
// invoke callback on error

var data = {a: {b: {c: {d: 10}}}};

digger.dig(data, null, 100, function(err, data, level, defaultValue, errorValue){
    console.log(err, data, level, defaultValue, errorValue);
})

// => [Error statck trace], <data>, null, 100, [object Function]
// using method chaining

var data = {a: {b: {c: {d: 10}}}};

new digger()
    .object(data)
    .level(null)
    .or(100)
    .onError(function(err, data, level, defaultValue, errorValue){
        console.log(err, data, level, defaultValue, errorValue);
    });

// => [Error statck trace], <data>, null, 100, [object Function]

Note: Error value is used when either of data or level is null/undefined

Exception is thrown if error value is not specified and error condition is met

Using in browser

  1. Use service like rawgit with node-digger source file
  2. Use any of available CDN providers
  3. Use your own hosting

Then just include it in <script> tag of your page

See test for all API methods