Created
February 28, 2019 05:17
-
-
Save azu/6d217e900d0606a34339f907e00bd29b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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