Created
August 7, 2018 00:53
-
-
Save mr21/097fa551e26b41f9f2539ed46215091d to your computer and use it in GitHub Desktop.
Math.min/max/round/... check for NaN
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
( function() { | |
const _Math = { | |
min: Math.min, | |
max: Math.max, | |
abs: Math.abs, | |
ceil: Math.ceil, | |
floor: Math.floor, | |
round: Math.round, | |
}; | |
Math.min = mathCall.bind( null, "min" ); | |
Math.max = mathCall.bind( null, "max" ); | |
Math.abs = mathCall.bind( null, "abs" ); | |
Math.ceil = mathCall.bind( null, "ceil" ); | |
Math.floor = mathCall.bind( null, "floor" ); | |
Math.round = mathCall.bind( null, "round" ); | |
function mathCall( fn ) { | |
const arr = Array.from( arguments ), | |
_ = arr.shift(), | |
res = _Math[ fn ].apply( Math, arr ); | |
if ( isNaN( res ) ) { | |
console.warn( `NaN detected on Math.${ fn }( ${ arr } )` ); | |
} | |
return res; | |
}; | |
} )(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment