( x: Complex | BigDecimal | ExpressionInput | undefined )
| 76 | return bn / bd; |
| 77 | } |
| 78 | |
| 79 | export function asBigint( |
| 80 | x: Complex | BigDecimal | ExpressionInput | undefined |
| 81 | ): bigint | null { |
| 82 | if (x === undefined || x === null) return null; |
| 83 | |
| 84 | if (typeof x === 'bigint') return x; |
| 85 | if (typeof x === 'number' && Number.isInteger(x)) return BigInt(x); |
| 86 | |
| 87 | if (isExpression(x)) { |
| 88 | if (!isNumber(x)) return null; |
| 89 | const num = x.numericValue; |
| 90 | |
| 91 | if (typeof num === 'number') { |
| 92 | if (Number.isInteger(num)) return BigInt(num); |
| 93 | return null; |
| 94 | } |
| 95 | |
| 96 | // Extract the exact integer without a precision-limited round-trip. |
| 97 | const exact = exactIntegerValue(num); |
| 98 | if (exact !== null) return exact; |
| 99 | |
| 100 | if (num.im !== 0) return null; |
| 101 | |
| 102 | // Not an exact integer: only accept a genuine integer-valued float. |
| 103 | if (!Number.isInteger(num.re)) return null; |
| 104 | |
| 105 | return BigInt(num.re); |
| 106 | } |
| 107 | |
| 108 | if (x instanceof BigDecimal || typeof x === 'string') return bigint(x); |
| 109 | |
| 110 | if (x instanceof Complex) { |
| 111 | if (x.im === 0) return bigint(x.re); |
| 112 | return null; |
| 113 | } |
| 114 | |
| 115 | return bigintValue(x as MathJsonExpression); |
| 116 | } |
| 117 | |
| 118 | export function asBignum(expr: Expression | undefined): BigDecimal | null { |
no test coverage detected