Skip to content

Instantly share code, notes, and snippets.

@mathieucaroff
Last active February 27, 2023 14:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mathieucaroff/882677e76839123b6cf8955568ab322a to your computer and use it in GitHub Desktop.
Save mathieucaroff/882677e76839123b6cf8955568ab322a to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>JS conversion to number</title>
<style>
table {
border-collapse: collapse;
}
tr:nth-child(odd) {
background: #fafafa;
}
th {
text-align: left;
font-family: monospace;
font-weight: 900;
font-size: 14px;
}
td {
border: 1px solid #e0e0e0;
padding: 5px;
font: 12px monospace;
}
td:not(:first-child) {
text-align: right;
}
thead td {
background: #3663ae;
color: white;
}
</style>
</head>
<body>
<script src="main.ts" type="module"></script>
</body>
</html>
const EXPRESSION_ARRAY = [
['parseInt(x)', (x) => parseInt(x), 40],
['parseFloat(x)', (x) => parseFloat(x), 5],
['Number(x)', (x) => Number(x), 13],
['+x', (x) => +x, 2],
['~~x', (x) => ~~x, 0],
['x >>> 0', (x) => x >>> 0, 0],
['isNaN(x)', (x) => isNaN(x), 3],
['Number.isNaN(x)', (x) => Number.isNaN(x), 0],
] as const
const VALUE_ARRAY = [
['"123"', '123'],
['"+123"', '+123'],
['"-123"', '-123'],
['"123.45"', '123.45'],
['"-123.45"', '-123.45'],
['"12e5"', '12e5'],
['"12e-5"', '12e-5'],
[],
['"0123"', '0123'],
['"0000123"', '0000123'],
['"0b111"', '0b111'],
['"0o10"', '0o10'],
['"0xBABE"', '0xBABE'],
[],
['"4294967295"', '4294967295'],
['"123456789012345678"', '123456789012345678'],
['"12e999"', '12e999'],
[],
['""', ''],
['"123foo"', '123foo'],
['"123.45foo"', '123.45foo'],
['" 123 "', ' 123 '],
['"foo"', 'foo'],
['"12e"', '12e'],
['"0b567"', '0b567'],
['"0o999"', '0o999'],
['"0xFUZZ"', '0xFUZZ'],
[],
['"+0"', '+0'],
['"-0"', '-0'],
['"Infinity"', 'Infinity'],
['"+Infinity"', '+Infinity'],
['"-Infinity"', '-Infinity'],
['BigInt(1)', BigInt(1)],
[],
['null', null],
['undefined', undefined],
['true', true],
['false', false],
['Infinity', Infinity],
['NaN', NaN],
[],
['{}', {}],
[
'{valueOf: function f(){return 42}}',
{
valueOf: function f() {
return 42
},
},
],
[
'{toString: function f(){return "56"}}',
{
toString: function f() {
return '56'
},
},
],
] as const
document.body.appendChild(
h('table', {}, [
h('tr', {}, [
h('th', { textContent: 'Method stats ->' }),
...EXPRESSION_ARRAY.map(([_exp, _call, value]) =>
h('th', {}, [h('input', { value })])
),
]),
h('tr', {}, [
h('th', { textContent: 'Value v' }),
...EXPRESSION_ARRAY.map(([expression]) =>
h('th', { textContent: expression }, [])
),
]),
...VALUE_ARRAY.map(([text, value]) =>
text === undefined
? h('tr', {}, [h('td')])
: h('tr', {}, [
h('th', { textContent: text }),
...EXPRESSION_ARRAY.map(([_expression, callback]) =>
h('td', {}, [evaluate(value, callback)])
),
])
),
])
)
function evaluate(input, callback: (s) => any) {
let result
let output
try {
result = callback(input)
output = h('span', { textContent: result })
} catch (e) {
output = h('span', { textContent: 'Error' })
result = NaN
}
if (result === true) {
output.style.fontWeight = 900
output.style.color = '#007700'
}
if (Object.is(result, NaN)) {
output.style.color = 'red'
}
return output
}
function h(
name,
props: { textContent?: string; value?: string | number } = {},
children: HTMLElement[] = []
) {
const element = document.createElement(name)
element.textContent = props.textContent
element.value = props.value
children.forEach((child) => {
element.appendChild(child)
})
return element
}
Value v \ Method >parseInt(x)parseFloat(x)Number(x)+x~~xx >>> 0isNaN(x)Number.isNaN(x)
"123"123123123123123123falsefalse
"+123"123123123123123123falsefalse
"-123"-123-123-123-123-1234294967173falsefalse
"123.45"123123.45123.45123.45123123falsefalse
"-123.45"-123-123.45-123.45-123.45-1234294967173falsefalse
"12e5"1212000001200000120000012000001200000falsefalse
"12e-5"120.000120.000120.0001200falsefalse
"0123"123123123123123123falsefalse
"0000123"123123123123123123falsefalse
"0b111"007777falsefalse
"0o10"008888falsefalse
"0xBABE"47806047806478064780647806falsefalse
"4294967295"4294967295429496729542949672954294967295-14294967295falsefalse
"123456789012345678"123456789012345680123456789012345680123456789012345680123456789012345680-15067414242788225872falsefalse
"12e999"12InfinityInfinityInfinity00falsefalse
""NaNNaN0000falsefalse
"123foo"123123NaNNaN00truefalse
"123.45foo"123123.45NaNNaN00truefalse
" 123 "123123123123123123falsefalse
"foo"NaNNaNNaNNaN00truefalse
"12e"1212NaNNaN00truefalse
"0b567"00NaNNaN00truefalse
"0o999"00NaNNaN00truefalse
"0xFUZZ"150NaNNaN00truefalse
"+0"000000falsefalse
"-0"000000falsefalse
"Infinity"NaNInfinityInfinityInfinity00falsefalse
"+Infinity"NaNInfinityInfinityInfinity00falsefalse
"-Infinity"NaN-Infinity-Infinity-Infinity00falsefalse
BigInt(1)111Error1ErrorErrorfalse
nullNaNNaN0000falsefalse
undefinedNaNNaNNaNNaN00truefalse
trueNaNNaN1111falsefalse
falseNaNNaN0000falsefalse
InfinityNaNInfinityInfinityInfinity00falsefalse
NaNNaNNaNNaNNaN00truetrue
{}NaNNaNNaNNaN00truefalse
{valueOf: function f(){return 42}}NaNNaN42424242falsefalse
{toString: function f(){return "56"}}565656565656falsefalse
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment