Comparing version 1.4.1 to 1.5.0
@@ -258,10 +258,23 @@ // Load modules | ||
exports.reach = function (obj, chain, separator) { | ||
exports.reach = function (obj, chain, options) { | ||
var path = chain.split(separator || '.'); | ||
options = options || {}; | ||
if (typeof options === 'string') { | ||
options = { separator: options }; | ||
} | ||
var path = chain.split(options.separator || '.'); | ||
var ref = obj; | ||
for (var i = 0, il = path.length; i < il; ++i) { | ||
if (ref) { | ||
ref = ref[path[i]]; | ||
if (!ref || | ||
!ref.hasOwnProperty(path[i]) || | ||
(typeof ref !== 'object' && (options.functions === false || typeof ref !== 'function'))) { | ||
exports.assert(!options.strict || i + 1 === il, 'Missing segment', path[i], 'in reach path ', chain); | ||
exports.assert(typeof ref === 'object' || options.functions === true || typeof ref !== 'function', 'Invalid segment', path[i], 'in reach path ', chain); | ||
ref = undefined; | ||
break; | ||
} | ||
ref = ref[path[i]]; | ||
} | ||
@@ -268,0 +281,0 @@ |
{ | ||
"name": "hoek", | ||
"description": "General purpose node utilities", | ||
"version": "1.4.1", | ||
"version": "1.5.0", | ||
"repository": "git://github.com/spumko/hoek", | ||
@@ -6,0 +6,0 @@ "main": "index", |
@@ -21,3 +21,3 @@ <a href="https://github.com/spumko"><img src="https://raw.github.com/spumko/spumko/master/images/from.png" align="right" /></a> | ||
* [removeKeys](#removekeysobject-keys "removeKeys") | ||
* [reach](#reachobj-chain "reach") | ||
* [reach](#reachobj-chain-options "reach") | ||
* [inheritAsync](#inheritasyncself-obj-keys "inheritAsync") | ||
@@ -208,3 +208,3 @@ * [rename](#renameobj-from-to "rename") | ||
### reach(obj, chain) | ||
### reach(obj, chain, [options]) | ||
@@ -211,0 +211,0 @@ Converts an object key chain string to reference |
@@ -593,5 +593,8 @@ // Load modules | ||
}, | ||
i: function () { } | ||
i: function () { }, | ||
j: null | ||
}; | ||
obj.i.x = 5; | ||
it('returns a valid member', function (done) { | ||
@@ -609,20 +612,30 @@ | ||
it('returns null on null object', function (done) { | ||
it('returns undefined on null object', function (done) { | ||
expect(Hoek.reach(null, 'a.b.c.d')).to.not.exist; | ||
expect(Hoek.reach(null, 'a.b.c.d')).to.equal(undefined); | ||
done(); | ||
}); | ||
it('returns null on missing member', function (done) { | ||
it('returns undefined on missing member', function (done) { | ||
expect(Hoek.reach(obj, 'a.b.c.d.x')).to.not.exist; | ||
expect(Hoek.reach(obj, 'a.b.c.d.x')).to.equal(undefined); | ||
done(); | ||
}); | ||
it('returns null on invalid member', function (done) { | ||
it('throws on missing member in strict mode', function (done) { | ||
expect(Hoek.reach(obj, 'a.b.c.d-.x')).to.not.exist; | ||
expect(function () { | ||
Hoek.reach(obj, 'a.b.c.o.x', { strict: true }); | ||
}).to.throw('Missing segment o in reach path a.b.c.o.x'); | ||
done(); | ||
}); | ||
it('returns undefined on invalid member', function (done) { | ||
expect(Hoek.reach(obj, 'a.b.c.d-.x')).to.equal(undefined); | ||
done(); | ||
}); | ||
it('returns function member', function (done) { | ||
@@ -633,2 +646,24 @@ | ||
}); | ||
it('returns function property', function (done) { | ||
expect(Hoek.reach(obj, 'i.x')).to.equal(5); | ||
done(); | ||
}); | ||
it('returns null', function (done) { | ||
expect(Hoek.reach(obj, 'j')).to.equal(null); | ||
done(); | ||
}); | ||
it('throws on function property when functions not allowed', function (done) { | ||
expect(function () { | ||
Hoek.reach(obj, 'i.x', { functions: false }); | ||
}).to.throw('Invalid segment x in reach path i.x'); | ||
done(); | ||
}); | ||
}); | ||
@@ -635,0 +670,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
107979
1543