Skip to content

Digs nested objects out using string or array notation

Notifications You must be signed in to change notification settings

freakynit/node-digger

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

26 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

About

Digs nested objects out using string or array notation

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published