UNPKG

@iwsio/json-csv-node

Version:

ESM/CJS module that easily converts JSON to CSV. This package supports streaming and buffered conversion to CSV.

37 lines (36 loc) 1.3 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.StringWriter = void 0; const stream_1 = require("stream"); const string_decoder_1 = require("string_decoder"); /** * This is a writable stream that encodes data to an internal buffered string. It's pulled from Node14 docs and used as a test utility here. * Ex: * ``` * const writer = new StringWriter({defaultEncoding: 'utf8'}) * Readable.from(['1', '2', '3']).pipe(writer).finish(() => { console.log(writer.data) }) * '123' * ``` * See: https://nodejs.org/docs/latest-v14.x/api/stream.html#stream_decoding_buffers_in_a_writable_stream */ class StringWriter extends stream_1.Writable { constructor(options) { var _a; super(options); this._decoder = new string_decoder_1.StringDecoder((_a = options === null || options === void 0 ? void 0 : options.defaultEncoding) !== null && _a !== void 0 ? _a : 'utf8'); this.data = ''; } _write(chunk, encoding, callback) { if (encoding != null) { chunk = this._decoder.write(chunk); } this.data += chunk; callback(); } _final(callback) { this.data += this._decoder.end(); callback(); } } exports.default = StringWriter; exports.StringWriter = StringWriter;