MCPcopy Index your code
hub / github.com/Effect-TS/effect / fromString

Function fromString

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

Source from the content-addressed store, hash-verified

892 * @category constructors
893 */
894export const fromString = (s: string): Option.Option<BigDecimal> => {
895 if (s === "") {
896 return Option.some(zero)
897 }
898
899 let base: string
900 let exp: number
901 const seperator = s.search(/[eE]/)
902 if (seperator !== -1) {
903 const trail = s.slice(seperator + 1)
904 base = s.slice(0, seperator)
905 exp = Number(trail)
906 if (base === "" || !Number.isSafeInteger(exp) || !FINITE_INT_REGEX.test(trail)) {
907 return Option.none()
908 }
909 } else {
910 base = s
911 exp = 0
912 }
913
914 let digits: string
915 let offset: number
916 const dot = base.search(/\./)
917 if (dot !== -1) {
918 const lead = base.slice(0, dot)
919 const trail = base.slice(dot + 1)
920 digits = `${lead}${trail}`
921 offset = trail.length
922 } else {
923 digits = base
924 offset = 0
925 }
926
927 if (!FINITE_INT_REGEX.test(digits)) {
928 return Option.none()
929 }
930
931 const scale = offset - exp
932 if (!Number.isSafeInteger(scale)) {
933 return Option.none()
934 }
935
936 return Option.some(make(BigInt(digits), scale))
937}
938
939/**
940 * Parses a numerical `string` into a `BigDecimal`.

Callers 2

safeFromNumberFunction · 0.70
unsafeFromStringFunction · 0.70

Calls 2

NumberInterface · 0.85
makeFunction · 0.70

Tested by

no test coverage detected