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

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
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
42 changes: 38 additions & 4 deletions src/utils/number.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,41 @@ 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)
}

/**
* Check if a string contains a hex number like "0xA2"
* @param {string} str
* @return {boolean} isInteger
*/
export function isHexStr (str) {
return /^0x[0-9A-Fa-f]+$/.test(str)
}

/**
* Check if a string contains a binary number like "0b1011"
* @param {string} str
* @return {boolean} isInteger
*/
export function isBinStr (str) {
return /^0b[01]+$/.test(str)
}

/**
* Check if a string contains an octal number like "0o70"
* @param {string} str
* @return {boolean} isInteger
*/
export function isOctStr (str) {
return /^0o[0-7]+$/.test(str)
}

/**
* Ensure the number type is compatible with the provided value.
* If not, return 'number' instead.
Expand All @@ -48,8 +74,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') {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The following definition of safeNumberType passes all tests:

export function safeNumberType (numberStr, config) {
  if (config.number === 'bigint') {
    try {
      BigInt(numberStr)
    } catch {
      return config.numberFallback
    }
  }

  return config.number
}

If we adopted this, we would not need the three new "isXXXStr" functions, and we would be future-proofing mathjs against any other integer notations. Would you like to switch to this? If so, will you do the refactoring or would you like me to? Just let me know.

const canParseIntoBigint =
isIntegerStr(numberStr) ||
isHexStr(numberStr) ||
isBinStr(numberStr) ||
isOctStr(numberStr)

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
Loading