UNPKG

poe-i18n

Version:

i18n utility for Path of Exile

79 lines (78 loc) 2.63 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 }); // types of interval overlap var Match; (function (Match) { Match[Match["exact"] = 0] = "exact"; Match[Match["subset"] = 1] = "subset"; Match[Match["superset"] = 2] = "superset"; Match[Match["partial_upper"] = 3] = "partial_upper"; Match[Match["partial_lower"] = 4] = "partial_lower"; Match[Match["none"] = 5] = "none"; // A \minus B = A })(Match = exports.Match || (exports.Match = {})); // match value if for every value in values: value in (min, max) function matchesTranslation(translation, values) { return matches(values, translation.matchers).every(function (matched) { return matched === Match.subset || matched === Match.exact; }); } exports.matchesTranslation = matchesTranslation; // does a value match a matcher function matchesSingle(value, matcher) { return matches([value], [matcher])[0]; } exports.matchesSingle = matchesSingle; function matches(values, matchers) { return matchers.map(function (matcher, i) { return match(values[i], matcher); }); } exports.matches = matches; // interval matching function match(value, matcher) { if (value === undefined) { return Match.none; } var A = rangeCast(value); var B = rangeCast(matcher); if (A[0] === B[0] && A[1] === B[1]) { return Match.exact; } else if (A[0] >= B[0] && A[1] <= B[1]) { return Match.subset; } else if (A[0] <= B[0] && A[1] >= B[1]) { return Match.superset; } else if (A[0] >= B[0] && A[0] <= B[1] && A[1] > B[1]) { return Match.partial_upper; } else if (A[1] >= B[0] && A[1] <= B[1] && A[0] < B[0]) { return Match.partial_lower; } else { return Match.none; } } function rangeCast(value) { var _a = __read(isBoundedRange(value) ? value : [value, value], 2), lower = _a[0], upper = _a[1]; return [ lower === '#' ? Number.NEGATIVE_INFINITY : lower, upper === '#' ? Number.POSITIVE_INFINITY : upper ]; } function isBoundedRange(matcher) { return Array.isArray(matcher) && matcher.length === 2; }