UNPKG

poe-i18n

Version:

i18n utility for Path of Exile

220 lines (219 loc) 10.2 kB
"use strict"; var __generator = (this && this.__generator) || function (thisArg, body) { var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; function verb(n) { return function (v) { return step([n, v]); }; } function step(op) { if (f) throw new TypeError("Generator is already executing."); while (_) try { if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; if (y = 0, t) op = [op[0] & 2, t.value]; switch (op[0]) { case 0: case 1: t = op; break; case 4: _.label++; return { value: op[1], done: false }; case 5: _.label++; y = op[1]; op = [0]; continue; case 7: op = _.ops.pop(); _.trys.pop(); continue; default: if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } if (t[2]) _.ops.pop(); _.trys.pop(); continue; } op = body.call(thisArg, _); } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; } }; var __values = (this && this.__values) || function (o) { var m = typeof Symbol === "function" && o[Symbol.iterator], i = 0; if (m) return m.call(o); return { next: function () { if (o && i >= o.length) o = void 0; return { value: o && o[i++], done: !o }; } }; }; Object.defineProperty(exports, "__esModule", { value: true }); var formatters_1 = require("../localize/formatters"); var translate_1 = require("../translate"); var symbolicStats_1 = require("../util/symbolicStats"); var util_1 = require("./util"); /** * finds every stat or list of stats that could produce this text with its values * * use {textToStatsSingle} if you just want the first match * use {textToStatsArray} if you want the generator values as an array * * @param text the stat text * @param options see type definition */ function textToStats(text, options) { var e_1, _a, e_2, _b, e_3, _c, _d, datas, _e, start_file, _f, _g, descriptions, _h, _j, description, _loop_1, _k, _l, translation, e_3_1, e_2_1, e_1_1; if (options === void 0) { options = {}; } return __generator(this, function (_m) { switch (_m.label) { case 0: _d = options.datas, datas = _d === void 0 ? {} : _d, _e = options.start_file, start_file = _e === void 0 ? 'stat_descriptions' : _e; _m.label = 1; case 1: _m.trys.push([1, 18, 19, 20]); _f = __values(util_1.getDescriptions(datas, start_file)), _g = _f.next(); _m.label = 2; case 2: if (!!_g.done) return [3 /*break*/, 17]; descriptions = _g.value; _m.label = 3; case 3: _m.trys.push([3, 14, 15, 16]); _h = __values(Object.values(descriptions)), _j = _h.next(); _m.label = 4; case 4: if (!!_j.done) return [3 /*break*/, 13]; description = _j.value; if (description.no_description) { return [3 /*break*/, 12]; } _loop_1 = function (translation) { var regexp, match, stats; return __generator(this, function (_a) { switch (_a.label) { case 0: regexp = translate_1.asRegexp(translation); match = regexp.match(text); if (!(match !== null)) return [3 /*break*/, 2]; stats = description.stats.map(function (stat_id, i) { // arguments are 1-based var arg_index = String(i + 1); var matched_value = match[arg_index]; var matcher = translation.matchers[i]; var value = Number.NaN; if (matched_value !== undefined) { var formatter = translation.formatters .filter(function (f) { return typeof f !== 'string'; }) .find(function (_a) { var arg = _a.arg; return String(arg) === arg_index; }); if (formatter === undefined) { value = +matched_value; } else { value = formatters_1.inverseFactory(formatter.id)(matched_value); if (Number.isNaN(value)) { // otherwise the return value would be equal to the case // where the value is not contained in the text throw new Error('int parsing returned NaN'); } } } else if (typeof matcher === 'number') { // value is not contained in the text // guess (deterministically) a value for this instance // that would have produced that translation value = symbolicStats_1.deterministicValueForMatcher(translation.matchers[i]); } return { id: stat_id, value: value }; }); if (!translate_1.matchesTranslation(translation, stats.map(function (_a) { var value = _a.value; return value; }))) return [3 /*break*/, 2]; return [4 /*yield*/, stats]; case 1: _a.sent(); _a.label = 2; case 2: return [2 /*return*/]; } }); }; _m.label = 5; case 5: _m.trys.push([5, 10, 11, 12]); _k = __values(description.translations), _l = _k.next(); _m.label = 6; case 6: if (!!_l.done) return [3 /*break*/, 9]; translation = _l.value; return [5 /*yield**/, _loop_1(translation)]; case 7: _m.sent(); _m.label = 8; case 8: _l = _k.next(); return [3 /*break*/, 6]; case 9: return [3 /*break*/, 12]; case 10: e_3_1 = _m.sent(); e_3 = { error: e_3_1 }; return [3 /*break*/, 12]; case 11: try { if (_l && !_l.done && (_c = _k.return)) _c.call(_k); } finally { if (e_3) throw e_3.error; } return [7 /*endfinally*/]; case 12: _j = _h.next(); return [3 /*break*/, 4]; case 13: return [3 /*break*/, 16]; case 14: e_2_1 = _m.sent(); e_2 = { error: e_2_1 }; return [3 /*break*/, 16]; case 15: try { if (_j && !_j.done && (_b = _h.return)) _b.call(_h); } finally { if (e_2) throw e_2.error; } return [7 /*endfinally*/]; case 16: _g = _f.next(); return [3 /*break*/, 2]; case 17: return [3 /*break*/, 20]; case 18: e_1_1 = _m.sent(); e_1 = { error: e_1_1 }; return [3 /*break*/, 20]; case 19: try { if (_g && !_g.done && (_a = _f.return)) _a.call(_f); } finally { if (e_1) throw e_1.error; } return [7 /*endfinally*/]; case 20: return [2 /*return*/]; } }); } exports.default = textToStats; /** * @see {textToStats} as array * * @param text * @param options */ function textToStatsArray(text, options) { if (options === void 0) { options = {}; } return Array.from(textToStats(text, options)); } exports.textToStatsArray = textToStatsArray; /** * only first match of @see {textToStats} but throws if none was found * * @param text * @param options */ function textToStatsFirst(text, options) { if (options === void 0) { options = {}; } var _a = textToStats(text, options).next(), done = _a.done, value = _a.value; if (done) { throw new Error('Could match a single stat'); } return value; } exports.textToStatsFirst = textToStatsFirst;