Skip to content

Instantly share code, notes, and snippets.

@Cycymomo
Created May 20, 2014 12:45
Show Gist options
  • Save Cycymomo/5ba5efffa42f7810f657 to your computer and use it in GitHub Desktop.
Save Cycymomo/5ba5efffa42f7810f657 to your computer and use it in GitHub Desktop.
/*\
|*|
|*| :: Number.my_isSafeInteger() polyfill ::
|*|
|*| https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/my_isSafeInteger
|*|
\*/
// 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](https://people.mozilla.org/~jorendorff/es6-draft.html#sec-number.issafeinteger)
// already in Chrome 34+ : [Number.isSafeInteger](https://code.google.com/p/v8/source/browse/branches/bleeding_edge/src/v8natives.js?r=18480#1655)
/* 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](https://people.mozilla.org/~jorendorff/es6-draft.html#sec-number.isinteger)
// already in Chrome 34+ : [Number.isInteger](https://code.google.com/p/v8/source/browse/branches/bleeding_edge/src/v8natives.js?r=18480#1642)
/* 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"));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment