UNPKG

fracturedjsonjs

Version:

JSON formatter that produces highly readable but fairly compact output

45 lines (44 loc) 1.45 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.StringJoinBuffer = void 0; /** * A place where strings are piled up sequentially to eventually make one big string. */ class StringJoinBuffer { constructor(trimTrailingWhitespace) { this._lineBuff = []; this._docBuff = []; this._trimTrailingWhitespace = trimTrailingWhitespace; } Add(...values) { this._lineBuff.push(...values); return this; } EndLine(eolString) { this.AddLineToWriter(eolString); return this; } Flush() { this.AddLineToWriter(""); return this; } AsString() { // I experimented with a few approaches to try to make this faster, but none of them made much difference // for an 8MB file. Turns out Array.join is really quite good. return this._docBuff.join(""); } /** * Takes the contents of _lineBuff and merges them into a string and adds it to _docBuff. If desired, * we trim trailing whitespace in the process. */ AddLineToWriter(eolString) { if (this._lineBuff.length === 0 && eolString.length === 0) return; let line = this._lineBuff.join(""); if (this._trimTrailingWhitespace) line = line.trimEnd(); this._docBuff.push(line + eolString); this._lineBuff = []; } } exports.StringJoinBuffer = StringJoinBuffer;