Code Monkey home page Code Monkey logo

expr's Introduction

expr

Tiny expression helper for creating compiled accessors; handles most stuff, including ["bracket notation"] for property access. Originally based off of Kendo UI Core expression code

npm install property-expr

Use

Setters and getters are compiled to functions and cached for Performance™

var expr = require('property-expr')
  , obj = {
    foo: {
      bar: [ "hi", { buz: { baz: 'found me!' } }]
    }
  };

var getBaz = expr.getter('foo.bar[1]["buz"].baz')
  , setBaz = expr.setter('foo.bar[1]["buz"].baz')

console.log(getBaz(obj)) // => 'found me!'
setBaz(obj, 'set me!')
console.log(obj.foo.bar[1].buz.baz) // => 'set me!'

getter(expression, [ safeAccess ])

Returns a function that accepts an obj and returns the value at the supplied expression. You can create a "safe" getter, which won't error out when accessing properties that don't exist, reducing existance checks befroe property access:

expr.getter('foo.bar.baz', true)({ foo: {} }) // => undefined
//instead of val = foo.bar && foo.bar.baz

setter(expression)

Returns a function that accepts an obj and a value and sets the property pointed to by the expression to the supplied value.

expr(expression, [ safeAccess], [ paramName = 'data'])

Returns a normalized expression string pointing to a property on root object paramName.

expr.expr("foo['bar'][0].baz", true, 'obj') // => "(((obj.foo || {})['bar'] || {})[0])"

split(path) -> Array

Returns an array of each path segment.

expr.split("foo['bar'][0].baz") // [ "foo", "'bar'", "0", "baz"]

forEach(path, iterator[, thisArg])

Iterate through a path but segment, with some additional helpful metadata about the segment. The iterator function is called with: pathSegment, isBracket, isArray, idx, segments

expr.forEach('foo["bar"][1]', function(pathSegment, isBracket, isArray, idx, segments) {
  // 'foo'   -> isBracket = false, isArray = false, idx = 0
  // '"bar"' -> isBracket = true,  isArray = false, idx = 1
  // '0'     -> isBracket = false, isArray = true,  idx = 2
})

normalizePath(path)

Returns an array of path segments without quotes and spaces.

expr.normalizePath('foo["bar"][ "1" ][2][ " sss " ]')
// ['foo', 'bar', '1', '2', ' sss ']

new Cache(maxSize)

Just an utility class, returns an instance of cache. When the max size is exceeded, cache clears its storage.

var cache = new Cache(2)
cache.set('a', 123) // returns 123
cache.get('a') // returns 123
cache.clear()

cache.set('a', 1)
cache.set('b', 2) // cache contains 2 values
cache.set('c', 3) // cache was cleaned automatically and contains 1 value

expr's People

Contributors

corydeppen avatar jquense avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.