path-lookup
lookup path within object
.
Installation
Install with component(1):
$ component install yields/path-lookup
Example
user: first_name: 'john'
// lookup var path = ;path; // => "['user']['first_name']" // getFunction'_' 'return _' + pathobj; // => "john" // setFunction'_, v' '_' + path + ' = v'obj ''; // getFunction'_' 'return _' + pathobj; // => "" // deleteFunction'_' 'delete _' + pathobj; // => true // getFunction'_' 'return _' + pathobj; // => undefined
API
lookup(obj, path, fns)
Lookup path
within obj
invoking fn(key)
for each key that wasn't found
the functions should return other possibilities for a key.
return a string path
or null
lookup.keys(obj, path, fns)
Same as lookup()
but returns an array of keys
or null
.
caveats
you should probably make sure the path
exists, before doing anything
that will throw.
you can either keep cache of path's and their state { path: '...', exists: true }
or just wrap stuff that will throw in a try {} catch {}
block.
License
MIT