MCPcopy Create free account
hub / github.com/Effect-TS/effect / fromString

Function fromString

packages/effect/src/BigDecimal.ts:1358–1407  ·  view source on GitHub ↗
(s: string)

Source from the content-addressed store, hash-verified

1356 * @since 2.0.0
1357 */
1358export const fromString = (s: string): Option.Option<BigDecimal> => {
1359 if (s === "") {
1360 return Option.some(zero)
1361 }
1362
1363 let base: string
1364 let exp: number
1365 const seperator = s.search(/[eE]/)
1366 if (seperator !== -1) {
1367 const trail = s.slice(seperator + 1)
1368 base = s.slice(0, seperator)
1369 exp = Number(trail)
1370 if (base === "" || !Number.isSafeInteger(exp) || !FINITE_INT_REGEXP.test(trail)) {
1371 return Option.none()
1372 }
1373 } else {
1374 base = s
1375 exp = 0
1376 }
1377
1378 let digits: string
1379 let offset: number
1380 const dot = base.search(/\./)
1381 if (dot !== -1) {
1382 const lead = base.slice(0, dot)
1383 const trail = base.slice(dot + 1)
1384 if (!/^\d*$/.test(trail)) {
1385 return Option.none()
1386 }
1387 digits = `${lead}${trail}`
1388 offset = trail.length
1389 } else {
1390 digits = base
1391 offset = 0
1392 }
1393
1394 if (!FINITE_INT_REGEXP.test(digits)) {
1395 return Option.none()
1396 }
1397
1398 const scale = offset - exp
1399 const value = BigInt(digits)
1400 // Keeps `format` output parseable
1401 const normalizedScale = value === bigint0 ? scale : scale - trailingZeros(digits)
1402 if (scale > MAX_SCALE || normalizedScale < -MAX_SCALE) {
1403 return Option.none()
1404 }
1405
1406 return Option.some(make(value, scale))
1407}
1408
1409/**
1410 * Parses a decimal string into a `BigDecimal`, throwing if the string is

Callers 2

fromNumberFunction · 0.70
fromStringUnsafeFunction · 0.70

Calls 7

trailingZerosFunction · 0.85
someMethod · 0.80
searchMethod · 0.80
sliceMethod · 0.80
NumberFunction · 0.70
BigIntInterface · 0.70
makeFunction · 0.70

Tested by

no test coverage detected