Skip to content

Commit

Permalink
Merge pull request #70 from jurca/master
Browse files Browse the repository at this point in the history
Exclude non-enumerable symbol-named object properties from cloning
  • Loading branch information
pvorb authored Nov 9, 2016
2 parents 1ca5f49 + 02423ac commit 0a9bb14
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
4 changes: 4 additions & 0 deletions clone.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,10 @@ function clone(parent, circular, depth, prototype) {
// Don't need to worry about cloning a symbol because it is a primitive,
// like a number or string.
var symbol = symbols[i];
var descriptor = Object.getOwnPropertyDescriptor(parent, symbol);
if (descriptor && !descriptor.enumerable) {
continue;
}
child[symbol] = _clone(parent[symbol], depth - 1);
}
}
Expand Down
30 changes: 29 additions & 1 deletion test.js
Original file line number Diff line number Diff line change
Expand Up @@ -511,4 +511,32 @@ if (nativePromise) {
test.done();
});
}
}
}

var nativeSymbol;
try {
nativeSymbol = Symbol
} catch(_) {}
if (nativeSymbol) {
exports["clone only enumerable symbol properties"] = function (test) {
test.expect(3);

var source = {};
var symbol1 = nativeSymbol('the first symbol');
var symbol2 = nativeSymbol('the second symbol');
var symbol3 = nativeSymbol('the third symbol');
source[symbol1] = 1;
source[symbol2] = 2;
source[symbol3] = 3;
Object.defineProperty(source, symbol2, {
enumerable: false
});

var cloned = clone(source);
test.equal(cloned[symbol1], 1);
test.equal(cloned.hasOwnProperty(symbol2), false);
test.equal(cloned[symbol3], 3);

test.done();
};
}

0 comments on commit 0a9bb14

Please sign in to comment.