ng-string-parser
Version:
thick as a brick Angular string parser service
50 lines (45 loc) • 1.35 kB
JavaScript
import { Injectable } from '@angular/core';
class ParserService {
constructor() {
this.pattern = '{value}';
}
parse(patterns, values) {
const result = new Object();
for (const key in patterns) {
if (!patterns.hasOwnProperty(key)) {
continue;
}
const str = patterns[key];
if (typeof str !== 'string') {
throw new Error(`In patterns, the value should be a string: ${key}`);
}
const match = str.match(new RegExp(`(.*)(${this.pattern})(.*)`));
if (!match || !(key in values)) {
result[key] = str;
continue;
}
const particles = Array.from(match).slice(1);
const index = particles.indexOf(this.pattern);
particles[index] = String(values[key]);
result[key] = particles.join('');
}
return result;
}
changePattern(newPattern) {
if (!newPattern) {
throw new Error('Pattern cannot be empty string');
}
this.pattern = newPattern;
}
}
ParserService.decorators = [
{ type: Injectable }
];
/*
* Public API Surface of parser
*/
/**
* Generated bundle index. Do not edit.
*/
export { ParserService };
//# sourceMappingURL=ng-string-parser.js.map