From abca18e75c795eb6d8b3fff46051496cb09e2097 Mon Sep 17 00:00:00 2001 From: John Gee Date: Fri, 6 Jan 2023 16:13:53 +1300 Subject: [PATCH] Ignore number-like strings with huge values --- index.js | 1 + test/num.js | 7 ++++++- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index 536fc5b..1a2b071 100644 --- a/index.js +++ b/index.js @@ -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); } diff --git a/test/num.js b/test/num.js index 074393e..ca8c710 100644 --- a/test/num.js +++ b/test/num.js @@ -10,7 +10,9 @@ test('nums', function (t) { '-z', '1e7', '-w', '10f', '--hex', '0xdeadbeef', + '--id', '1234e5678', '789', + '1234e5678', ]); t.deepEqual(argv, { x: 1234, @@ -18,14 +20,17 @@ test('nums', function (t) { 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(); });