From 646126d9a4a9c193889f7473c5392f8d337f7315 Mon Sep 17 00:00:00 2001 From: Jos de Jong Date: Wed, 8 Jan 2025 11:32:55 +0100 Subject: [PATCH] fix: #3342 hexadecimal input not turned into a bigint --- src/utils/number.js | 12 ++++++++++-- test/unit-tests/expression/parse.test.js | 6 ++++++ 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/src/utils/number.js b/src/utils/number.js index 5095b71628..6d63570791 100644 --- a/src/utils/number.js +++ b/src/utils/number.js @@ -48,8 +48,16 @@ export function isIntegerStr (str) { * @returns {'number' | 'BigNumber' | 'bigint' | 'Fraction'} */ export function safeNumberType (numberStr, config) { - if (config.number === 'bigint' && !isIntegerStr(numberStr)) { - return config.numberFallback + if (config.number === 'bigint') { + const canParseIntoBigint = + isIntegerStr(numberStr) || + numberStr.startsWith('0x') || + numberStr.startsWith('0b') || + numberStr.startsWith('0o') + + if (!canParseIntoBigint) { + return config.numberFallback + } } return config.number diff --git a/test/unit-tests/expression/parse.test.js b/test/unit-tests/expression/parse.test.js index 92a1871b5f..51651a5adc 100644 --- a/test/unit-tests/expression/parse.test.js +++ b/test/unit-tests/expression/parse.test.js @@ -2248,6 +2248,12 @@ describe('parse', function () { assert.strictEqual(bigmath.evaluate('-2.3'), -2.3) }) + it('should parse hex, bin, oct numbers as bigint', function () { + assert.strictEqual(bigmath.evaluate('0xA2'), 162n) + assert.strictEqual(bigmath.evaluate('0b1011'), 11n) + assert.strictEqual(bigmath.evaluate('0o70'), 56n) + }) + it('should fallback on the configured numberFallback when parsing as bigint', function () { const bigmathFallback = math.create({ number: 'bigint',