Skip to content

Commit f98e516

Browse files
committed
Initial/final commit!
0 parents  commit f98e516

3 files changed

Lines changed: 54 additions & 0 deletions

File tree

index.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
if (typeof module!=='undefined') module.exports = dlv;
2+
3+
function dlv(obj, key) {
4+
if (key.split) key = key.split('.');
5+
for (var i=0; i<key.length && obj; i++) obj = obj[key[i]];
6+
return obj;
7+
}
8+

package.json

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"name": "dlv",
3+
"version": "1.0.0",
4+
"description": "Safely get a dot-notated property within an object.",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "node test"
8+
},
9+
"keywords": ["delve","dot notation","dot"],
10+
"files": ["index.js"],
11+
"author": "Jason Miller <jason@developit.ca>",
12+
"license": "MIT"
13+
}

test.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
var assert = require('assert');
2+
var dlv = require('.');
3+
4+
var obj = {
5+
one: 1,
6+
a: {
7+
two: 2,
8+
b: {
9+
three: 3,
10+
c: {
11+
four: 4
12+
}
13+
}
14+
}
15+
};
16+
17+
function check(path, value) {
18+
assert.equal(dlv(obj, path), value);
19+
console.log(' ✓ dlv(obj, "'+path+'")');
20+
}
21+
22+
check('', undefined);
23+
check('one', obj.one);
24+
check('one.two', undefined);
25+
check('a', obj.a);
26+
check('a.two', obj.a.two);
27+
check('a.b', obj.a.b);
28+
check('a.b.three', obj.a.b.three);
29+
check('a.b.c', obj.a.b.c);
30+
check('a.b.c.four', obj.a.b.c.four);
31+
32+
console.log('✅ Success!');
33+
process.exit(0);

0 commit comments

Comments
 (0)