hoek
Advanced tools
Comparing version 0.1.0 to 0.2.0
@@ -223,2 +223,19 @@ // Load modules | ||
// Convert an object key chain string ('a.b.c') to reference (object[a][b][c]) | ||
exports.reach = function (obj, chain) { | ||
var path = chain.split('.'); | ||
var ref = obj; | ||
path.forEach(function (level) { | ||
if (ref) { | ||
ref = ref[level]; | ||
} | ||
}); | ||
return ref; | ||
}; | ||
// Inherits a selected set of methods from an object, wrapping functions in asynchronous syntax and catching errors | ||
@@ -225,0 +242,0 @@ |
{ | ||
"name": "hoek", | ||
"description": "General purpose node utilities", | ||
"version": "0.1.0", | ||
"version": "0.2.0", | ||
"author": "Eran Hammer <eran@hueniverse.com> (http://hueniverse.com)", | ||
@@ -6,0 +6,0 @@ "contributors":[ |
@@ -229,2 +229,51 @@ // Load modules | ||
describe('#reach', function () { | ||
var obj = { | ||
a: { | ||
b: { | ||
c: { | ||
d: 1, | ||
e: 2 | ||
}, | ||
f: 'hello', | ||
}, | ||
g: { | ||
h: 3 | ||
} | ||
}, | ||
i: function () { } | ||
}; | ||
it('returns a valid member', function (done) { | ||
expect(Hoek.reach(obj, 'a.b.c.d')).to.equal(1); | ||
done(); | ||
}); | ||
it('returns null on null object', function (done) { | ||
expect(Hoek.reach(null, 'a.b.c.d')).to.not.exist; | ||
done(); | ||
}); | ||
it('returns null on missing member', function (done) { | ||
expect(Hoek.reach(obj, 'a.b.c.d.x')).to.not.exist; | ||
done(); | ||
}); | ||
it('returns null on invalid member', function (done) { | ||
expect(Hoek.reach(obj, 'a.b.c.d-.x')).to.not.exist; | ||
done(); | ||
}); | ||
it('returns function member', function (done) { | ||
expect(typeof Hoek.reach(obj, 'i')).to.equal('function'); | ||
done(); | ||
}); | ||
}); | ||
describe('#inheritAsync', function () { | ||
@@ -231,0 +280,0 @@ |
72396
808