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

Function parseScalar

packages/effect/src/unstable/encoding/Yaml.ts:152–183  ·  view source on GitHub ↗
(input: string)

Source from the content-addressed store, hash-verified

150}
151
152const parseScalar = (input: string): unknown => {
153 const value = input.trim()
154 if (value.length === 0) return null
155 if (value.startsWith("\"") && value.endsWith("\"")) {
156 return parseDoubleQuoted(value)
157 }
158 if (value.startsWith("'") && value.endsWith("'")) {
159 return value.slice(1, -1).replace(/''/g, "'")
160 }
161 if (/^(?:null|~)$/i.test(value)) return null
162 if (/^true$/i.test(value)) return true
163 if (/^false$/i.test(value)) return false
164 if (/^[+-]?\.inf$/i.test(value)) return value[0] === "-" ? Number.NEGATIVE_INFINITY : Number.POSITIVE_INFINITY
165 if (/^\.nan$/i.test(value)) return Number.NaN
166
167 const normalized = value.replace(/_/g, "")
168 if (/^[+-]?0x[0-9a-f]+$/i.test(normalized)) {
169 const sign = normalized[0] === "-" ? -1 : 1
170 return sign * Number.parseInt(normalized.replace(/^[+-]?0x/i, ""), 16)
171 }
172 if (/^[+-]?0o[0-7]+$/i.test(normalized)) {
173 const sign = normalized[0] === "-" ? -1 : 1
174 return sign * Number.parseInt(normalized.replace(/^[+-]?0o/i, ""), 8)
175 }
176 if (
177 /^[+-]?(?:0|[1-9]\d*)$/.test(normalized) ||
178 /^[+-]?(?:(?:0|[1-9]\d*)?\.\d+|(?:0|[1-9]\d*)\.?\d*[eE][+-]?\d+)$/.test(normalized)
179 ) {
180 return Number(normalized)
181 }
182 return value
183}
184
185const parseKey = (input: string): string => {
186 const value = parseScalar(input)

Callers 3

parseKeyFunction · 0.85
parseValueMethod · 0.85
parseInlineValueMethod · 0.85

Calls 3

parseDoubleQuotedFunction · 0.85
NumberInterface · 0.50
replaceMethod · 0.45

Tested by

no test coverage detected