(data: string, strict?: boolean)
| 171 | } |
| 172 | |
| 173 | function 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. |
no test coverage detected