UNPKG

@ima/core

Version:

IMA.js framework for isomorphic javascript application

57 lines (56 loc) 1.55 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); Object.defineProperty(exports, "UrlTransformer", { enumerable: true, get: function() { return UrlTransformer; } }); class UrlTransformer { _rules; static get $dependencies() { return []; } /** * Initializes the URL transformer. */ constructor(){ this._rules = {}; } /** * Adds the provided replacement rule to the rules used by this URL * transformer. * * @param pattern Regexp patter to look for (must be escaped as if * for use in the {@link Regexp} constructor). * @param replacement The replacement of the matched patter in any * matched URL. * @return This transformer. */ addRule(pattern, replacement) { this._rules[pattern] = replacement; return this; } /** * Clears all rules. */ clear() { this._rules = {}; return this; } /** * Applies all rules registered with this URL transformer to the provided * URL and returns the result. The rules will be applied in the order they * were registered. * * @param url The URL for transformation. * @return Transformed URL. */ transform(url) { const rulesKey = Object.keys(this._rules); if (rulesKey.length === 0) { return url; } const reg = new RegExp(rulesKey.join('|'), 'g'); return url.replace(reg, (ruleKey)=>this._rules[ruleKey]); } } //# sourceMappingURL=UrlTransformer.js.map