Skip to content

Commit

Permalink
feat: support propertyNames 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 583ce67 commit 5442279
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 14 deletions.
25 changes: 18 additions & 7 deletions object/copy.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,21 @@
'use strict';
"use strict";

var assign = require('./assign')
, value = require('./valid-value');
var aFrom = require("../array/from")
, assign = require("./assign")
, value = require("./valid-value")
, propertyIsEnumerable = Object.prototype.propertyIsEnumerable;

module.exports = function (obj) {
var copy = Object(value(obj));
if (copy !== obj) return copy;
return assign({}, obj);
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];
});
} else {
assign(result, obj);
}
return result;
};
22 changes: 15 additions & 7 deletions test/object/copy.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,27 @@
'use strict';
"use strict";

var stringify = JSON.stringify;

module.exports = function (t, a) {
var o = { 1: 'raz', 2: 'dwa', 3: 'trzy' }
, no = t(o);
module.exports = function(t, a) {
var o = { 1: "raz", 2: "dwa", 3: "trzy" }, no = t(o);

a.not(no, o, "Return different object");
a(stringify(no), stringify(o), "Match properties and values");

o = { foo: 'bar', raz: { dwa: 'dwa',
trzy: { cztery: 'pięć', 'sześć': 'siedem' }, osiem: {},
'dziewięć': function () { } }, 'dziesięć': 10 };
o = {
foo: "bar",
raz: {
dwa: "dwa",
trzy: { cztery: "pięć", sześć: "siedem" },
osiem: {},
dziewięć: function() {}
},
dziesięć: 10
};
o.raz.rec = o;

no = t(o);
a(o.raz, no.raz, "Shallow");

a.deep(t(o, ["foo"]), { foo: "bar" });
};

0 comments on commit 5442279

Please sign in to comment.