get-first

0.1.2 • Public • Published

get-first NPM version

Pass an array of object paths (e.g. a.b.c) and get the first non-undefined value if the property exists, or null if it does not.

Install

Install with npm:

$ npm i get-first

Run tests

npm test

Usage

var getFirst = require('get-first');
 
getFirst({a: 'b'}, 'a');
//=> 'b'
 
getFirst({a: 'b'}, ['a']);
//=> 'b'
 
getFirst({a: {b: 'c'}}, 'a.b');
//=> 'c'
 
getFirst({a: {b: 'c'}}, ['a.b']);
//=> 'c'
 
getFirst({a: {b: 'c'}, x: {b: 'd'}}, ['a.b', 'x.b']);
//=> 'c'
 
getFirst({a: {b: 'c'}, x: {b: 'd'}}, ['x.b', 'a.b']);
//=> 'd'
 
getFirst({a: {b: {c: 'd'}}}, ['a.b', 'x.y.z']);
//=> {c: 'd'}
 
getFirst({a: {b: {c: 'd'}, e: 'f'}}, ['a.c', 'a.e']);
//=> 'f'

Related

Running tests

Install dev dependencies:

$ npm i -d && npm test

Contributing

Pull requests and stars are always welcome. For bugs and feature requests, please create an issue.

Author

Jon Schlinkert

License

Copyright © 2016 Jon Schlinkert Released under the MIT license.


This file was generated by verb on January 18, 2016.

Package Sidebar

Install

npm i get-first

Weekly Downloads

130

Version

0.1.2

License

MIT

Last publish

Collaborators

  • doowb
  • jonschlinkert