UNPKG

@etothepii/satisfactory-file-parser

Version:

A file parser for satisfactory files. Includes save files and blueprint files.

84 lines (83 loc) 3.63 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.JsonStreamWritable = void 0; class JsonStreamWritable { constructor(pushWritable) { this.pushWritable = pushWritable; this.isValid = () => this.openedSymbols.length === 0; this.numPropertiesInCurrentOpenedSymbol = () => this.numberPropertiesInOpenedSymbol.length > 0 ? this.numberPropertiesInOpenedSymbol.at(-1) : 0; this.openedSymbols = []; this.numberPropertiesInOpenedSymbol = []; this.currentPropertyOpen = false; } async startObject() { await this.pushWritable('{'); this.openedSymbols.push('object'); this.numberPropertiesInOpenedSymbol.push(0); this.currentPropertyOpen = false; return; } async endObject() { await this.pushWritable('}'); this.openedSymbols.splice(this.openedSymbols.lastIndexOf('object'), 1); if (this.numberPropertiesInOpenedSymbol.length > 0) { this.numberPropertiesInOpenedSymbol.splice(this.numberPropertiesInOpenedSymbol.length - 1, 1); } return; } async startArray() { await this.pushWritable('['); this.openedSymbols.push('array'); this.numberPropertiesInOpenedSymbol.push(0); this.currentPropertyOpen = false; return; } async endArray() { this.pushWritable(']'); this.openedSymbols.splice(this.openedSymbols.lastIndexOf('array'), 1); if (this.numberPropertiesInOpenedSymbol.length > 0) { this.numberPropertiesInOpenedSymbol.splice(this.numberPropertiesInOpenedSymbol.length - 1, 1); } return; } async writeProperty(name, value) { if (this.openedSymbols.length <= 0 || this.openedSymbols.at(-1) !== 'object' || this.currentPropertyOpen) { throw new Error(`Json Write Method is not allowed for property ${name} here.`); } const prefix = this.numPropertiesInCurrentOpenedSymbol() > 0 ? ', ' : ''; await this.pushWritable(`${prefix}${name}: ${await value}`); this.numberPropertiesInOpenedSymbol[this.numberPropertiesInOpenedSymbol.length - 1] += 1; return; } async writeProperties(properties) { if (this.currentPropertyOpen) { throw new Error(`Json Write Method is not allowed, since currently a property value needs to be written.`); } for (const property of properties) { await this.writeProperty(property.name, property.value); } return; } async writePropertyName(name) { if (this.currentPropertyOpen) { throw new Error(`Json Write Method is not allowed, since currently a property value needs to be written.`); } const prefix = this.numPropertiesInCurrentOpenedSymbol() > 0 ? ', ' : ''; this.currentPropertyOpen = true; await this.pushWritable(`${prefix}${name}:`); return; } async writeValue(value) { if (this.openedSymbols.length <= 0 || (!this.currentPropertyOpen && this.openedSymbols.at(-1) !== 'array')) { throw new Error(`Json Write Method is not allowed for any value here.`); } const prefix = this.numPropertiesInCurrentOpenedSymbol() > 0 ? ', ' : ''; if (this.currentPropertyOpen) { this.currentPropertyOpen = false; } await this.pushWritable(`${prefix}${await value}`); this.numberPropertiesInOpenedSymbol[this.numberPropertiesInOpenedSymbol.length - 1] += 1; return; } } exports.JsonStreamWritable = JsonStreamWritable;