pathval

Object value retrieval given a string path

npm install pathval
12 downloads in the last day
108 downloads in the last week
386 downloads in the last month

NPM version Build Status Coverage Status Code Climate

pathval

Usage

var props = require('pathval');

Given:

var obj = {
    prop1: {
        arr: ['a', 'b', 'c']
      , str: 'Hello'
    }
  , prop2: {
        arr: [ { nested: 'Universe' } ]
      , str: 'Hello again!'
    }
}

var arr = [ { foo: 'bar' } ];

Expect:

get

props.get(obj, 'prop1.str'); // => "Hello"
props.get(obj, 'prop1.arr[2]'); // => "c"
props.get(obj, 'prop2.arr[0].nested'); // => "Universe"

props.get(arr, '[0].foo'); // => "bar"

props.get(undefined, 'doesnt.matter'); // => undefined
props.get({}, 'doesnt.exist'); // => undefined

set

props.set(obj, 'hello.universe', 'properties');
props.set(obj, 'hello.universe[1].properties', 'galaxy');
props.set(obj, 'hello', 'universe');
props.set(obj, 'hello[0]', 1);
props.set(obj, 'hello[2]', 3);

Installation

npm install pathval

Tests

Running the tests

$ npm test

Test coverage

$ npm run-script coverage

Readme tests

$ npm run-script test-readme

License

MIT License

Copyright (c) 2011-2013 Jake Luer jake@alogicalparadox.com

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

npm loves you