(s: string)
| 1275 | * @since 2.0.0 |
| 1276 | */ |
| 1277 | export const fromString = (s: string): Option.Option<BigDecimal> => { |
| 1278 | if (s === "") { |
| 1279 | return Option.some(zero) |
| 1280 | } |
| 1281 | |
| 1282 | let base: string |
| 1283 | let exp: number |
| 1284 | const seperator = s.search(/[eE]/) |
| 1285 | if (seperator !== -1) { |
| 1286 | const trail = s.slice(seperator + 1) |
| 1287 | base = s.slice(0, seperator) |
| 1288 | exp = Number(trail) |
| 1289 | if (base === "" || !Number.isSafeInteger(exp) || !FINITE_INT_REGEXP.test(trail)) { |
| 1290 | return Option.none() |
| 1291 | } |
| 1292 | } else { |
| 1293 | base = s |
| 1294 | exp = 0 |
| 1295 | } |
| 1296 | |
| 1297 | let digits: string |
| 1298 | let offset: number |
| 1299 | const dot = base.search(/\./) |
| 1300 | if (dot !== -1) { |
| 1301 | const lead = base.slice(0, dot) |
| 1302 | const trail = base.slice(dot + 1) |
| 1303 | digits = `${lead}${trail}` |
| 1304 | offset = trail.length |
| 1305 | } else { |
| 1306 | digits = base |
| 1307 | offset = 0 |
| 1308 | } |
| 1309 | |
| 1310 | if (!FINITE_INT_REGEXP.test(digits)) { |
| 1311 | return Option.none() |
| 1312 | } |
| 1313 | |
| 1314 | const scale = offset - exp |
| 1315 | if (!Number.isSafeInteger(scale)) { |
| 1316 | return Option.none() |
| 1317 | } |
| 1318 | |
| 1319 | return Option.some(make(BigInt(digits), scale)) |
| 1320 | } |
| 1321 | |
| 1322 | /** |
| 1323 | * Parses a decimal string into a `BigDecimal`, throwing if the string is |
no test coverage detected