UNPKG

genshin-manager

Version:

<div align="center"> <p> <a href="https://www.npmjs.com/package/genshin-manager"><img src="https://img.shields.io/npm/v/genshin-manager.svg?maxAge=3600" alt="npm version" /></a> <a href="https://www.npmjs.com/package/genshin-manager"><img src="https:

84 lines (83 loc) 2.58 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.TextMapTransform = void 0; const stream_1 = require("stream"); const TextMapFormatError_1 = require("../errors/TextMapFormatError"); /** * TextMapTransform */ class TextMapTransform extends stream_1.Transform { /** * Create a TextMapTransform * @param language Language * @param filterSet Filter set */ constructor(language, filterSet) { super(); this.buffer = Buffer.from(''); this.firstFlag = true; this.language = language; this.filterSet = filterSet; } /** * Transform * @param chunk Buffer * @param encoding Encoding * @param callback Callback */ _transform(chunk, encoding, callback) { const combinedBuffer = Buffer.concat([this.buffer, chunk]); const lineBuffers = this.splitBuffer(combinedBuffer, Buffer.from('\n')); this.buffer = lineBuffers.pop() || Buffer.from(''); if (lineBuffers.length === 0) { callback(); return; } const lines = lineBuffers.map((buffer) => buffer.toString()); const isFirstChunk = this.firstFlag && lines[0].startsWith('{'); if (isFirstChunk) this.push('{\n'); lines.forEach((line) => { const matchArray = line.match(/(?<=")([^"\\]|\\.)*?(?=")/g); if (!matchArray) return; const [key, , value] = matchArray; if (this.filterSet.has(+key)) { if (!this.firstFlag) this.push(',\n'); this.firstFlag = false; this.push(`"${key}":"${value.replace(/\\\\n/g, '\\n')}"`); } }); callback(); } /** * Flush * @param callback Callback */ _flush(callback) { this.push('\n' + this.buffer.toString()); callback(); } /** * Final * @param callback Callback */ _final(callback) { if (!this.buffer.toString().endsWith('}')) throw new TextMapFormatError_1.TextMapFormatError(this.language); callback(); } splitBuffer(array, separator) { const result = []; let start = 0; let index; while ((index = array.indexOf(separator, start)) !== -1) { result.push(array.slice(start, index)); start = index + separator.length; } result.push(array.slice(start)); return result; } } exports.TextMapTransform = TextMapTransform;