-
-
Notifications
You must be signed in to change notification settings - Fork 81
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support propertyNames option in Object.copy
- Loading branch information
Showing
2 changed files
with
33 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" }); | ||
}; |