lst-get
v1.1.2
Published
Maybe the lightest package to access nested property values at any depth in the world
Downloads
6
Maintainers
Readme
lst-get
Maybe the lightest package to access nested property values at any depth in the world. Less than 0.3K size after compression.
Feature
- Light and easy
- Access nested property values at any depth
- Execute function, like
lstGet(obj, 'a.f()')
- You can use default value, when the result is undefined
Install
npm install lst-get --save
Usage
// the object
var obj = {
a: {
b: [
{
c: 'foo'
}
],
f: function(){
return 'fn-foo'
},
f2: function(){
return {
d: 'fn-bar'
}
}
}
}
// before,bad code with long logical expressions
var value = obj.a && obj.a.b && obj.a.b[0] && obj.a.b[0].c
// now, nice code
var lstGet = require('lst-get')
lstGet(obj, 'a.b[0].c') //=> 'foo'
lstGet(obj, 'a.b[0].c.e', 'defaultValue') //=> 'defaultValue'
lstGet(obj, 'a.f()') //=> 'fn-foo'
lstGet(obj, 'a.f2().d') //=> 'fn-bar'
Arguments
(object, expression, defaultValue)
| Param | Type | Required | Description |
| --- | --- | --- | --- |
| object | object
| true
| the input object |
| expression | string
| true
| the property accessor expression to get object value |
| defaultValue | *
| false
| the default value, if [object].[expression]
is undefined |