Digs nested objects out using string or/and array notation
npm install node-digger
var digger = require('node-digger');
- Extract nested object
- Extract nested object using array input
- Using constructor to pass some of the inputs
- Using one-shot mode (less code)
- Providing default value
- Error handling
var data = {a: {b: {c: {d: 10}}}};
console.log(new digger()
.object(data)
.level('a.b')
.or(100)
.dig());
// => { c: { d: 10 } }
var data = {a: {b: {c: {d: 10}}}};
console.log(new digger()
.object(data)
.level(['a', 'b'])
.or(100)
.dig());
// => { c: { d: 10 } }
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
var data = {a: {b: {c: {d: 10}}}};
console.log(new digger(data, 'a.b.c.d.e', 100)
.dig());
// => 100 // default value
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"
// 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
orlevel
is null/undefined
Exception is thrown if error value is not specified and error condition is met
- Use service like rawgit with node-digger source file
- Use any of available CDN providers
- Use your own hosting
Then just include it in <script>
tag of your page