Here's a polyfill for the Number
predicate functions:
"use strict";
Number.isNaN = Number.isNaN ||
n => n !== n; // only NaN
Number.isNumeric = Number.isNumeric ||
n => n === +n; // all numbers excluding NaN
Number.isFinite = Number.isFinite ||
n => n === +n // all numbers excluding NaN
&& n >= Number.MIN_VALUE // and -Infinity
&& n <= Number.MAX_VALUE; // and +Infinity
Number.isInteger = Number.isInteger ||
n => n === +n // all numbers excluding NaN
&& n >= Number.MIN_VALUE // and -Infinity
&& n <= Number.MAX_VALUE // and +Infinity
&& !(n % 1); // and non-whole numbers
Number.isSafeInteger = Number.isSafeInteger ||
n => n === +n // all numbers excluding NaN
&& n >= Number.MIN_SAFE_INTEGER // and small unsafe numbers
&& n <= Number.MAX_SAFE_INTEGER // and big unsafe numbers
&& !(n % 1); // and non-whole numbers
All major browsers support these functions, except isNumeric
, which is not in the specification because I made it up. Hence, you can reduce the size of this polyfill:
"use strict";
Number.isNumeric = Number.isNumeric ||
n => n === +n; // all numbers excluding NaN
Alternatively, just inline the expression n === +n
manually.