Skip to content

Instantly share code, notes, and snippets.

@azu
Created February 28, 2019 05:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save azu/6d217e900d0606a34339f907e00bd29b to your computer and use it in GitHub Desktop.
Save azu/6d217e900d0606a34339f907e00bd29b to your computer and use it in GitHub Desktop.
import * as fs from "fs";
import split from "split2";
export interface ndjsonToJsonTextOption extends split.Options {
}
export function ndjsonToJsonText(ndjsonFileName: string, options: ndjsonToJsonTextOption = {}) {
function appendComma(line: string) {
return line + ",";
}
return new Promise((resolve, reject) => {
const fileStream = fs.createReadStream(ndjsonFileName);
const list: string[] = [];
fileStream
.pipe(split(appendComma, options))
.on("data", function(obj) {
list.push(obj.toString());
})
.on("error", (error: Error) => {
reject(error);
})
.on("end", () => {
fileStream.close();
const text = list.join("");
// trim last ,
const jsonText = `[${text.substr(0, text.length - 1)}]`;
resolve(jsonText);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment