string-interpolation-ts
Version:
Dynamic string manipulation in typescript
116 lines (115 loc) • 3.99 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Interpolator = void 0;
const get_1 = __importDefault(require("lodash/get"));
class Interpolator {
constructor(options = {}) {
this.options = options;
this.modifiers = [];
this.aliases = [];
options.modifiers?.forEach((x) => this.registerModifier(x));
options.aliases?.forEach((x) => this.registerAlias(x));
}
registerModifier(modifier) {
this.modifiers.push(modifier);
return this;
}
parseRules(str) {
return (str.match(new RegExp(`{([^}]+)}`, "gi"))?.map((match) => {
return {
key: this.getKeyFromMatch(match),
replace: match,
modifiers: this.getModifiers(match).reduce((acc, x) => {
if (x)
acc.push(x);
return acc;
}, []),
alternativeText: this.getAlternativeText(match)
};
}) || []);
}
getKeyFromMatch(match) {
const removeReservedSymbols = [":", "|"];
return this.removeDelimiter(removeReservedSymbols.reduce((val, sym) => (val.indexOf(sym) > 0 ? this.removeAfter(val, sym) : val), match));
}
removeDelimiter(val) {
return val
.replace(new RegExp("{", "g"), "")
.replace(new RegExp("}", "g"), "");
}
removeAfter(str, val) {
return str.substring(0, str.indexOf(val));
}
extractAfter(str, val) {
return str.substring(str.indexOf(val) + 1);
}
getAlternativeText(str) {
if (str.indexOf(":") > 0) {
const altText = this.removeDelimiter(this.extractAfter(str, ":"));
if (altText.indexOf("|") > 0) {
return this.removeAfter(altText, "|");
}
return altText;
}
return "";
}
getModifiers(str) {
if (str.indexOf("|") > 0) {
return this.removeDelimiter(this.extractAfter(str, "|"))
.split(",")
.map((modifier) => this.getModifier(modifier));
}
return [];
}
parse(str = "", data = {}) {
const rules = this.parseRules(str);
if (rules && rules.length > 0) {
return this.parseFromRules(str, data, rules);
}
return str;
}
parseFromRules(str, data, rules) {
return rules.reduce((reducedStr, rule) => this.applyRule(reducedStr, rule, data), str);
}
applyRule(str, rule, data = {}) {
const dataToReplace = this.applyData(rule.key, data);
if (dataToReplace) {
return str.replace(rule.replace, this.applyModifiers(rule.modifiers, String(dataToReplace), data));
}
else if (rule.alternativeText) {
return str.replace(rule.replace, this.applyModifiers(rule.modifiers, rule.alternativeText, data));
}
return str.replace(rule.replace, "");
}
getFromAlias(key) {
return this.aliases.find((alias) => alias.key === key);
}
applyData(key, data) {
const alias = this.getFromAlias(key);
if (alias) {
const value = (0, get_1.default)(data, alias.ref);
if (value) {
return value;
}
}
return (0, get_1.default)(data, key);
}
getModifier(key) {
return this.modifiers.find((modifier) => modifier.key === key);
}
applyModifiers(modifiers, str, rawData) {
return modifiers.reduce((acc, x) => x.transform(acc, rawData), str);
}
registerAlias(newAlias) {
this.aliases.push(newAlias);
return this;
}
removeAlias(key) {
this.aliases = this.aliases.filter((alias) => alias.key !== key);
return this;
}
}
exports.Interpolator = Interpolator;