Skip to content

Commit

Permalink
feat: if listed copy not only if own property
Browse files Browse the repository at this point in the history
  • Loading branch information
medikoo committed May 17, 2017
1 parent f1d1179 commit d7e7cef
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 5 deletions.
9 changes: 4 additions & 5 deletions object/copy.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
'use strict';

var aFrom = require('../array/from')
, assign = require('./assign')
, value = require('./valid-value')
, propertyIsEnumerable = Object.prototype.propertyIsEnumerable;
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];
if (copy !== obj && !propertyNames) return copy;
var result = {};
if (propertyNames) {
aFrom(propertyNames, function (propertyName) {
if (propertyIsEnumerable.call(obj, propertyName)) result[propertyName] = obj[propertyName];
if (propertyName in obj) result[propertyName] = obj[propertyName];
});
} else {
assign(result, obj);
Expand Down
2 changes: 2 additions & 0 deletions test/object/copy.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,6 @@ module.exports = function (t, a) {
a(o.raz, no.raz, 'Shallow');

a.deep(t(o, ['foo']), { foo: 'bar' });
a.deep(t(Object.create(o), ['foo']), { foo: 'bar' });
a.deep(t(o, ['foo', 'habla']), { foo: 'bar' });
};

0 comments on commit d7e7cef

Please sign in to comment.