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

Fix: #3342 hexadecimal input not turned into a bigint #3348

Merged
merged 3 commits into from
Jan 24, 2025
Merged
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
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') {
josdejong marked this conversation as resolved.
Show resolved Hide resolved
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
Loading