Skip to content

Commit

Permalink
feat: support ensure option in Object.copy
Browse files Browse the repository at this point in the history
  • Loading branch information
medikoo committed May 17, 2017
1 parent d7e7cef commit 295326f
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 3 deletions.
6 changes: 3 additions & 3 deletions object/copy.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ var aFrom = require('../array/from')
, assign = require('./assign')
, value = require('./valid-value');

module.exports = function (obj/*, propertyNames*/) {
var copy = Object(value(obj)), propertyNames = arguments[1];
module.exports = function (obj/*, propertyNames, options*/) {
var copy = Object(value(obj)), propertyNames = arguments[1], options = Object(arguments[2]);
if (copy !== obj && !propertyNames) return copy;
var result = {};
if (propertyNames) {
aFrom(propertyNames, function (propertyName) {
if (propertyName in obj) result[propertyName] = obj[propertyName];
if (options.ensure || propertyName in obj) result[propertyName] = obj[propertyName];
});
} else {
assign(result, obj);
Expand Down
1 change: 1 addition & 0 deletions test/object/copy.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,5 @@ module.exports = function (t, a) {
a.deep(t(o, ['foo']), { foo: 'bar' });
a.deep(t(Object.create(o), ['foo']), { foo: 'bar' });
a.deep(t(o, ['foo', 'habla']), { foo: 'bar' });
a.deep(t(o, ['foo', 'habla'], { ensure: true }), { foo: 'bar', habla: undefined });
};

0 comments on commit 295326f

Please sign in to comment.