Skip to content

Commit

Permalink
fix: #3342 hexadecimal input not turned into a bigint (#3348)
Browse files Browse the repository at this point in the history
  • Loading branch information
josdejong authored Jan 24, 2025
1 parent 29abd17 commit b5d635e
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
11 changes: 7 additions & 4 deletions src/utils/number.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,11 @@ export function isInteger (value) {
}

/**
* Check if a string contains an integer
* Check if a string contains an integer number like "123" and "-123"
* @param {string} str
* @return {boolean} isInteger
*/
export function isIntegerStr (str) {
// regex matching strings like "123" and "-123"
return /^-?\d+$/.test(str)
}

Expand All @@ -48,8 +47,12 @@ 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') {
try {
BigInt(numberStr)
} catch {
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 b5d635e

Please sign in to comment.