UNPKG

@naturalcycles/nodejs-lib

Version:
33 lines 1.12 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const js_lib_1 = require("@naturalcycles/js-lib"); const stream_1 = require("stream"); /** * Transforms objects (objectMode=true) into chunks \n-terminated JSON strings (readableObjectMode=false). */ function transformToNDJson(opt = {}) { const { strict = true, separator = '\n', sortObjects = false } = opt; return new stream_1.Transform({ objectMode: true, readableObjectMode: false, transform(chunk, _encoding, cb) { try { if (sortObjects) { chunk = js_lib_1.sortObjectDeep(chunk); } cb(null, JSON.stringify(chunk) + separator); } catch (err) { console.error(err); if (strict) { cb(err); // emit error } else { cb(); // emit no error, but no result neither } } }, }); } exports.transformToNDJson = transformToNDJson; //# sourceMappingURL=transformToNDJson.js.map