Skip to content

Commit

Permalink
Implemented Reflect.ownKeys, and added more tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
Victorystick committed Jan 4, 2015
1 parent da3d2a6 commit 5a19eb4
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 5 deletions.
20 changes: 18 additions & 2 deletions es6-shim.js
Original file line number Diff line number Diff line change
Expand Up @@ -2346,8 +2346,24 @@

isExtensible: throwUnlessTargetIsObject(Object.isExtensible),

// Different name.
ownKeys: throwUnlessTargetIsObject(Object.keys),
// Basically the result of calling the internal [[OwnPropertyKeys]].
// Concatenating propertyNames and propertySymbols should do the trick.
// This should continue to work together with a Symbol shim
// which overrides Object.getOwnPropertyNames and implements
// Object.getOwnPropertySymbols.
ownKeys: function ownKeys(target) {
if (!ES.TypeIsObject(target)) {
throw new TypeError('target must be an object');
}

var keys = Object.getOwnPropertyNames(target);

if (ES.IsCallable(Object.getOwnPropertySymbols)) {
keys = keys.concat(Object.getOwnPropertySymbols(target));
}

return keys;
},

preventExtensions: wrapObjectFunction(Object.preventExtensions),

Expand Down
23 changes: 20 additions & 3 deletions test/reflect.js
Original file line number Diff line number Diff line change
Expand Up @@ -366,10 +366,27 @@ describe('Reflect', function () {
});
});

it('should return the same result as Object.keys()', function () {
var obj = { foo: 1, bar: 2};
it('should return the same result as Object.getOwnPropertyNames if there are no Symbols', function () {
var obj = { foo: 1, bar: 2 };

expect(Reflect.ownKeys(obj)).to.deep.equal(Object.keys(obj));
obj[1] = 'first';

var result = Object.getOwnPropertyNames(obj);

expect(Reflect.ownKeys(obj)).to.deep.equal(result);
expect(result).to.deep.equal(['1', 'foo', 'bar']);
});

(typeof Symbol === 'function' ? it : xit)('symbols come last', function () {
var s = Symbol();

var o = {
'non-symbol': true
};

o[s] = true;

expect(Reflect.ownKeys(o)).to.deep.equal(['non-symbol', s]);
});
});

Expand Down

0 comments on commit 5a19eb4

Please sign in to comment.