Skip to content

Commit

Permalink
added; support for adapters in set
Browse files Browse the repository at this point in the history
relates to #1
  • Loading branch information
aheckmann committed Mar 15, 2013
1 parent a4d5402 commit 97e85d3
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 13 deletions.
48 changes: 35 additions & 13 deletions lib/index.js
Expand Up @@ -89,13 +89,19 @@ exports.get = function (path, o, special, map) {
* @param {Object} o
* @param {String} [special] When this property name is present on any object in the path, walking will continue on the value of this property.
* @param {Function} [map] Optional function which is passed each individual value before setting it. The value returned from `map` is used in the original values place.
*/

exports.set = function (path, val, o, special, map, _copying) {
var lookup;

if ('function' == typeof special) {
map = special;
special = undefined;
if (special.length < 2) {
map = special;
special = undefined;
} else {
lookup = special;
special = undefined;
}
}

map || (map = K);
Expand Down Expand Up @@ -134,20 +140,24 @@ exports.set = function (path, val, o, special, map, _copying) {
if (!copy && Array.isArray(val)) {
for (var j = 0; j < obj.length && j < val.length; ++j) {
// assignment of single values of array
exports.set(paths, val[j], obj[j], special, map, copy);
exports.set(paths, val[j], obj[j], special || lookup, map, copy);
}
} else {
for (var j = 0; j < obj.length; ++j) {
// assignment of entire value
exports.set(paths, val, obj[j], special, map, copy);
exports.set(paths, val, obj[j], special || lookup, map, copy);
}
}
return;
}

obj = special && obj[special]
? obj[special][part]
: obj[part];
if (lookup) {
obj = lookup(obj, part);
} else {
obj = special && obj[special]
? obj[special][part]
: obj[part];
}

if (!obj) return;
}
Expand All @@ -167,21 +177,33 @@ exports.set = function (path, val, o, special, map, _copying) {
for (var item, j = 0; j < obj.length && j < val.length; ++j) {
item = obj[j];
if (item) {
if (item[special]) item = item[special];
item[part] = map(val[j]);
if (lookup) {
lookup(item, part, map(val[j]));
} else {
if (item[special]) item = item[special];
item[part] = map(val[j]);
}
}
}
} else {
for (var j = 0; j < obj.length; ++j) {
item = obj[j];
if (item) {
if (item[special]) item = item[special];
item[part] = map(val);
if (lookup) {
lookup(item, part, map(val));
} else {
if (item[special]) item = item[special];
item[part] = map(val);
}
}
}
}
} else {
obj[part] = map(val);
if (lookup) {
lookup(item, part, map(val));
} else {
obj[part] = map(val);
}
}
}

Expand Down
38 changes: 38 additions & 0 deletions test/index.js
Expand Up @@ -1658,6 +1658,44 @@ describe('mpath', function(){
})
})

describe('that is a function', function(){
it('works without map', function(done){
var o = { hello: { world: [{ how: 'are' }, { you: '?' }] }};
var special = function (obj, key, val) {
if (val) {
obj[key] = val;
} else {
return 'thing' == key
? obj.world
: obj[key]
}
}
mpath.set('hello.thing.how', 'arrrr', o, special);
assert.deepEqual(o, { hello: { world: [{ how: 'arrrr' }, { you: '?', how: 'arrrr' }] }});
done();
})
it('works with map', function(done){
var o = { hello: { world: [{ how: 'are' }, { you: '?' }] }};
var special = function (obj, key, val) {
if (val) {
obj[key] = val;
} else {
return 'thing' == key
? obj.world
: obj[key]
}
}
var map = function (val) {
return 'convert' == val
? 'ºº'
: val
}
mpath.set('hello.thing.how', 'convert', o, special, map);
assert.deepEqual(o, { hello: { world: [{ how: 'ºº' }, { you: '?', how: 'ºº' }] }});
done();
})
})

})

describe('get/set integration', function(){
Expand Down

0 comments on commit 97e85d3

Please sign in to comment.