Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Getting properties from values without the error-ridden headache.
JavaScript
branch: master

Fetching latest commit…

Cannot retrieve the latest commit at this time

Failed to load latest commit information.
src
test
README.markdown
package.json

README.markdown

delve

Delve recursively into a value to retrieve a property; without erroring.

browser support

Why

It sucks to have to do if ( obj && obj.prop && obj.prop.secondProp ) { ... }.

Example

delve

var delve = require('delve')

var o = { x: { y: { z: 'my val' } } }

delve(o, 'x.y')               //= { z: 'my val' }
delve(o, 'x.y.z')             //= 'my val'
delve(o, 'x.y.z.foo')         //= undefined
delve(undefined, 'x.y.z.foo') //= undefined
delve(null, 'x.y.z.foo')      //= undefined
delve('foo', 'length')        //= 3

delve.has

var delve = require('delve')

delve.has({ x: { y: undefined } }, 'x.y') //= true
delve.has('foo', 'length')                //= true
delve.has(null, 'foo')                    //= false
delve.has({ x: { } }, 'x.y')              //= false

For more examples, see tests/delve-test.js.

Install

npm

npm install delve

browser

Download src/delve.js, and include it as a script tag.

AMD/require.js

Download src/delve.js, and require it in:

require(['libs/delve'], function(delve){
  // ... assuming delve is in libs/delve, now it's ready to use
})

Note for Lodash Users

For any of you using the fantastic lodash library, you can find similar functionality to delve with the get function.

Something went wrong with that request. Please try again.