poe-i18n
Version:
i18n utility for Path of Exile
27 lines (26 loc) • 1.03 kB
JavaScript
import * as escapeStringRegexp from 'escape-string-regexp';
import { regexpFactory } from '../localize/formatters';
import NamedGroupsRegexp from '../util/NamedGroupsRegexp';
export default function asRegexp(translation) {
var formatters = translation.formatters, text = translation.text;
var groups = [];
var regexp = escapeStringRegexp(text)
.replace(/%(\d+)(\\\$\\\+d|%)/g, function (match, arg, modifier) {
groups.push(arg);
var formatter = formatters
.filter(function (f) { return typeof f !== 'string'; })
.find(function (_a) {
var other = _a.arg;
return "" + other === arg;
});
var prefix = modifier === '\\$\\+d' ? '\\+' : '';
if (formatter === undefined) {
return prefix + "(-?\\d+)";
}
else {
return prefix + "(" + regexpFactory(formatter.id) + ")";
}
})
.replace(/%%/g, '%');
return new NamedGroupsRegexp(new RegExp("^" + regexp + "$"), groups);
}