Skip to content

Commit

Permalink
fix: #3342 hexadecimal input not turned into a bigint
Browse files Browse the repository at this point in the history
  • Loading branch information
josdejong committed Jan 8, 2025
1 parent 29abd17 commit 646126d
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
12 changes: 10 additions & 2 deletions src/utils/number.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions test/unit-tests/expression/parse.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down

0 comments on commit 646126d

Please sign in to comment.