Skip to content

Commit

Permalink
feat: add rudimentary support for maps
Browse files Browse the repository at this point in the history
  • Loading branch information
vkarpov15 committed Mar 26, 2018
1 parent b62ca24 commit 744b3c8
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 7 deletions.
18 changes: 11 additions & 7 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,10 @@ exports.get = function (path, o, special, map) {
if (lookup) {
obj = lookup(obj, part);
} else {
obj = special && obj[special]
? obj[special][part]
: obj[part];
var _from = special && obj[special] ? obj[special] : obj;
obj = _from instanceof Map ?
_from.get(part) :
obj[part];
}

if (!obj) return map(obj);
Expand Down Expand Up @@ -134,7 +135,7 @@ exports.unset = function (path, o) {
delete cur[parts[i]];
return true;
}
cur = cur[parts[i]];
cur = cur instanceof Map ? cur.get(parts[i]) : cur[parts[i]];
}

return true;
Expand Down Expand Up @@ -213,9 +214,10 @@ exports.set = function (path, val, o, special, map, _copying) {
if (lookup) {
obj = lookup(obj, part);
} else {
obj = special && obj[special]
? obj[special][part]
: obj[part];
var _to = special && obj[special] ? obj[special] : obj;
obj = _to instanceof Map ?
_to.get(part) :
_to[part];
}

if (!obj) return;
Expand Down Expand Up @@ -260,6 +262,8 @@ exports.set = function (path, val, o, special, map, _copying) {
} else {
if (lookup) {
lookup(obj, part, map(val));
} else if (obj instanceof Map) {
obj.set(part, val);
} else {
obj[part] = map(val);
}
Expand Down
11 changes: 11 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1792,6 +1792,17 @@ describe('mpath', function(){
done();
});

it('underneath a map', function(done) {
assert.equal(mpath.get('a.b', { a: new Map([['b', 1]]) }), 1);

var m = new Map([['b', 1]]);
var obj = { a: m };
mpath.set('a.c', 2, obj);
assert.equal(m.get('c'), 2);

done();
});

it('unset', function(done) {
var o = { a: 1 };
mpath.unset('a', o);
Expand Down

0 comments on commit 744b3c8

Please sign in to comment.