This commit is contained in:
Will Greenberg
2025-04-08 15:12:41 -07:00
parent 057c9acb40
commit cf2d406d88
7 changed files with 121 additions and 37 deletions

View File

@@ -1,11 +1,15 @@
export type NewlineDeliminatedJson = any[];
export function parse_ndjson(input: string): NewlineDeliminatedJson {
console.log(input)
const lines = input.split('\n');
const result = [];
let current_line = '';
while (lines.length > 0) {
current_line += lines.shift();
if (current_line.length === 0) {
continue;
}
try {
const entry = JSON.parse(current_line);
result.push(entry);
@@ -16,7 +20,7 @@ export function parse_ndjson(input: string): NewlineDeliminatedJson {
// however, if we've reached the end of the input, that means we
// were given invalid nd-json
if (lines.length === 0) {
throw new Error(`unable to parse invalid nd-json: ${e}`);
throw new Error(`unable to parse invalid nd-json: ${e}, "${current_line}"`);
}
}
}