UNPKG

amourlinguini

Version:

A JSON-based translation file manager.

70 lines 2.89 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.DataUtils = void 0; var categorized_location_1 = require("../models/categorized-location"); var _1 = require("./"); var DataUtils = /** @class */ (function () { function DataUtils() { } DataUtils.replaceVariables = function (input, variables) { var output = input; for (var _i = 0, _a = Object.entries(variables); _i < _a.length; _i++) { var _b = _a[_i], varName = _b[0], varValue = _b[1]; output = output.replaceAll("{{".concat(varName, "}}"), varValue); } return output; }; DataUtils.replaceVariablesInObj = function (jsonValue, variables) { switch (typeof jsonValue) { case 'object': { for (var key in jsonValue) { // "for ... in" loops over all properties, including prototypes // So we need to check if this property belong to only the object if (!jsonValue.hasOwnProperty(key)) { continue; } jsonValue[key] = this.replaceVariablesInObj(jsonValue[key], variables); } break; } case 'string': { if ((0, categorized_location_1.instanceOfLocation)(variables)) { jsonValue = this.replaceVariables(jsonValue, variables); } else { jsonValue = this.replaceVariablesInObj(jsonValue, variables); } break; } default: { break; } } return jsonValue; }; DataUtils.flatten = function (input) { var output = {}; for (var _i = 0, _a = Object.entries(input); _i < _a.length; _i++) { var _b = _a[_i], categoryName = _b[0], categoryData = _b[1]; for (var _c = 0, _d = Object.entries(categoryData); _c < _d.length; _c++) { var _e = _d[_c], itemName = _e[0], itemData = _e[1]; output["".concat(categoryName, ".").concat(itemName)] = itemData; } } return output; }; DataUtils.flattenToVariables = function (input, prefix) { var output = {}; for (var _i = 0, _a = Object.entries(input); _i < _a.length; _i++) { var _b = _a[_i], categoryName = _b[0], categoryData = _b[1]; for (var _c = 0, _d = Object.entries(categoryData); _c < _d.length; _c++) { var _e = _d[_c], itemName = _e[0], itemData = _e[1]; output["".concat(prefix).concat(categoryName, ".").concat(itemName)] = _1.StringUtils.join(itemData, '\n'); } } return output; }; return DataUtils; }()); exports.DataUtils = DataUtils; //# sourceMappingURL=data-utils.js.map