MCPcopy Create free account
hub / github.com/Noumena-Network/code / parseJSONLString

Function parseJSONLString

src/utils/json.ts:173–198  ·  view source on GitHub ↗
(data: string, strict?: boolean)

Source from the content-addressed store, hash-verified

171}
172
173function parseJSONLString<T>(data: string, strict?: boolean): T[] {
174 const stripped = stripBOM(data)
175 const len = stripped.length
176 let start = 0
177
178 const results: T[] = []
179 let lineNumber = 0
180 while (start < len) {
181 let end = stripped.indexOf('\n', start)
182 if (end === -1) end = len
183
184 const line = stripped.substring(start, end).trim()
185 start = end + 1
186 lineNumber++
187 if (!line) continue
188 try {
189 results.push(JSON.parse(line) as T)
190 } catch (e) {
191 if (strict) {
192 throw new JSONLParseError(lineNumber, line, e)
193 }
194 // Skip malformed lines
195 }
196 }
197 return results
198}
199
200/**
201 * Parses JSONL data from a string or Buffer, skipping malformed lines.

Callers 1

parseJSONLFunction · 0.85

Calls 1

stripBOMFunction · 0.90

Tested by

no test coverage detected