Files
strom/src/functions/stringify/index.ts
Jerry Kurian a40b1bf38c Save
2019-08-15 11:54:50 -04:00

23 lines
654 B
TypeScript

import { Transform } from "stream";
import { JsonValue, JsonParseOptions } from "../definitions";
/**
* Return a ReadWrite stream that stringifies the streamed chunks to JSON
*/
export function stringify(
options: JsonParseOptions = { pretty: false },
): NodeJS.ReadWriteStream {
return new Transform({
readableObjectMode: true,
writableObjectMode: true,
transform(chunk: JsonValue, encoding, callback) {
callback(
undefined,
options.pretty
? JSON.stringify(chunk, null, 2)
: JSON.stringify(chunk),
);
},
});
}