Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
55 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
|
||
export const REGEXPS = { | ||
variable: { | ||
start: "{{", | ||
end: "}}", | ||
regexp: /{{\w*}}/g | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import {ParserOptions, ParserFunc} from "../types/parsers"; | ||
import renderVariable from "./variable"; | ||
|
||
class Parser { | ||
private parsers: ParserFunc[] | ||
private cache: boolean; | ||
constructor(options: ParserOptions|undefined) { | ||
this.parsers = [ | ||
renderVariable | ||
]; | ||
this.cache = !!(options && options.cache); // TODO: ROADMAP | ||
} | ||
|
||
render(string: string, data: (object | null)) { | ||
return this.parsers.reduce((str, parser) => { | ||
return parser(str, data); | ||
}, string); | ||
} | ||
} | ||
|
||
export default Parser; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import {REGEXPS} from "../constants"; | ||
import {ParserFunc} from "../types/parsers"; | ||
|
||
|
||
const renderVariable: ParserFunc = (string: string, data: object|null): string => { | ||
const config = REGEXPS.variable; | ||
return string.replaceAll(config.regexp, (substr) => { | ||
const variable = substr.substring(config.start.length, substr.length - config.end.length); | ||
if (data && data.hasOwnProperty(variable)) { | ||
return data[variable]; | ||
} | ||
return ""; | ||
}); | ||
} | ||
export default renderVariable; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
|
||
export interface ParserOptions { | ||
cache?: boolean | ||
} | ||
|
||
export interface ParserFunc { | ||
(string: string, data: (object | null)): string | ||
} |