goodwin

Deep object inspection and modification given a stringed path.

npm install goodwin
3 downloads in the last week
9 downloads in the last month

Goodwin Build Status

Deep object inspection and modification given a stringed path.

Features

  • blazing fast (see benchmarks)
  • works for objects and arrays
  • supports infinite nesting

Installation

Node.js

goodwin is available on npm.

$ npm install goodwin
Component

goodwin is available as a component.

$ component install logicalparadox/goodwin
Browser

goodwin is available as an AMD compatible script in the dist directory.

<script src="goodwin.js"></script>
<script src="goodwin.min.js"></script>

API

.getPathValue(path, object)

  • @param {String} path
  • @param {Object} object
  • @returns {Object} value or undefined

Retrieve the value in an object given a string path.

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

The following would be the results.

goodwin.getPathValue('prop1.str', obj); // Hello
goodwin.getPathValue('prop1.att[2]', obj); // b
goodwin.getPathValue('prop2.arr[0].nested', obj); // Universe

.setPathValue(path, value, object)

  • @param {String} path
  • @param {Mixed} value
  • @param {Object} object

Defining the value in an object at a given string path.

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

The following would be acceptable.

goodwin.setPathValue('prop1.str', 'Hello Universe!', obj);
goodwin.setPathValue('prop1.arr[2]', 'B', obj);
goodwin.setPathValue('prop2.arr[0].nested.value', { hello: 'universe' }, obj);

Tests

Tests are written in Mocha using the Chai should BDD assertion library. To make sure you have that installed, clone this repo, install dependacies using npm install -d.

$ npm test

Benchmarks

Benchmarks are written in Matcha. To make sure you have that installed, clone this repo and install dependancies using npm install -d.

$ npm run-script bench

Contributors

Interested in contributing? Fork to get started. Contact @logicalparadox if you are interested in being regular contributor.

License

(The MIT License)

Copyright (c) 2012 Jake Luer jake@alogicalparadox.com (http://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