Skip to content

Instantly share code, notes, and snippets.

@hdragomir
Last active Jan 1, 2016
Embed
What would you like to do?
<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