jcc-express-mvc 1.0.16 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "jcc-express-mvc",
3
- "version": "1.0.16",
3
+ "version": "1.1.0",
4
4
  "description": "express mvc structure",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -5,18 +5,19 @@ class Template {
5
5
  #directives = {
6
6
  extends: /@extends\((?<extendPath>['"]([^'"]+)['"])\)/,
7
7
  foreach:
8
- /@foreach \s*\((?<loopvariable>[a-zA-Z0-9_-]+) in (?<loopData>[a-zA-Z0-9._-]+)\)(?<loopBody>[\s\S\t\n\r]+?)@endforeach/,
9
- if: /@if \s*\((?<ifData>[a-zA-Z0-9._-]+)\)(?<ifBody>[\s\S]+?)@else(?<elseBody>[\s\S]+?)@endif/,
8
+ /@foreach\s*\((?<loopvariable>(.*?)+) in (?<loopData>(.*?)+)\)(?<loopBody>[\s\S\t\n\r]+?)@endforeach/,
9
+ if: /@if\s*\((?<ifData>(.*?))\)\s*(?<ifBody>[\s\S]*?)\s*(?:@else\s*(?<elseBody>[\s\S]*?)\s*)?@endif/g,
10
10
  include: /@include\((?<includePath>['"]([^'"]+)['"])\)/,
11
11
  ternary:
12
- /@ternary \S*\((?<condition>[a-zA-Z0-9_.-]+)\s*\?\s*(?<trueData>[\s\S]+?)\s*:\s*(?<falseData>[\s\S]+?)\)/,
12
+ /@ternary\s*\((?<condition>[a-zA-Z0-9_.-]+)\s*\?\s*(?<trueData>[\s\S]+?)\s*:\s*(?<falseData>[\s\S]+?)\)/,
13
13
  variable: /{{\s*(?<param>(.*?))\s*}}/,
14
- auth: /@auth\s*(?:(\((?<authData>[^)]*)\)))?(?<authBody>[\s\S]*?)@endauth/,
14
+ auth: /@auth\s*(?:(\((?<authData>(.*?)*)\)))?(?<authBody>[\s\S]*?)@endauth/,
15
15
  guest: /@guest\s*(?<guestBody>[\s\S]*?)@endguest/,
16
16
  };
17
17
 
18
18
  constructor() {
19
19
  this.option = {};
20
+ this.cache = new Map();
20
21
  }
21
22
 
22
23
  #handleVariable(content, match, param) {
@@ -31,12 +32,14 @@ class Template {
31
32
  const loopData = Util.accessNestedProperty(param, match?.groups.loopData);
32
33
  if (Array.isArray(loopData)) {
33
34
  const loopBody = match.groups.loopBody;
35
+
34
36
  result = loopData
35
37
  .map((item, index) => {
36
38
  const loopContext = {
37
39
  ...param,
38
- [match.groups.loopvariable]: { ...item, loopIndex: index },
40
+ [match.groups.loopvariable]: item,
39
41
  };
42
+ loopContext[match.groups.loopvariable]["loopIndex"] = index;
40
43
  return this.#parser(loopBody + "@endforeach", loopContext);
41
44
  })
42
45
  .join("");
@@ -49,7 +52,10 @@ class Template {
49
52
 
50
53
  #handleCondition(content, match, param) {
51
54
  let result = "";
52
- const value = Util.accessNestedProperty(param, match?.groups?.ifData);
55
+ const value =
56
+ Util.accessNestedProperty(param, match?.groups?.ifData) ??
57
+ match?.groups.ifData;
58
+
53
59
  if (value) {
54
60
  result = this.#parser(match.groups?.ifBody, param);
55
61
  } else {
@@ -73,7 +79,7 @@ class Template {
73
79
  return beforMatch + result + this.#parser(afterMatch, param);
74
80
  }
75
81
 
76
- #extends(content) {
82
+ async #extends(content) {
77
83
  const match = content.match(this.#directives.extends);
78
84
  if (!match) return content;
79
85
  const layoutPath = match.groups?.extendPath;
@@ -101,7 +107,16 @@ class Template {
101
107
  #auth(content, match, param) {
102
108
  let result = "";
103
109
  if (Object.values(param?.Auth).length > 1) {
104
- result = match?.groups?.authBody;
110
+ const authData = match?.groups?.authData?.replace(/(\'|\")/g, "");
111
+ if (
112
+ (authData && param?.Auth?.role === authData) ||
113
+ param?.Auth?.roleType === authData
114
+ ) {
115
+ result = match?.groups?.authBody;
116
+ }
117
+ if (!authData) {
118
+ result = match?.groups?.authBody;
119
+ }
105
120
  }
106
121
  const { beforMatch, afterMatch } = Util.content(content, match);
107
122
  return this.#parser(beforMatch + result + afterMatch, param);
@@ -151,22 +166,29 @@ class Template {
151
166
  }
152
167
  }
153
168
 
154
- #compiler(content) {
155
- const newContent = this.#extends(content)
156
- .replace(/(\{\{\-\-)/g, "<!--")
157
- .replace(/(\-\-\}\})/g, "-->");
169
+ async #compiler(content) {
170
+ const newContent = await this.#extends(content);
158
171
  const finalContent = this.#parser(newContent);
159
172
  return finalContent
160
- .replace(/(\{\{\-\-)/g, "<!--")
161
- .replace(/(\-\-\}\})/g, "-->");
173
+ .replace(/(\{\{\-\-)/gm, "<!--")
174
+ .replace(/(\-\-\}\})/gm, "-->");
175
+ }
176
+
177
+ async #renderFromCache(templatePath) {
178
+ const renderedContent = this.cache.get(templatePath);
179
+ return await this.#compiler(renderedContent);
162
180
  }
163
- render(filePath, locals, callback) {
181
+ async render(filePath, locals, callback) {
164
182
  try {
165
- return fs.readFile(filePath, "utf-8", (err, content) => {
166
- if (err) return callback(err);
167
- this.option = locals;
168
- return callback(null, this.#compiler(content));
169
- });
183
+ // if (this.cache.has(filePath)) {
184
+ // return callback(null, await this.#renderFromCache(filePath));
185
+ // }
186
+ const content = fs.readFileSync(filePath, "utf-8");
187
+ this.option = locals;
188
+ const renderedContent = await this.#compiler(content);
189
+ // this.cache.set(filePath, renderedContent);
190
+
191
+ return callback(null, renderedContent);
170
192
  } catch (error) {
171
193
  throw new Error(error?.message);
172
194
  }