UNPKG

ng-string-parser

Version:

thick as a brick Angular string parser service

59 lines (52 loc) 2.16 kB
(function (global, factory) { typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('@angular/core')) : typeof define === 'function' && define.amd ? define('ng-string-parser', ['exports', '@angular/core'], factory) : (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global['ng-string-parser'] = {}, global.ng.core)); }(this, (function (exports, core) { 'use strict'; var ParserService = /** @class */ (function () { function ParserService() { this.pattern = '{value}'; } ParserService.prototype.parse = function (patterns, values) { var result = new Object(); for (var key in patterns) { if (!patterns.hasOwnProperty(key)) { continue; } var str = patterns[key]; if (typeof str !== 'string') { throw new Error("In patterns, the value should be a string: " + key); } var match = str.match(new RegExp("(.*)(" + this.pattern + ")(.*)")); if (!match || !(key in values)) { result[key] = str; continue; } var particles = Array.from(match).slice(1); var index = particles.indexOf(this.pattern); particles[index] = String(values[key]); result[key] = particles.join(''); } return result; }; ParserService.prototype.changePattern = function (newPattern) { if (!newPattern) { throw new Error('Pattern cannot be empty string'); } this.pattern = newPattern; }; return ParserService; }()); ParserService.decorators = [ { type: core.Injectable } ]; /* * Public API Surface of parser */ /** * Generated bundle index. Do not edit. */ exports.ParserService = ParserService; Object.defineProperty(exports, '__esModule', { value: true }); }))); //# sourceMappingURL=ng-string-parser.umd.js.map