( expr: MathJsonExpression | null | undefined )
| 4 | import { numberToString } from './strings.js'; |
| 5 | |
| 6 | export function bigintValue( |
| 7 | expr: MathJsonExpression | null | undefined |
| 8 | ): bigint | null { |
| 9 | if (typeof expr === 'number') |
| 10 | return Number.isInteger(expr) ? BigInt(expr) : null; |
| 11 | |
| 12 | if (expr === null || expr === undefined) return null; |
| 13 | |
| 14 | if (!isNumberExpression(expr)) return null; |
| 15 | |
| 16 | const num = isNumberObject(expr) ? expr.num : expr; |
| 17 | |
| 18 | if (typeof num === 'number') |
| 19 | return Number.isInteger(num) ? BigInt(num) : null; |
| 20 | if (typeof num !== 'string') return null; |
| 21 | |
| 22 | const s = num |
| 23 | .toLowerCase() |
| 24 | .replace(/[nd]$/, '') |
| 25 | .replace(/[\u0009-\u000d\u0020\u00a0]/g, ''); |
| 26 | |
| 27 | if (s === 'nan') return null; |
| 28 | if (/^(infinity|\+infinity|oo|\+oo|-infinity|-oo)$/.test(s)) return null; |
| 29 | |
| 30 | return bigint(s); |
| 31 | } |
| 32 | |
| 33 | /** Output a shorthand if possible */ |
| 34 | export function numberToExpression( |
no test coverage detected