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

Function number

packages/effect/src/internal/jsonParser.ts:106–151  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

104 }
105
106 const number = () => {
107 if (!json5) {
108 const start = i
109 if (run(jsonNumber) === 0) fail(i)
110 return Number(text.slice(start, i))
111 }
112 let code = text.charCodeAt(i)
113 let sign = 1
114 if (code === 0x2b || code === 0x2d) {
115 if (code === 0x2d) sign = -1
116 code = text.charCodeAt(++i)
117 }
118 const start = i
119 if (code === 0x49) return word("Infinity", sign * Infinity)
120 if (code === 0x4e) return word("NaN", NaN)
121 if (code === 0x30 && (text.charCodeAt(i + 1) | 0x20) === 0x78) {
122 i += 2
123 if (run(hexDigits) === 0) fail(i)
124 return sign * Number(text.slice(start, i))
125 }
126 // Integers below 2^53 are accumulated directly; anything else goes through Number.
127 let integer = 0
128 if (code === 0x30) code = text.charCodeAt(++i)
129 else if (isDigit(code)) {
130 do {
131 integer = integer * 10 + code - 0x30
132 code = text.charCodeAt(++i)
133 } while (isDigit(code))
134 } else if (code !== 0x2e) fail(i)
135 if (code !== 0x2e && code !== 0x65 && code !== 0x45 && i - start < 16) return sign * integer
136 if (code === 0x2e) {
137 const fraction = ++i
138 while (isDigit(text.charCodeAt(i))) i++
139 // A leading decimal point needs digits after it; a trailing one does not.
140 if (i === fraction && fraction === start + 1) fail(i)
141 code = text.charCodeAt(i)
142 }
143 if (code === 0x65 || code === 0x45) {
144 code = text.charCodeAt(++i)
145 if (code === 0x2b || code === 0x2d) i++
146 const exponent = i
147 while (isDigit(text.charCodeAt(i))) i++
148 if (i === exponent) fail(i)
149 }
150 return sign * Number(text.slice(start, i))
151 }
152
153 const string = (quote: number) => {
154 const plain = quote === 0x27 ? json5Single : json5 ? json5Double : jsonDouble

Callers 2

valueFunction · 0.70
dateTime.tsFile · 0.70

Calls 6

sliceMethod · 0.80
runFunction · 0.70
failFunction · 0.70
wordFunction · 0.70
isDigitFunction · 0.70
NumberFunction · 0.50

Tested by

no test coverage detected