sign(x)

http://stackoverflow.com/questions/7624920/number-sign-in-javascript

by disfated

HTML

<a href="http://stackoverflow.com/questions/7624920/number-sign-in-javascript">about</a> | <a href="#" id="update">update</a>
<table id="table"></table>

CSS

body { margin: 12px; font: sans-serif; }
table { border-collapse: collapse; font: monospace; margin-top: 5px; }
table, th, td { border: 1px dotted #CCC; }
th, td { padding: 2px 10px; text-align: center; }
thead th { padding: 8px 10px; background: #F6F6F6; font-weight: bold; }
td:first-child, th:first-child { text-align: left; }
td:first-child { background: #FCFCFC; }
td.valid { background: #DFF6DF; }
td.invalid { background: #FFB8AE; }

JavaScript

window.addEventListener('DOMContentLoaded', () => {

	const testees = [
		[ 'Math.sign' , Math.sign                                                                 ],
		[ 'moz'       , x => ((x > 0) - (x < 0)) || +x                                            ],
		[ '1'         , x => x > 0 ? 1 : x < 0 ? -1 : 0                                           ],
		[ '1.1'       , x => x ? x < 0 ? -1 : 1 : 0                                               ],
		[ '2'         , x => x && x / Math.abs(x)                                                 ],
		[ '3'         , x => (x > 0 ? 1 : 0) - (x < 0 ? 1 : 0)                                    ],
		[ '4'         , x => (x >> 31) + (x > 0 ? 1 : 0)                                          ],
		[ '5'         , x => typeof x === 'number' ? x ? x < 0 ? -1 : 1 : x === x ? 0 : NaN : NaN ],
	];

	const types = [
		[ '-100'            , -100       ],
		[ '-1'              , -1         ],
		[ '-0'              , -0         ],
		[ '+0'              , +0         ],
		[ '+1'              , +1         ],
		[ '+100'            , +100       ],
		[ '+Infinity'       , +Infinity  ],
		[ '-Infinity'       , -Infinity  ],
		[ 'NaN'             , NaN        ],
		[ 'null'            , null       ],
		[ 'undefined'       , undefined  ],
		[ '{}'              , {}         ],
		[ '[]'              , []         ],
		[ '[1]'             , [1]        ],
		[ '[0]'             , [0]        ],
		[ '[-1]'            , [-1]       ],
		[ '[0,0]'           , [0,0]      ],
		[ '""'              , ''         ],
		[ '"string"'        , 'string'   ],
		[ '"100"'           , '100'      ],
		[ '"0"'             , '0'        ],
		[ '"-100"'          , '-100'     ],
		[ 'true'            , 'true'     ],
		[ 'false'           , 'false'    ],
		[ '[]'              , []         ],
		[ '[-100]'          , [-100]     ],
		[ '[0]'             , [0]        ],
		[ '[100]'           , [100]      ],
		[ 'date'            , new Date() ],
		[ '{}'              , {}         ],
		[...