UNPKG

dbmaster-cli

Version:

Tool for converting tables between Fifa Soccer Games

38 lines (37 loc) 1.19 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Json2CsvTransform = void 0; const stream_1 = require("stream"); const interfaces_1 = require("../interfaces"); const utils_1 = require("../utils"); class Json2CsvTransform extends stream_1.Transform { constructor(opts) { super(opts); this.line = 0; this.opts = opts; } _transform(chunk, encoding, callback) { const lines = []; const orderedFields = this.opts.fields.sort(utils_1.sortByOrder); if (this.line === 0) { lines.push('\ufeff' + orderedFields.map((f) => f.name).join('\t')); } const oldValue = JSON.parse(chunk.toString()); lines.push(orderedFields .map((f) => { if (f.type === interfaces_1.Datatype.Float) { return String(oldValue[f.name]).replace('.', ','); } return oldValue[f.name]; }) .join('\t')); this.push(lines.join('\r\n')); this.line++; callback(); } _flush(callback) { this.line = 0; callback(); } } exports.Json2CsvTransform = Json2CsvTransform;