UNPKG

poe-i18n

Version:

i18n utility for Path of Exile

100 lines (99 loc) 3.41 kB
"use strict"; var __read = (this && this.__read) || function (o, n) { var m = typeof Symbol === "function" && o[Symbol.iterator]; if (!m) return o; var i = m.call(o), r, ar = [], e; try { while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value); } catch (error) { e = { error: error }; } finally { try { if (r && !r.done && (m = i["return"])) m.call(i); } finally { if (e) throw e.error; } } return ar; }; Object.defineProperty(exports, "__esModule", { value: true }); function deterministicValueForMatcher(matcher) { if (Array.isArray(matcher)) { var _a = __read(matcher, 2), min = _a[0], max = _a[1]; if (min === '#' && max === '#') { return 0; } else if (min === '#' && max !== '#') { return max; } else if (min !== '#' && max === '#') { return min; } else if (min !== '#' && max !== '#') { return Math.floor((max - min) / 2); } else { throw new Error('ts never'); } } else if (matcher === '#') { return 0; } else { return matcher; } } exports.deterministicValueForMatcher = deterministicValueForMatcher; // builds random stats matching every single translation of the description function buildRandomStats(description) { if (description.no_description) { return null; } return description.translations.map(function (_a) { var matchers = _a.matchers; return description.stats.map(function (stat_id, i) { return { id: stat_id, // division by 60 can produce nasty rounding errors value: randomNumberForMatcher(matchers[i]) }; }); }); } exports.buildRandomStats = buildRandomStats; function randomNumberForMatcher(matcher, step, precision) { if (step === void 0) { step = 1; } if (precision === void 0) { precision = 0; } if (Array.isArray(matcher)) { var _a = __read(matcher, 2), min = _a[0], max = _a[1]; if (min === '#' && max === '#') { return randomNumberForMatcher('#', step, precision); } else if (min === '#' && max !== '#') { return randomNumber({ precision: precision, max: max }); } else if (min !== '#' && max === '#') { return randomNumber({ precision: precision, min: min }); } else if (min !== '#' && max !== '#') { return randomNumber({ precision: precision, min: min, max: max, step: step }); } else { throw new Error('ts never'); } } else if (matcher === '#') { return randomNumber({ precision: precision, max: -1, step: step }); } else { return matcher; } } function randomNumber(domain) { if (domain === void 0) { domain = {}; } // tslint:disable-next-line: no-bitwise var _a = domain.min, min = _a === void 0 ? 1 << 31 : _a, _b = domain.max, max = _b === void 0 ? Math.pow(2, 31) - 1 : _b, _c = domain.precision, precision = _c === void 0 ? 0 : _c, _d = domain.step, step = _d === void 0 ? 1 : _d; var range = Math.abs(max - min); var rand = Math.random(); var n = +(rand * range + min); return +(n - +(n % step).toFixed(precision)).toFixed(precision); }