UNPKG

@mann-conomy/vdf-utils

Version:

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

50 lines 1.62 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getFileLines = getFileLines; exports.getLastKey = getLastKey; exports.getObject = getObject; exports.isObject = isObject; exports.isObjectEmpty = isObjectEmpty; function isValidLine(line) { return line.length > 0 && line !== "\"" && !line.startsWith("//"); } function removeInlineComments(lines) { return lines.filter(line => isValidLine(line)); } function getFilteredLines(file) { const lines = file.split("\n"); const trimmed = lines.map(line => line.trim()); return removeInlineComments(trimmed); } function isFirstLineEmpty(lines) { const [first] = lines; return first == null || first.length === 0; } function getFileLines(file) { const lines = getFilteredLines(file); if (!(lines.length > 1) && isFirstLineEmpty(lines)) { throw new RangeError("The VDF file is empty"); } return lines; } function getLastKey(object) { const last = Object.keys(object).pop(); if (last == null || last.length === 0) { throw new RangeError("The object is empty"); } return last; } function getObject(line) { const [key, value] = line.split(/"\s*"/).map(e => e.replace(/"/g, "")); if (key === undefined || key.length === 0) { throw new RangeError("The line contains no JSON key and or value."); } return { key, value }; } function isObject(value) { return typeof value === "object" && value != null; } function isObjectEmpty(source, target) { return isObject(source) && JSON.stringify(source) === target; } //# sourceMappingURL=utils.js.map