add function to enforce base 10 decimal sytnax before parsing
This commit is contained in:
parent
e95782eba7
commit
fd1be923b2
@ -36,6 +36,12 @@ function safeBigInt(str, context="number") {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function validateDecimal(input) {
|
||||||
|
// validates optional-sign base-10 decimal input: integers, decimals with optional digits before or after the decimal point, and scientific notation
|
||||||
|
if (!/^[+-]?(?:\d+\.?\d*|\.\d+)(?:e[+-]?\d+)?$/i.test(input))
|
||||||
|
throw new Error("Invalid decimal number");
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Compute 10^exp as BigInt without using ** on BigInt, to avoid
|
* Compute 10^exp as BigInt without using ** on BigInt, to avoid
|
||||||
@ -212,6 +218,8 @@ export function ToIEEE754Float64(input) {
|
|||||||
return `${s} ${expOnes} ${mantZeros}`;
|
return `${s} ${expOnes} ${mantZeros}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
validateDecimal(input);
|
||||||
|
|
||||||
let sign = 0n;
|
let sign = 0n;
|
||||||
if (input.startsWith("-")) {
|
if (input.startsWith("-")) {
|
||||||
sign = 1n;
|
sign = 1n;
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user