1.1.2 • Published 6 years ago

lst-get v1.1.2

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

lst-get

NPM

Build Status Language License Size

English | 中文

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)

ParamTypeRequiredDescription
objectobjecttruethe input object
expressionstringtruethe property accessor expression to get object value
defaultValue*falsethe default value, if [object].[expression] is undefined

License

license