ng-string-parser
Version:
thick as a brick Angular string parser service
69 lines (55 loc) • 1.31 kB
Markdown
Angular 11.\*.\*
`npm install ng-string-parser --save-dev`<br>
or<br>
`yarn add ng-string-parser --dev`
```typescript
// some.component.ts
import { ParserService } from "ng-string-parser";
interface IPatterns {
[]: string;
}
interface IValues {
[]: string | number | boolean;
}
interface IResult {
[]: 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}",
}
```
Default pattern is `{value}`. You can change it for each instance via `changePattern(newPattern: string)` public method.