Skip to content

Commit

Permalink
permit get/set path value of nested arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
logicalparadox committed Jun 19, 2012
1 parent f25e9c8 commit 84848c5
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 37 deletions.
63 changes: 30 additions & 33 deletions lib/filtr.js
Original file line number Diff line number Diff line change
Expand Up @@ -234,13 +234,13 @@ Filtr.setPathValue = function (path, val, obj) {
*/

function parsePath (path) {
var parts = path.split('.').filter(Boolean);
var str = path.replace(/\[/g, '.[')
, parts = str.match(/(\\\.|[^.]+?)+/g);
return parts.map(function (value) {
var re = /([A-Za-z0-9]+)\[(\d+)\]$/
var re = /\[(\d+)\]$/
, mArr = re.exec(value)
, val;
if (mArr) val = { p: mArr[1], i: parseFloat(mArr[2]) };
return val || value;
if (mArr) return { i: parseFloat(mArr[1]) };
else return { p: value };
});
};

Expand All @@ -263,11 +263,10 @@ function getPathValue (parsed, obj) {
for (var i = 0, l = parsed.length; i < l; i++) {
var part = parsed[i];
if (tmp) {
if ('object' === typeof part && tmp[part.p]) {
tmp = tmp[part.p][part.i];
} else {
tmp = tmp[part];
}
if ('undefined' !== typeof part.p)
tmp = tmp[part.p];
else if ('undefined' !== typeof part.i)
tmp = tmp[part.i];
if (i == (l - 1)) res = tmp;
} else {
res = undefined;
Expand Down Expand Up @@ -296,34 +295,32 @@ function setPathValue (parsed, val, obj) {
var part = parsed[i];
if ('undefined' !== typeof tmp) {
if (i == (l - 1)) {
if ('object' === typeof part && tmp[part.p]) {
tmp[part.p][part.i] = val;
} else {
tmp[part] = val;
}
if ('undefined' !== typeof part.p)
tmp[part.p] = val;
else if ('undefined' !== typeof part.i)
tmp[part.i] = val;
} else {
if ('object' === typeof part && tmp[part.p]) {
tmp = tmp[part.p][part.i];
} else if ('object' === typeof part && !tmp[part.p] ) {
tmp[part.p] = []; //new Array(part.i + 1);
if ('object' === typeof parsed[i + 1]) tmp[part.p][part.i] = [];
else tmp[part.p][part.i] = {};
tmp = tmp[part.p][part.i];
} else if (!tmp[part]) {
tmp[part] = {};
tmp = tmp[part];
} else {
tmp = tmp[part];
if ('undefined' !== typeof part.p && tmp[part.p])
tmp = tmp[part.p];
else if ('undefined' !== typeof part.i && tmp[part.i])
tmp = tmp[part.i];
else {
var next = parsed[i + 1];
if ('undefined' !== typeof part.p) {
tmp[part.p] = {};
tmp = tmp[part.p];
} else if ('undefined' !== typeof part.i) {
tmp[part.i] = [];
tmp = tmp[part.i]
}
}
}
} else {
if (i == (l - 1)) {
tmp = val;
} else if ('object' === typeof part) {
tmp = [];
} else {
if (i == (l - 1)) tmp = val;
else if ('undefined' !== typeof part.p)
tmp = {};
}
else if ('undefined' !== typeof part.i)
tmp = [];
}
}
};
Expand Down
34 changes: 30 additions & 4 deletions test/query.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,36 @@ describe('Query', function () {
Q.test({ test: 'hello', world: 'galaxy' }, { type: 'single' }).should.be.false;
});

it('should provide the getPathValue helper', function () {
var obj = { hello: { universe: 'world' }}
, val = filtr.getPathValue('hello.universe', obj);
val.should.equal('world');
describe('getPathValue', function () {
it('can get value for simple nested object', function () {
var obj = { hello: { universe: 'world' }}
, val = filtr.getPathValue('hello.universe', obj);
val.should.equal('world');
});

it('can get value for simple array', function () {
var obj = { hello: [ 'zero', 'one' ] }
, val = filtr.getPathValue('hello[1]', obj);
val.should.equal('one');
});

it('can get value of nested array', function () {
var obj = { hello: [ 'zero', [ 'a', 'b' ] ] }
, val = filtr.getPathValue('hello[1][0]', obj);
val.should.equal('a');
});

it('can get value of array only', function () {
var obj = [ 'zero', 'one' ]
, val = filtr.getPathValue('[1]', obj);
val.should.equal('one');
});

it('can get value of array only nested', function () {
var obj = [ 'zero', [ 'a', 'b' ] ]
, val = filtr.getPathValue('[1][1]', obj);
val.should.equal('b');
});
});

describe('setPathValue', function () {
Expand Down

0 comments on commit 84848c5

Please sign in to comment.