Add a `Number.isNaN` polyfill

Reviewed By: sahrens

Differential Revision: D3222299

fb-gh-sync-id: 601283fb0b140bb305181ea381907e62286f7a37
fbshipit-source-id: 601283fb0b140bb305181ea381907e62286f7a37
This commit is contained in:
Steven Luscher 2016-04-25 23:14:45 -07:00 committed by Facebook Github Bot 8
parent 6f43d1f2cb
commit c05169d8b7
4 changed files with 85 additions and 1 deletions

View File

@ -143,12 +143,21 @@ describe('Resolver', function() {
'polyfills/console.js'
],
},
{ id: 'polyfills/Number.es6.js',
file: 'polyfills/Number.es6.js',
dependencies: [
'polyfills/polyfills.js',
'polyfills/console.js',
'polyfills/error-guard.js'
],
},
{ id: 'polyfills/String.prototype.es6.js',
file: 'polyfills/String.prototype.es6.js',
dependencies: [
'polyfills/polyfills.js',
'polyfills/console.js',
'polyfills/error-guard.js'
'polyfills/error-guard.js',
'polyfills/Number.es6.js',
],
},
{ id: 'polyfills/Array.prototype.es6.js',
@ -157,6 +166,7 @@ describe('Resolver', function() {
'polyfills/polyfills.js',
'polyfills/console.js',
'polyfills/error-guard.js',
'polyfills/Number.es6.js',
'polyfills/String.prototype.es6.js',
],
},
@ -166,6 +176,7 @@ describe('Resolver', function() {
'polyfills/polyfills.js',
'polyfills/console.js',
'polyfills/error-guard.js',
'polyfills/Number.es6.js',
'polyfills/String.prototype.es6.js',
'polyfills/Array.prototype.es6.js',
],
@ -176,6 +187,7 @@ describe('Resolver', function() {
'polyfills/polyfills.js',
'polyfills/console.js',
'polyfills/error-guard.js',
'polyfills/Number.es6.js',
'polyfills/String.prototype.es6.js',
'polyfills/Array.prototype.es6.js',
'polyfills/Array.es6.js',
@ -187,6 +199,7 @@ describe('Resolver', function() {
'polyfills/polyfills.js',
'polyfills/console.js',
'polyfills/error-guard.js',
'polyfills/Number.es6.js',
'polyfills/String.prototype.es6.js',
'polyfills/Array.prototype.es6.js',
'polyfills/Array.es6.js',
@ -251,6 +264,7 @@ describe('Resolver', function() {
'polyfills/polyfills.js',
'polyfills/console.js',
'polyfills/error-guard.js',
'polyfills/Number.es6.js',
'polyfills/String.prototype.es6.js',
'polyfills/Array.prototype.es6.js',
'polyfills/Array.es6.js',

View File

@ -177,6 +177,7 @@ class Resolver {
path.join(__dirname, 'polyfills/polyfills.js'),
path.join(__dirname, 'polyfills/console.js'),
path.join(__dirname, 'polyfills/error-guard.js'),
path.join(__dirname, 'polyfills/Number.es6.js'),
path.join(__dirname, 'polyfills/String.prototype.es6.js'),
path.join(__dirname, 'polyfills/Array.prototype.es6.js'),
path.join(__dirname, 'polyfills/Array.es6.js'),

View File

@ -0,0 +1,24 @@
/**
* Copyright (c) 2013-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
* @provides Number.es6
* @polyfill
*/
if (!Number.isNaN) {
// https://github.com/dherman/tc39-codex-wiki/blob/master/data/es6/number/index.md#polyfill-for-numberisnan
const globalIsNaN = global.isNaN;
Object.defineProperty(Number, 'isNaN', {
configurable: true,
enumerable: false,
value: function isNaN(value) {
return typeof value === 'number' && globalIsNaN(value);
},
writable: true,
});
}

View File

@ -0,0 +1,45 @@
/**
* Copyright (c) 2013-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
* @emails oncall+jsinfra
*/
jest.autoMockOff();
describe('Number (ES6)', () => {
describe('isNaN()', () => {
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/isNaN#Examples
beforeEach(() => {
delete Number.isNaN;
jest.resetModuleRegistry();
require('../Number.es6');
});
it('returns true when fed something that is not-a-number', () => {
[
NaN,
Number.NaN,
0 / 0,
].forEach(value => expect(Number.isNaN(value)).toBe(true));
});
it('returns false when fed something other than not-a-number', () => {
[
'NaN',
undefined,
{},
'blabla',
true,
null,
37,
'37',
'37.37',
'',
' ',
].forEach(value => expect(Number.isNaN(value)).toBe(false));
});
});
});