Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ignore numeric-looking strings with huge values #18

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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; }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

isSafeInteger isn’t available in all the engines we support.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. I looked up when isSafeInteger was introduced but then forgot to check the oldest supported version for Minimist, or lack of an explicit minimum. Realised when I saw the test fail.

The PR isn't a no-brainer so I'll only try and make it work with older nodes if otherwise of interest?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (!Number.isSafeInteger(Math.floor(x))) { return false; }
if (x >= Math.pow(2, 53)) { return false; }

i think this would do it?

My concern is whether this bugfix could arguably be considered a breaking change, in which case it wouldn't likely be worth it.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, being conservative it is arguably a breaking change, and it isn't a full solution.

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