Skip to content

Commit

Permalink
Initial/final commit!
Browse files Browse the repository at this point in the history
  • Loading branch information
developit committed Aug 4, 2016
0 parents commit f98e516
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 0 deletions.
8 changes: 8 additions & 0 deletions index.js
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;
}

13 changes: 13 additions & 0 deletions package.json
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"
}
33 changes: 33 additions & 0 deletions test.js
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);

0 comments on commit f98e516

Please sign in to comment.