Skip to content

Instantly share code, notes, and snippets.

@JosePedroDias

JosePedroDias/jsonl.js

Last active Apr 18, 2020
Embed
What would you like to do?
jsonl as generators
const fs = require('fs');
const readline = require('readline');
async function* readJsonlGen(filename) {
const lineReader = readline.createInterface({
input: fs.createReadStream(filename),
});
let i = 0;
for await (const line of lineReader) {
if (i === 0 && line === '') {
continue;
}
yield JSON.parse(line);
++i;
}
}
function* writeJsonlGen(filename) {
const logger = fs.createWriteStream(filename, {
flags: 'a',
});
while (true) {
const o = yield;
logger.write('\n' + JSON.stringify(o));
}
}
module.exports = { readJsonlGen, writeJsonlGen };
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.