UNPKG

ng-string-parser

Version:

thick as a brick Angular string parser service

69 lines (55 loc) 1.31 kB
## Requirements Angular 11.\*.\* ## Install `npm install ng-string-parser --save-dev`<br> or<br> `yarn add ng-string-parser --dev` ## Usage example ```typescript // some.component.ts import { ParserService } from "ng-string-parser"; interface IPatterns { [key: string]: string; } interface IValues { [key: string]: string | number | boolean; } interface IResult { [key: string]: string; } @Component({ // ... providers: [ParserService], }) export class SomeComponent implements OnInit { patterns: IPatterns = { first: "Boolean is {value}", second: "Number is {value}", third: "There is no pattern", fourth: "String is {value}", fifth: "No value for this one: {value}", }; values: IValues = { first: true, second: 42, fourth: "Bill Gates", }; results: IResult; constructor(private parser: ParserService) {} ngOnInit() { this.results = this.parser.parse<IResult>(this.patterns, this.values); } } ``` Result will be: ```typescript { first: "Boolean is true", second: "Number is 42", third: "There is no pattern", fourth: "String is Bill Gates", fifth: "No value for this one: {value}", } ``` ## Change pattern Default pattern is `{value}`. You can change it for each instance via `changePattern(newPattern: string)` public method.