Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow reach to work with path array #280

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions API.md
Original file line number Diff line number Diff line change
Expand Up @@ -218,10 +218,10 @@ flattenedArray = Hoek.flatten(array, target); // results in [4, [5], 1, 2, 3]

### reach(obj, chain, [options])

Converts an object key chain string to reference
Converts an object **key chain string** or **path array** to reference

- `options` - optional settings
- `separator` - string to split chain path on, defaults to '.'
- `separator` - string to split string chain path on, defaults to '.'. if set with path array, will throw an error.
- `default` - value to return if the path or value is not present, default is `undefined`
- `strict` - if `true`, will throw an error on missing member, default is `false`
- `functions` - if `true` allow traversing functions for properties. `false` will throw an error if a function is part of the chain.
Expand All @@ -238,6 +238,11 @@ var obj = {a : {b : { c : 1}}};

Hoek.reach(obj, chain); // returns 1

var chain = ['a', 'b'];
var obj = {a : {b : [2,3,6]}};

Hoek.reach(obj, chain); // returns [2,3,6]

var chain = 'a.b.-1';
var obj = {a : {b : [2,3,6]}};

Expand Down
5 changes: 3 additions & 2 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -467,7 +467,7 @@ exports.flatten = function (array, target) {
};


// Convert an object key chain string ('a.b.c') to reference (object[a][b][c])
// Convert an object key chain string ('a.b.c') or path array ['a','b','c'] to reference (object[a][b][c])

exports.reach = function (obj, chain, options) {

Expand All @@ -483,7 +483,8 @@ exports.reach = function (obj, chain, options) {
options = { separator: options };
}

const path = chain.split(options.separator || '.');
exports.assert(!Array.isArray(chain) || !options.seperator, 'Invalid arguments: cannot use seperator option with path array');
const path = Array.isArray(chain) ? chain : chain.split(options.separator || '.');
let ref = obj;
for (let i = 0; i < path.length; ++i) {
let key = path[i];
Expand Down
15 changes: 15 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1748,6 +1748,7 @@ describe('reach()', () => {

expect(Hoek.reach(obj, null)).to.equal(obj);
expect(Hoek.reach(obj, false)).to.equal(obj);
expect(Hoek.reach(obj, [])).to.equal(obj);
expect(Hoek.reach(obj)).to.equal(obj);
});

Expand Down Expand Up @@ -1776,6 +1777,20 @@ describe('reach()', () => {
expect(Hoek.reach(obj, 'a/b/c/d', '/')).to.equal(1);
});

it('returns a valid member with path array', () => {

expect(Hoek.reach(obj, ['a', 'b', 'c', 'd'])).to.equal(1);
});

it('throws on path array with seperator override', () => {

expect(() => {

Hoek.reach(obj, ['a', 'b', 'c', 'd'], { seperator: '-' });
}).to.throw('Invalid arguments: cannot use seperator option with path array');

});

it('returns undefined on null object', () => {

expect(Hoek.reach(null, 'a.b.c.d')).to.equal(undefined);
Expand Down