args2object
Node/JavaScript library to access an item in an object structure given function arguments as path.
Install in Node
$ npm install args2object
Manual installation for Browser apps
Take the browserified file at dist/args2object.bundle.js
and include it in your HTML:
The browserified file exports the window.args2object
function.
Usage
var args2object = ; // if Node var myObject = a: true b: false c: 'hello' d: d1: 'foo' d2: 1234 true: false: 0 bar: null ; var getter = ; ; // => true; // => false; // => 'hello'; // => 'foo'; // => [1,2,3,4]; // => 0; // => null ; // => undefined; // => undefined; // => undefined; // => undefined; // => undefined ; // => the full object var getter = ; ; // => throws an Error exception; // => throws an Error exception; // => throws an Error exception; // => throws an Error exception; // => throws an Error exception
API
getter = args2object(object, options)
- param
{Object|Function}
object: The object to handle. - param
{Object}
options (optional):{Boolean}
failOnNotFound: Whether to throw anError
if the searched path does not exist in the object structure (default:false
).
Returns a getter
function.
getter(a,b,c)
Retrieve the value at object[a][b][c]
.
If the path does not exist it returns undefined
(or throws an Error
if options.failOnNotFound
is set).
Release History
0.1.1 (2014-11-20)
- Make it much more robust and allow retrieving the full object.
0.1.0 (2014-11-14)
- First release.
License
Copyright (c) 2014 Iñaki Baz Castillo Licensed under the MIT license.