1.0.14 • Published 6 years ago

node-digger v1.0.14

Weekly downloads
4
License
MIT
Repository
github
Last release
6 years ago

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

1.0.14

6 years ago

1.0.13

6 years ago

1.0.12

6 years ago

1.0.11

6 years ago

1.0.10

6 years ago

1.0.9

6 years ago

1.0.8

6 years ago

1.0.7

6 years ago

1.0.6

6 years ago

1.0.5

6 years ago

1.0.4

6 years ago