(value, name)
| 121 | * a //number//, then an ArgumentError will be thrown for %%name%%. |
| 122 | */ |
| 123 | export function getNumber(value, name) { |
| 124 | switch (typeof (value)) { |
| 125 | case "bigint": |
| 126 | assertArgument(value >= -maxValue && value <= maxValue, "overflow", name || "value", value); |
| 127 | return Number(value); |
| 128 | case "number": |
| 129 | assertArgument(Number.isInteger(value), "underflow", name || "value", value); |
| 130 | assertArgument(value >= -maxValue && value <= maxValue, "overflow", name || "value", value); |
| 131 | return value; |
| 132 | case "string": |
| 133 | try { |
| 134 | if (value === "") { |
| 135 | throw new Error("empty string"); |
| 136 | } |
| 137 | return getNumber(BigInt(value), name); |
| 138 | } |
| 139 | catch (e) { |
| 140 | assertArgument(false, `invalid numeric string: ${e.message}`, name || "value", value); |
| 141 | } |
| 142 | } |
| 143 | assertArgument(false, "invalid numeric value", name || "value", value); |
| 144 | } |
| 145 | /** |
| 146 | * Converts %%value%% to a number. If %%value%% is a Uint8Array, it |
| 147 | * is treated as Big Endian data. Throws if the value is not safe. |
no test coverage detected