Skip to content

Commit

Permalink
Ignore number-like strings with huge values
Browse files Browse the repository at this point in the history
  • Loading branch information
shadowspawn committed Jan 6, 2023
1 parent 5784b17 commit abca18e
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 1 deletion.
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ function hasKey(obj, keys) {

function isNumber(x) {
if (typeof x === 'number') { return true; }
if (!Number.isSafeInteger(Math.floor(x))) { return false; }
if ((/^0x[0-9a-f]+$/i).test(x)) { return true; }
return (/^[-+]?(?:\d+(?:\.\d*)?|\.\d+)(e[-+]?\d+)?$/).test(x);
}
Expand Down
7 changes: 6 additions & 1 deletion test/num.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,27 @@ test('nums', function (t) {
'-z', '1e7',
'-w', '10f',
'--hex', '0xdeadbeef',
'--id', '1234e5678',
'789',
'1234e5678',
]);
t.deepEqual(argv, {
x: 1234,
y: 5.67,
z: 1e7,
w: '10f',
hex: 0xdeadbeef,
_: [789],
id: '1234e5678',
_: [789, '1234e5678'],
});
t.deepEqual(typeof argv.x, 'number');
t.deepEqual(typeof argv.y, 'number');
t.deepEqual(typeof argv.z, 'number');
t.deepEqual(typeof argv.w, 'string');
t.deepEqual(typeof argv.hex, 'number');
t.deepEqual(typeof argv.id, 'string');
t.deepEqual(typeof argv._[0], 'number');
t.deepEqual(typeof argv._[1], 'string');
t.end();
});

Expand Down

0 comments on commit abca18e

Please sign in to comment.