From 613edccd076ac60bfd08bcabc5a058af17700ba9 Mon Sep 17 00:00:00 2001 From: Attila Reterics Date: Sat, 14 Oct 2023 10:35:19 +0200 Subject: [PATCH] Exported Parser to OOP Object --- src/constants.ts | 8 ++++++++ src/lejs.ts | 26 +++----------------------- src/parsers/index.ts | 21 +++++++++++++++++++++ src/parsers/variable.ts | 15 +++++++++++++++ src/types/parsers.ts | 8 ++++++++ 5 files changed, 55 insertions(+), 23 deletions(-) create mode 100644 src/constants.ts create mode 100644 src/parsers/index.ts create mode 100644 src/parsers/variable.ts create mode 100644 src/types/parsers.ts diff --git a/src/constants.ts b/src/constants.ts new file mode 100644 index 0000000..ce0ef54 --- /dev/null +++ b/src/constants.ts @@ -0,0 +1,8 @@ + +export const REGEXPS = { + variable: { + start: "{{", + end: "}}", + regexp: /{{\w*}}/g + } +} diff --git a/src/lejs.ts b/src/lejs.ts index e8b36a0..d75c808 100644 --- a/src/lejs.ts +++ b/src/lejs.ts @@ -23,32 +23,12 @@ 'use strict'; import * as fs from 'fs' import * as path from 'path' +import Parser from "./parsers"; -const REGEXPS = { - variable: { - start: "{{", - end: "}}", - regexp: /{{\w*}}/g - } -} - -/** - * - * @param {string} string - * @param {object} data - * @param {undefined|object} options - * @returns {*} - */ export function render (string: string, data, options: object|undefined) { - 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 ""; - }); + const parser = new Parser({cache: false}); + return parser.render(string, data); } export function renderFile (file: string, data: object|undefined, options: object|undefined) { diff --git a/src/parsers/index.ts b/src/parsers/index.ts new file mode 100644 index 0000000..36a01cc --- /dev/null +++ b/src/parsers/index.ts @@ -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; \ No newline at end of file diff --git a/src/parsers/variable.ts b/src/parsers/variable.ts new file mode 100644 index 0000000..c3c84e0 --- /dev/null +++ b/src/parsers/variable.ts @@ -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; diff --git a/src/types/parsers.ts b/src/types/parsers.ts new file mode 100644 index 0000000..8deeae5 --- /dev/null +++ b/src/types/parsers.ts @@ -0,0 +1,8 @@ + +export interface ParserOptions { + cache?: boolean +} + +export interface ParserFunc { + (string: string, data: (object | null)): string +} \ No newline at end of file