Skip to content

Commit

Permalink
Make sure Object.keys does not throw.
Browse files Browse the repository at this point in the history
Per https://people.mozilla.org/~jorendorff/es6-draft.html#sec-object.keys - Object.keys should now accept non-objects, and coerce them to objects.
  • Loading branch information
ljharb committed Feb 26, 2014
1 parent 9b7b748 commit 9349c1f
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
9 changes: 9 additions & 0 deletions es6-shim.js
Original file line number Diff line number Diff line change
Expand Up @@ -834,6 +834,15 @@
}
});

try {
Object.keys('foo');
} catch (e) {
var originalObjectKeys = Object.keys;
Object.keys = function (obj) {
return originalObjectKeys(Object(obj));
};
}

var MathShims = {
acosh: function(value) {
value = Number(value);
Expand Down
12 changes: 12 additions & 0 deletions test/object.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,18 @@
describe('Object', function() {
var undefined;

describe('Object.keys()', function() {
it('works on strings', function() {
expect(Object.keys('foo')).to.eql(['0', '1', '2']);
});

it('works on other primitives', function() {
[true, false, undefined, NaN, 42, /a/g].forEach(function (item) {
expect(Object.keys(item)).to.eql([]);
});
});
});

describe('Object.is()', function() {
it('should compare regular objects correctly', function() {
[null, undefined, [0], 5, 'str', {a: null}].map(function(item) {
Expand Down

0 comments on commit 9349c1f

Please sign in to comment.