Skip to content

Commit

Permalink
fix(3258): handle no prototype objects in R.clone (ramda#3261)
Browse files Browse the repository at this point in the history
* fix(3258): handle no prototype objects in R.clone

Fixes: ramda#3258

* simplify hasOwnProperty, toString, and test for clone
  • Loading branch information
thefourtheye authored Apr 6, 2022
1 parent ea8c8b1 commit beafc3c
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
4 changes: 2 additions & 2 deletions source/internal/_clone.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export default function _clone(value, deep, map) {
map.set(value, copiedValue);

for (var key in value) {
if (value.hasOwnProperty(key)) {
if (Object.prototype.hasOwnProperty.call(value, key)) {
copiedValue[key] = deep ? _clone(value[key], true, map) : value[key];
}
}
Expand Down Expand Up @@ -83,7 +83,7 @@ _ObjectMap.prototype.set = function(key, value) {
_ObjectMap.prototype.hash = function(key) {
let hashedKey = [];
for (var value in key) {
hashedKey.push(key[value]);
hashedKey.push(Object.prototype.toString.call(key[value]));
}
return hashedKey.join();
};
Expand Down
13 changes: 13 additions & 0 deletions test/clone.js
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,19 @@ describe('deep clone edge cases', function() {

});

describe('clone objects with no prototypes', function() {
it('nested object with no prototype', function() {
const obj = Object.create(null);
obj.intValue = 1;
obj.a = Object.create(null);
obj.a.stringValue = 'Yeah';
const clonedObj = R.clone(obj);
assert.strictEqual(Object.getPrototypeOf(clonedObj), null);
assert.strictEqual(Object.getPrototypeOf(clonedObj.a), null);
assert.strictEqual(clonedObj.intValue, 1);
assert.strictEqual(clonedObj.a.stringValue, 'Yeah');
});
});

describe('Let `R.clone` use an arbitrary user defined `clone` method', function() {

Expand Down

0 comments on commit beafc3c

Please sign in to comment.