UNPKG

@mann-conomy/vdf-utils

Version:

A Node.js utility library for parsing and converting objects in Valve's Data File (VDF) format.

69 lines 1.83 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.PopulatedStack = exports.Stack = void 0; const enums_1 = require("../resources/enums"); class Stack { constructor() { this.elements = []; } toValue(value) { if (value != null && value !== enums_1.StringSeparator.Empty && !isNaN(+value)) { return +value; } switch (value) { case "false": return false; case "true": return true; case "null": return null; case "undefined": return undefined; default: return value; } } new() { return Object.create(Object.prototype); } pop() { if (this.isEmpty()) { throw new RangeError("Cannot pop an element from an empty Stack"); } return this.elements.pop(); } push(element) { this.elements.push(element); } peek() { if (this.isEmpty()) { throw new RangeError("Cannot peek into an empty Stack"); } const index = this.size() - 1; const last = this.elements[index]; if (last == null) { throw new RangeError("The last Stack element is undefined or null"); } return last; } update(key, value) { const element = this.peek(); element[key] = this.toValue(value); } size() { return this.elements.length; } isEmpty() { return this.size() === 0; } } exports.Stack = Stack; class PopulatedStack extends Stack { constructor() { super(); const first = this.new(); this.push(first); } } exports.PopulatedStack = PopulatedStack; //# sourceMappingURL=stack.js.map