-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit f98e516
Showing
3 changed files
with
54 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
if (typeof module!=='undefined') module.exports = dlv; | ||
|
||
function dlv(obj, key) { | ||
if (key.split) key = key.split('.'); | ||
for (var i=0; i<key.length && obj; i++) obj = obj[key[i]]; | ||
return obj; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
{ | ||
"name": "dlv", | ||
"version": "1.0.0", | ||
"description": "Safely get a dot-notated property within an object.", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "node test" | ||
}, | ||
"keywords": ["delve","dot notation","dot"], | ||
"files": ["index.js"], | ||
"author": "Jason Miller <[email protected]>", | ||
"license": "MIT" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
var assert = require('assert'); | ||
var dlv = require('.'); | ||
|
||
var obj = { | ||
one: 1, | ||
a: { | ||
two: 2, | ||
b: { | ||
three: 3, | ||
c: { | ||
four: 4 | ||
} | ||
} | ||
} | ||
}; | ||
|
||
function check(path, value) { | ||
assert.equal(dlv(obj, path), value); | ||
console.log(' ✓ dlv(obj, "'+path+'")'); | ||
} | ||
|
||
check('', undefined); | ||
check('one', obj.one); | ||
check('one.two', undefined); | ||
check('a', obj.a); | ||
check('a.two', obj.a.two); | ||
check('a.b', obj.a.b); | ||
check('a.b.three', obj.a.b.three); | ||
check('a.b.c', obj.a.b.c); | ||
check('a.b.c.four', obj.a.b.c.four); | ||
|
||
console.log('✅ Success!'); | ||
process.exit(0); |