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

Function parse

packages/effect/src/unstable/encoding/Ini.ts:92–174  ·  view source on GitHub ↗
(input: string)

Source from the content-addressed store, hash-verified

90 * @since 4.0.0
91 */
92export const parse = (input: string): Record<string, unknown> => {
93 const output: Record<string, any> = Object.create(null)
94 let current = output
95 const sectionPattern = /^\[([^\]]*)\]\s*$/
96 const propertyPattern = /^([^=]+)(=(.*))?$/
97
98 for (const line of input.split(/[\r\n]+/g)) {
99 if (line.length === 0 || /^\s*(?:[;#]|$)/.test(line)) {
100 continue
101 }
102
103 const sectionMatch = sectionPattern.exec(line)
104 if (sectionMatch !== null) {
105 const section = unquote(sectionMatch[1])
106 if (section === "__proto__") {
107 current = Object.create(null)
108 } else {
109 current = output[section] ??= Object.create(null)
110 }
111 continue
112 }
113
114 const propertyMatch = propertyPattern.exec(line)
115 if (propertyMatch === null) {
116 continue
117 }
118
119 const rawKey = unquote(propertyMatch[1])
120 const array = rawKey.length > 2 && rawKey.endsWith("[]")
121 const key = array ? rawKey.slice(0, -2) : rawKey
122 if (key === "__proto__") {
123 continue
124 }
125
126 const rawValue = propertyMatch[2] === undefined ? true : unquote(propertyMatch[3])
127 const value = rawValue === "true" || rawValue === "false" || rawValue === "null"
128 ? JSON.parse(rawValue)
129 : rawValue
130
131 if (array) {
132 if (!hasOwn.call(current, key)) {
133 current[key] = []
134 } else if (!Array.isArray(current[key])) {
135 current[key] = [current[key]]
136 }
137 }
138 if (Array.isArray(current[key])) {
139 current[key].push(value)
140 } else {
141 current[key] = value
142 }
143 }
144
145 const remove: Array<string> = []
146 for (const section of Object.keys(output)) {
147 if (typeof output[section] !== "object" || output[section] === null || Array.isArray(output[section])) {
148 continue
149 }

Callers

nothing calls this directly

Calls 7

splitSectionsFunction · 0.85
execMethod · 0.80
pushMethod · 0.80
unquoteFunction · 0.70
splitMethod · 0.45
parseMethod · 0.45
replaceMethod · 0.45

Tested by

no test coverage detected