lst-get

1.1.2 • Public • Published

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)

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

License

license

Package Sidebar

Install

npm i lst-get

Weekly Downloads

0

Version

1.1.2

License

MIT

Unpacked Size

6.45 kB

Total Files

5

Last publish

Collaborators

  • chanceyu