UNPKG

enka-network-api

Version:

Enka-network API wrapper for Genshin Impact.

94 lines (93 loc) 4.21 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.DynamicTextAssets = void 0; const config_file_js_1 = require("config_file.js"); const TextAssets_1 = require("./TextAssets"); class DynamicTextAssets extends TextAssets_1.TextAssets { constructor(id, data, enka, convertToHtmlFormat = false, directory) { super(id, enka, convertToHtmlFormat, directory); this.dynamicData = (0, config_file_js_1.bindOptions)(enka.options.textAssetsDynamicData, data); } /** * @param replaceWith an empty array is the same as an array containing all paths * @throws AssetsNotFoundError */ getReplacedData(replaceWith = [], lang) { var _a, _b, _c; function isEnabled(key) { if (key.includes(".") && !isEnabled(key.split(".").slice(0, -1).join("."))) { return false; } return replaceWith.length === 0 || replaceWith.includes(key); } const usedParamIndices = []; let text = this.get(lang).replace(/^#/, ""); if (isEnabled("paramList")) { text = text.replace(/\{([^}]+):([^}]+)\}/g, (match, key, format) => { const index = Number(key.slice("param".length)) - 1; if (isNaN(index) || this.dynamicData.paramList.length <= index) return match; const value = this.dynamicData.paramList[index]; usedParamIndices.push(index); const isPercent = format.includes("P"); const isInteger = format.includes("I"); const fixMatch = format.match(/F(\d+)/); const fix = fixMatch && !isInteger ? Number(fixMatch[1]) : 0; return (value * (isPercent ? 100 : 1)).toFixed(fix) + (isPercent ? "%" : ""); }); } const placeholderMap = {}; if (isEnabled("userInfo.travelerGender") && ((_a = this.dynamicData.userInfo) === null || _a === void 0 ? void 0 : _a.travelerGender) != null) { placeholderMap["M"] = false; placeholderMap["F"] = false; placeholderMap[this.dynamicData.userInfo.travelerGender.slice(0, 1)] = true; } if (isEnabled("userInfo.platform") && ((_b = this.dynamicData.userInfo) === null || _b === void 0 ? void 0 : _b.platform) != null) { placeholderMap["LAYOUT_MOBILE"] = false; placeholderMap["LAYOUT_PC"] = false; placeholderMap["LAYOUT_PS"] = false; placeholderMap["LAYOUT_" + this.dynamicData.userInfo.platform] = true; } // show if placeholder is true, remove if placeholder is false, and do nothing if placeholder is undefined. text = text.replace(/\{([^#}]+)#([^}]+)\}/g, ($0, $1, $2) => placeholderMap[$1] ? $2 : placeholderMap[$1] === false ? "" : $0); if (isEnabled("userInfo.travelerNickname") && ((_c = this.dynamicData.userInfo) === null || _c === void 0 ? void 0 : _c.travelerNickname) != null) { text = text.replace(/\{NICKNAME\}/g, this.dynamicData.userInfo.travelerNickname); } return { text, usedParamIndices }; } /** * @returns null instead of throwing AssetsNotFoundError. */ getNullableReplacedData(replaceWith = [], lang) { try { return this.getReplacedData(replaceWith, lang); } catch (_a) { return null; } } /** * @throws AssetsNotFoundError */ getReplacedText(replaceWith = [], lang) { return this.getReplacedData(replaceWith, lang).text; } /** * @returns null instead of throwing AssetsNotFoundError. */ getNullableReplacedText(replaceWith = [], lang) { try { return this.getReplacedText(replaceWith, lang); } catch (_a) { return null; } } /** * @returns new instance of DynamicTextAssets with provided `userInfo`. */ copyWithUserInfo(userInfo) { return new DynamicTextAssets(this.id, Object.assign(Object.assign({}, this.dynamicData), { userInfo }), this.enka, this.convertToHtmlFormat); } } exports.DynamicTextAssets = DynamicTextAssets;