Skip to content

Commit

Permalink
Object.assign: Make sure non-enumerable Symbols are excluded.
Browse files Browse the repository at this point in the history
  • Loading branch information
ljharb committed Apr 12, 2015
1 parent fa3ca7d commit db7d6a4
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 6 deletions.
8 changes: 6 additions & 2 deletions es6-shim.js
Original file line number Diff line number Diff line change
Expand Up @@ -935,10 +935,14 @@
});

if (supportsDescriptors) {
var isEnumerableOn = Function.bind.call(Function.bind, Object.prototype.propertyIsEnumerable);
var assignReducer = function (target, source) {
var keys = Object.keys(Object(source));
var symbols = ES.IsCallable(Object.getOwnPropertySymbols) ? Object.getOwnPropertySymbols(Object(source)) : [];
return keys.concat(symbols).reduce(function (target, key) {
var symbols;
if (ES.IsCallable(Object.getOwnPropertySymbols)) {
symbols = Object.getOwnPropertySymbols(Object(source)).filter(isEnumerableOn(source));
}
return keys.concat(symbols || []).reduce(function (target, key) {
target[key] = source[key];
return target;
}, target);
Expand Down
11 changes: 7 additions & 4 deletions test/object.js
Original file line number Diff line number Diff line change
Expand Up @@ -220,16 +220,19 @@ describe('Object', function () {
expect(thrower).to.have.property(1, 2);
});

ifSymbolsIt('includes symbols, after keys', function () {
ifSymbolsIt('includes enumerable symbols, after keys', function () {
var visited = [];
var obj = {};
Object.defineProperty(obj, 'a', { get: function () { visited.push('a'); return 42; }, enumerable: true });
var symbol = Symbol();
var symbol = Symbol('enumerable');
Object.defineProperty(obj, symbol, { get: function () { visited.push(symbol); return Infinity; }, enumerable: true });
var nonEnumSymbol = Symbol('non-enumerable');
Object.defineProperty(obj, nonEnumSymbol, { get: function () { visited.push(nonEnumSymbol); return -Infinity; }, enumerable: false });
var target = Object.assign({}, obj);
expect(target[symbol]).to.equal(Infinity);
expect(target.a).to.equal(42);
expect(visited).to.eql(['a', symbol]);
expect(target.a).to.equal(42);
expect(target[symbol]).to.equal(Infinity);
expect(target[nonEnumSymbol]).not.to.equal(-Infinity);
});
});

Expand Down

0 comments on commit db7d6a4

Please sign in to comment.