1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
| /*\
|*|
|*| :: Number.my_isSafeInteger() polyfill ::
|*|
|*| <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/my_isSafeInteger" target="_blank">https://developer.mozilla.org/en-US/..._isSafeInteger</a>
|*|
\*/
// Note, the MDN polyfill is actually isSafeInteger polyfill
function my_isSafeInteger(n) {
return typeof n === "number" && isFinite(n) && n >= (Number.MIN_SAFE_INTEGER || -9007199254740991) && n <= (Number.MAX_SAFE_INTEGER || 9007199254740992) && Math.floor(n) === n;
}
// [ES6 proposal](<a href="https://people.mozilla.org/~jorendorff/es6-draft.html#sec-number.issafeinteger" target="_blank">https://people.mozilla.org/~jorendor....issafeinteger</a>)
// already in Chrome 34+ : [Number.isSafeInteger](<a href="https://code.google.com/p/v8/source/browse/branches/bleeding_edge/src/v8natives.js?r=18480#1655" target="_blank">https://code.google.com/p/v8/source/...s?r=18480#1655</a>)
/* These tests must be valid */
// Integers
console.assert(my_isSafeInteger(4597));
console.assert(my_isSafeInteger(8e3));
console.assert(my_isSafeInteger(-0));
console.assert(my_isSafeInteger(+0));
// !my_isSafeInteger
console.assert(!my_isSafeInteger("4598"));
console.assert(!my_isSafeInteger(Number.MAX_VALUE));
console.assert(!my_isSafeInteger(Infinity));
console.assert(!my_isSafeInteger(NaN));
console.assert(!my_isSafeInteger({}));
console.assert(!my_isSafeInteger(false));
console.assert(!my_isSafeInteger(true));
console.assert(!my_isSafeInteger(null));
console.assert(!my_isSafeInteger(undefined));
console.assert(!my_isSafeInteger("lol"));
console.assert(!my_isSafeInteger("9.489"));
console.assert(!my_isSafeInteger("4,579,312"));
console.assert(!my_isSafeInteger("5,2"));
console.assert(!my_isSafeInteger("8e3"));
console.assert(!my_isSafeInteger(".5"));
// my isInteger implementation
function my_isInteger(n) {
return typeof n === "number" && isFinite(n) && Math.floor(n) === n;
}
// [ES6 proposal](<a href="https://people.mozilla.org/~jorendorff/es6-draft.html#sec-number.isinteger" target="_blank">https://people.mozilla.org/~jorendor...mber.isinteger</a>)
// already in Chrome 34+ : [Number.isInteger](<a href="https://code.google.com/p/v8/source/browse/branches/bleeding_edge/src/v8natives.js?r=18480#1642" target="_blank">https://code.google.com/p/v8/source/...s?r=18480#1642</a>)
/* These tests must be valid */
// Integers
console.assert(my_isInteger(4597));
console.assert(my_isInteger(8e3));
console.assert(my_isInteger(Number.MAX_VALUE));
console.assert(my_isInteger(-0));
console.assert(my_isInteger(+0));
// !my_isInteger
console.assert(!my_isInteger("4598"));
console.assert(!my_isInteger(Infinity));
console.assert(!my_isInteger(NaN));
console.assert(!my_isInteger({}));
console.assert(!my_isInteger(false));
console.assert(!my_isInteger(true));
console.assert(!my_isInteger(null));
console.assert(!my_isInteger(undefined));
console.assert(!my_isInteger("lol"));
console.assert(!my_isInteger("9.489"));
console.assert(!my_isInteger("4,579,312"));
console.assert(!my_isInteger("5,2"));
console.assert(!my_isInteger("8e3"));
console.assert(!my_isInteger(".5")); |