UNPKG

@technobuddha/library

Version:
52 lines (51 loc) 2.17 kB
"use strict"; var __values = (this && this.__values) || function(o) { var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0; if (m) return m.call(o); if (o && typeof o.length === "number") return { next: function () { if (o && i >= o.length) o = void 0; return { value: o && o[i++], done: !o }; } }; throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined."); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.fillTemplate = void 0; var escapeRegExp_1 = __importDefault(require("lodash/escapeRegExp")); var constants_1 = require("../constants"); /** * Fill a template with supplies values * @param input The template * @param values A dictionary of name-values used to fill in values in the template * @param __namedParameters see {@link Options} * @default open '{{' * @default close (default '}}') * @return template with values replaced */ function fillTemplate(input, values, _a) { var e_1, _b; var _c, _d; var _e = _a === void 0 ? {} : _a, _f = _e.open, open = _f === void 0 ? '{{' : _f, _g = _e.close, close = _g === void 0 ? '}}' : _g; try { // eslint-disable-next-line @typescript-eslint/prefer-regexp-exec for (var _h = __values((_c = input.match(new RegExp(escapeRegExp_1.default(open) + "(.+?)" + escapeRegExp_1.default(close), 'ug'))) !== null && _c !== void 0 ? _c : []), _j = _h.next(); !_j.done; _j = _h.next()) { var match = _j.value; var key = match.slice(open.length, -close.length).trim(); input = input.replace(match, (_d = values[key]) !== null && _d !== void 0 ? _d : constants_1.empty); } } catch (e_1_1) { e_1 = { error: e_1_1 }; } finally { try { if (_j && !_j.done && (_b = _h.return)) _b.call(_h); } finally { if (e_1) throw e_1.error; } } return input; } exports.fillTemplate = fillTemplate; exports.default = fillTemplate;