Last active
January 1, 2016 12:59
-
-
Save hdragomir/8148600 to your computer and use it in GitHub Desktop.
As per @olov's twitter test: https://twitter.com/olov/status/416577948691726336
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
<script> | |
function abs(x) { | |
x = typeof x === "string" ? x.indexOf(',') > -1 ? Number.NaN : ( x.indexOf('.') > -1 ? parseFloat(x, 10) : parseInt(x, 10) ) : (typeof x === "number" || x === null ? x : Number.NaN); | |
return Number.isNaN(x) ? x : (x === null ? 0 : (x < 0 ? -x : x )); | |
} | |
function assert(message, condition) { | |
condition || console.error(message); | |
} | |
assert("-2.5 === 2.5", abs(-2.5) === 2.5); | |
assert("'2.5' === 2.5", abs("2.5") === 2.5); | |
assert("asd === NaN", Number.isNaN(abs("asd"))); | |
assert("undefined === NaN", Number.isNaN(abs())); | |
assert("null === 0", abs(null) === 0); | |
assert('"2,5" === NaN', Number.isNaN(abs('2,5'))); | |
assert("2e-5 == 2e-5", abs(-2e-5) === 2e-5); | |
assert("{} === NaN", Number.isNaN(abs({}))); | |
</script> | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment