Skip to content

Commit

Permalink
omit support keypaths, remove use of clone
Browse files Browse the repository at this point in the history
  • Loading branch information
tjmehta committed Mar 8, 2016
1 parent 7ca123c commit 36ca8c1
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 6 deletions.
14 changes: 8 additions & 6 deletions omit.js
Expand Up @@ -3,7 +3,8 @@
*/

var isObject = require('./is-object');
var clone = require('./clone');
var pick = require('./pick')
var keypather = require('keypather')()

/**
* Returns a new object without the specified keys.
Expand All @@ -27,17 +28,18 @@ module.exports = function () {
};

function omit (obj, args) {
var keys = [];
var omitKeys = [];
args.forEach(function (key) {
keys = keys.concat(key);
omitKeys = omitKeys.concat(key);
});
var out = clone(obj);
keys.forEach(remove(out));
var keys = Object.keys(obj);
var out = pick(obj, keys);
omitKeys.forEach(remove(out));
return out;
}

function remove (obj) {
return function (key) {
delete obj[key];
keypather.del(obj, key);
};
}
25 changes: 25 additions & 0 deletions test/test-omit.js
Expand Up @@ -112,6 +112,31 @@ describe('omit', function () {
goo: 3
}
]);
var objs2 = [
{
bar: 1
},
{
foo: { bar: 2 },
bar: 2,
qux: 2
},
{
foo: { bar: 3 },
bar: 3,
koo: 3,
goo: 3
}
];
expect(objs2.map(omit(['foo.bar', 'bar'], ['qux']))).to.deep.equal([
{},
{ foo: {} },
{
foo: {},
koo: 3,
goo: 3
}
]);
expect(objs.map(omit())).to.deep.equal(objs);
expect(objs.map(omit([]))).to.deep.equal(objs);
done();
Expand Down

0 comments on commit 36ca8c1

Please sign in to comment.