@oino-ts/types 0.0.13 → 0.0.15

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.
@@ -26,21 +26,23 @@ class OINOBenchmark {
26
26
  /**
27
27
  * Set benchmark names that are enabled.
28
28
  *
29
- * @param names array of those benchmarks that are enabled
29
+ * @param module array of those benchmarks that are enabled
30
30
  */
31
- static setEnabled(names) {
31
+ static setEnabled(module) {
32
32
  this._benchmarkEnabled = {};
33
- names.forEach(name => {
34
- this._benchmarkEnabled[name] = true;
33
+ module.forEach(module_name => {
34
+ this._benchmarkEnabled[module_name] = true;
35
35
  });
36
36
  }
37
37
  /**
38
38
  * Start benchmark timing.
39
39
  *
40
- * @param name of the benchmark
40
+ * @param module of the benchmark
41
+ * @param method of the benchmark
41
42
  */
42
- static start(name) {
43
- if (this._benchmarkEnabled[name]) {
43
+ static start(module, method) {
44
+ const name = module + "." + method;
45
+ if (this._benchmarkEnabled[module]) {
44
46
  if (this._benchmarkCount[name] == undefined) {
45
47
  this._benchmarkCount[name] = 0;
46
48
  this._benchmarkData[name] = 0;
@@ -51,12 +53,14 @@ class OINOBenchmark {
51
53
  /**
52
54
  * Complete benchmark timing
53
55
  *
54
- * @param name of the benchmark
56
+ * @param module of the benchmark
57
+ * @param method of the benchmark
55
58
  * @param category optional subcategory of the benchmark
56
59
  */
57
- static end(name, category) {
60
+ static end(module, method, category) {
61
+ const name = module + "." + method;
58
62
  let result = 0;
59
- if (this._benchmarkEnabled[name]) {
63
+ if (this._benchmarkEnabled[module]) {
60
64
  const duration = performance.now() - this._benchmarkStart[name];
61
65
  this._benchmarkCount[name] += 1;
62
66
  this._benchmarkData[name] += duration;
@@ -76,11 +80,14 @@ class OINOBenchmark {
76
80
  /**
77
81
  * Get given benchmark data.
78
82
  *
79
- * @param name of the benchmark
83
+ * @param module of the benchmark
84
+ * @param method of the benchmark
85
+ *
80
86
  */
81
- static get(name) {
82
- if (this._benchmarkEnabled[name]) {
83
- return this._benchmarkData[name] / this._benchmarkCount[name];
87
+ static get(module, method) {
88
+ const name = module + "." + method;
89
+ if (this._benchmarkEnabled[module]) {
90
+ return this._benchmarkData[module] / this._benchmarkCount[module];
84
91
  }
85
92
  return -1;
86
93
  }
@@ -6,6 +6,9 @@ const _1 = require(".");
6
6
  * Class for rendering HTML from data.
7
7
  */
8
8
  class OINOHtmlTemplate {
9
+ _tag;
10
+ _tagCleanRegex;
11
+ _variables = {};
9
12
  /** HTML template string */
10
13
  template;
11
14
  /** Cache modified value for template */
@@ -16,12 +19,15 @@ class OINOHtmlTemplate {
16
19
  * Creates HTML Response from a key-value-pair.
17
20
  *
18
21
  * @param template template string
22
+ * @param tag tag to identify variables in template
19
23
  *
20
24
  */
21
- constructor(template) {
25
+ constructor(template, tag = "###") {
22
26
  this.template = template;
23
27
  this.modified = 0;
24
28
  this.expires = 0;
29
+ this._tag = tag;
30
+ this._tagCleanRegex = new RegExp(tag + ".*" + tag, "g");
25
31
  }
26
32
  /**
27
33
  * @returns whether template is empty
@@ -29,15 +35,10 @@ class OINOHtmlTemplate {
29
35
  isEmpty() {
30
36
  return this.template == "";
31
37
  }
32
- /**
33
- * Creates HTML Response from a key-value-pair.
34
- *
35
- * @param key key
36
- * @param value value
37
- *
38
- */
39
- renderFromKeyValue(key, value) {
40
- const html = this.template.replaceAll('###' + key + '###', _1.OINOStr.encode(value, _1.OINOContentType.html));
38
+ _createHttpResult(html, removeUnusedTags) {
39
+ if (removeUnusedTags) {
40
+ html = html.replace(this._tagCleanRegex, "");
41
+ }
41
42
  const result = new _1.OINOHttpResult(html);
42
43
  if (this.expires >= 1) {
43
44
  result.expires = Math.round(this.expires);
@@ -47,72 +48,128 @@ class OINOHtmlTemplate {
47
48
  }
48
49
  return result;
49
50
  }
51
+ _renderHtml() {
52
+ let html = this.template;
53
+ for (let key in this._variables) {
54
+ const value = this._variables[key];
55
+ html = html.replaceAll(this._tag + key + this._tag, value);
56
+ }
57
+ return html;
58
+ }
50
59
  /**
51
- * Creates HTML Response from object properties.
60
+ * Clear template variables.
52
61
  *
53
- * @param object object
62
+ */
63
+ clearVariables() {
64
+ this._variables = {};
65
+ }
66
+ /**
67
+ * Sets template variable from a key-value-pair.
68
+ *
69
+ * @param variable key
70
+ * @param value value
71
+ * @param escapeValue whether to escape value
54
72
  *
55
73
  */
56
- renderFromObject(object) {
57
- let html = this.template;
74
+ setVariableFromValue(variable, value, escapeValue = true) {
75
+ if (escapeValue) {
76
+ value = _1.OINOStr.encode(value, _1.OINOContentType.html);
77
+ }
78
+ this._variables[variable] = value;
79
+ }
80
+ /**
81
+ * Sets template variables from object properties.
82
+ *
83
+ * @param object any object
84
+ * @param escapeValue whether to escape value
85
+ *
86
+ */
87
+ setVariableFromProperties(object, escapeValue = true) {
58
88
  if (object) {
59
89
  for (let key in object) {
60
- const value = object[key];
61
- if (value) {
62
- html = html.replaceAll('###' + key + '###', _1.OINOStr.encode(value.toString(), _1.OINOContentType.html));
90
+ if (escapeValue) {
91
+ this._variables[key] = _1.OINOStr.encode(object[key], _1.OINOContentType.html);
92
+ }
93
+ else {
94
+ this._variables[key] = object[key];
63
95
  }
64
96
  }
65
97
  }
66
- html = html.replace(/###[^#]*###/g, "");
67
- const result = new _1.OINOHttpResult(html);
68
- if (this.expires >= 1) {
69
- result.expires = Math.round(this.expires);
70
- }
71
- if (this.modified >= 1) {
72
- result.lastModified = this.modified;
73
- }
98
+ }
99
+ /**
100
+ * Creates HTML Response from set variables.
101
+ *
102
+ * @param removeUnusedTags whether to remove unused tags
103
+ *
104
+ */
105
+ render(removeUnusedTags = true) {
106
+ return this._createHttpResult(this._renderHtml(), removeUnusedTags);
107
+ }
108
+ /**
109
+ * Creates HTML Response from a key-value-pair.
110
+ *
111
+ * @param key key
112
+ * @param value value
113
+ * @param removeUnusedTags whether to remove unused tags
114
+ *
115
+ */
116
+ renderFromKeyValue(key, value, removeUnusedTags = true) {
117
+ _1.OINOBenchmark.start("OINOHtmlTemplate", "renderFromKeyValue");
118
+ this.setVariableFromValue(key, value);
119
+ const result = this.render(removeUnusedTags);
120
+ _1.OINOBenchmark.end("OINOHtmlTemplate", "renderFromKeyValue");
121
+ return result;
122
+ }
123
+ /**
124
+ * Creates HTML Response from object properties.
125
+ *
126
+ * @param object object
127
+ * @param removeUnusedTags whether to remove unused tags
128
+ *
129
+ */
130
+ renderFromObject(object, removeUnusedTags = true) {
131
+ _1.OINOBenchmark.start("OINOHtmlTemplate", "renderFromObject");
132
+ this.setVariableFromProperties(object);
133
+ const result = this.render(removeUnusedTags);
134
+ _1.OINOBenchmark.end("OINOHtmlTemplate", "renderFromObject");
74
135
  return result;
75
136
  }
76
137
  /**
77
138
  * Creates HTML Response from API result.
78
139
  *
79
140
  * @param result OINOResult-object
141
+ * @param removeUnusedTags whether to remove unused tags
142
+ * @param messageSeparator HTML separator for messages
80
143
  * @param includeErrorMessages include debug messages in result
81
144
  * @param includeWarningMessages include debug messages in result
82
145
  * @param includeInfoMessages include debug messages in result
83
146
  * @param includeDebugMessages include debug messages in result
84
147
  *
85
148
  */
86
- renderFromResult(result, includeErrorMessages = false, includeWarningMessages = false, includeInfoMessages = false, includeDebugMessages = false) {
87
- let html = this.template;
88
- html = html.replaceAll('###statusCode###', _1.OINOStr.encode(result.statusCode.toString(), _1.OINOContentType.html));
89
- html = html.replaceAll('###statusMessage###', _1.OINOStr.encode(result.statusMessage.toString(), _1.OINOContentType.html));
90
- let messages = "";
149
+ renderFromResult(result, removeUnusedTags = true, messageSeparator, includeErrorMessages = false, includeWarningMessages = false, includeInfoMessages = false, includeDebugMessages = false) {
150
+ _1.OINOBenchmark.start("OINOHtmlTemplate", "renderFromResult");
151
+ this.setVariableFromValue("statusCode", result.statusCode.toString());
152
+ this.setVariableFromValue("statusMessage", result.statusMessage.toString());
153
+ let messages = [];
91
154
  for (let i = 0; i < result.messages.length; i++) {
92
155
  if (includeErrorMessages && result.messages[i].startsWith(_1.OINO_ERROR_PREFIX)) {
93
- messages += "<li>" + _1.OINOStr.encode(result.messages[i], _1.OINOContentType.html) + "</li>";
156
+ messages.push(_1.OINOStr.encode(result.messages[i], _1.OINOContentType.html));
94
157
  }
95
158
  if (includeWarningMessages && result.messages[i].startsWith(_1.OINO_WARNING_PREFIX)) {
96
- messages += "<li>" + _1.OINOStr.encode(result.messages[i], _1.OINOContentType.html) + "</li>";
159
+ messages.push(_1.OINOStr.encode(result.messages[i], _1.OINOContentType.html));
97
160
  }
98
161
  if (includeInfoMessages && result.messages[i].startsWith(_1.OINO_INFO_PREFIX)) {
99
- messages += "<li>" + _1.OINOStr.encode(result.messages[i], _1.OINOContentType.html) + "</li>";
162
+ messages.push(_1.OINOStr.encode(result.messages[i], _1.OINOContentType.html));
100
163
  }
101
164
  if (includeDebugMessages && result.messages[i].startsWith(_1.OINO_DEBUG_PREFIX)) {
102
- messages += "<li>" + _1.OINOStr.encode(result.messages[i], _1.OINOContentType.html) + "</li>";
165
+ messages.push(_1.OINOStr.encode(result.messages[i], _1.OINOContentType.html));
103
166
  }
104
167
  }
105
- if (messages) {
106
- html = html.replaceAll('###messages###', "<ul>" + messages + "</ul>");
107
- }
108
- html = html.replace(/###[^#]*###/g, "");
109
- const http_result = new _1.OINOHttpResult(html);
110
- if (this.expires >= 1) {
111
- http_result.expires = Math.round(this.expires);
112
- }
113
- if (this.modified >= 1) {
114
- http_result.lastModified = this.modified;
168
+ if (messages.length > 0) {
169
+ this.setVariableFromValue("messages", messages.join(messageSeparator), false); // messages have been escaped already
115
170
  }
171
+ const http_result = this.render(removeUnusedTags);
172
+ _1.OINOBenchmark.end("OINOHtmlTemplate", "renderFromResult");
116
173
  return http_result;
117
174
  }
118
175
  }
package/dist/cjs/index.js CHANGED
@@ -22,7 +22,9 @@ exports.OINO_WARNING_PREFIX = "OINO WARNING";
22
22
  exports.OINO_INFO_PREFIX = "OINO INFO";
23
23
  /** OINO debug message prefix */
24
24
  exports.OINO_DEBUG_PREFIX = "OINO DEBUG";
25
- /** Supported content format mime-types */
25
+ /**
26
+ * Supported content format mime-types
27
+ */
26
28
  var OINOContentType;
27
29
  (function (OINOContentType) {
28
30
  /** JSON encoded data */
@@ -23,21 +23,23 @@ export class OINOBenchmark {
23
23
  /**
24
24
  * Set benchmark names that are enabled.
25
25
  *
26
- * @param names array of those benchmarks that are enabled
26
+ * @param module array of those benchmarks that are enabled
27
27
  */
28
- static setEnabled(names) {
28
+ static setEnabled(module) {
29
29
  this._benchmarkEnabled = {};
30
- names.forEach(name => {
31
- this._benchmarkEnabled[name] = true;
30
+ module.forEach(module_name => {
31
+ this._benchmarkEnabled[module_name] = true;
32
32
  });
33
33
  }
34
34
  /**
35
35
  * Start benchmark timing.
36
36
  *
37
- * @param name of the benchmark
37
+ * @param module of the benchmark
38
+ * @param method of the benchmark
38
39
  */
39
- static start(name) {
40
- if (this._benchmarkEnabled[name]) {
40
+ static start(module, method) {
41
+ const name = module + "." + method;
42
+ if (this._benchmarkEnabled[module]) {
41
43
  if (this._benchmarkCount[name] == undefined) {
42
44
  this._benchmarkCount[name] = 0;
43
45
  this._benchmarkData[name] = 0;
@@ -48,12 +50,14 @@ export class OINOBenchmark {
48
50
  /**
49
51
  * Complete benchmark timing
50
52
  *
51
- * @param name of the benchmark
53
+ * @param module of the benchmark
54
+ * @param method of the benchmark
52
55
  * @param category optional subcategory of the benchmark
53
56
  */
54
- static end(name, category) {
57
+ static end(module, method, category) {
58
+ const name = module + "." + method;
55
59
  let result = 0;
56
- if (this._benchmarkEnabled[name]) {
60
+ if (this._benchmarkEnabled[module]) {
57
61
  const duration = performance.now() - this._benchmarkStart[name];
58
62
  this._benchmarkCount[name] += 1;
59
63
  this._benchmarkData[name] += duration;
@@ -73,11 +77,14 @@ export class OINOBenchmark {
73
77
  /**
74
78
  * Get given benchmark data.
75
79
  *
76
- * @param name of the benchmark
80
+ * @param module of the benchmark
81
+ * @param method of the benchmark
82
+ *
77
83
  */
78
- static get(name) {
79
- if (this._benchmarkEnabled[name]) {
80
- return this._benchmarkData[name] / this._benchmarkCount[name];
84
+ static get(module, method) {
85
+ const name = module + "." + method;
86
+ if (this._benchmarkEnabled[module]) {
87
+ return this._benchmarkData[module] / this._benchmarkCount[module];
81
88
  }
82
89
  return -1;
83
90
  }
@@ -1,8 +1,11 @@
1
- import { OINOStr, OINOContentType, OINOHttpResult, OINO_ERROR_PREFIX, OINO_WARNING_PREFIX, OINO_INFO_PREFIX, OINO_DEBUG_PREFIX } from ".";
1
+ import { OINOStr, OINOContentType, OINOHttpResult, OINO_ERROR_PREFIX, OINO_WARNING_PREFIX, OINO_INFO_PREFIX, OINO_DEBUG_PREFIX, OINOBenchmark } from ".";
2
2
  /**
3
3
  * Class for rendering HTML from data.
4
4
  */
5
5
  export class OINOHtmlTemplate {
6
+ _tag;
7
+ _tagCleanRegex;
8
+ _variables = {};
6
9
  /** HTML template string */
7
10
  template;
8
11
  /** Cache modified value for template */
@@ -13,12 +16,15 @@ export class OINOHtmlTemplate {
13
16
  * Creates HTML Response from a key-value-pair.
14
17
  *
15
18
  * @param template template string
19
+ * @param tag tag to identify variables in template
16
20
  *
17
21
  */
18
- constructor(template) {
22
+ constructor(template, tag = "###") {
19
23
  this.template = template;
20
24
  this.modified = 0;
21
25
  this.expires = 0;
26
+ this._tag = tag;
27
+ this._tagCleanRegex = new RegExp(tag + ".*" + tag, "g");
22
28
  }
23
29
  /**
24
30
  * @returns whether template is empty
@@ -26,15 +32,10 @@ export class OINOHtmlTemplate {
26
32
  isEmpty() {
27
33
  return this.template == "";
28
34
  }
29
- /**
30
- * Creates HTML Response from a key-value-pair.
31
- *
32
- * @param key key
33
- * @param value value
34
- *
35
- */
36
- renderFromKeyValue(key, value) {
37
- const html = this.template.replaceAll('###' + key + '###', OINOStr.encode(value, OINOContentType.html));
35
+ _createHttpResult(html, removeUnusedTags) {
36
+ if (removeUnusedTags) {
37
+ html = html.replace(this._tagCleanRegex, "");
38
+ }
38
39
  const result = new OINOHttpResult(html);
39
40
  if (this.expires >= 1) {
40
41
  result.expires = Math.round(this.expires);
@@ -44,72 +45,128 @@ export class OINOHtmlTemplate {
44
45
  }
45
46
  return result;
46
47
  }
48
+ _renderHtml() {
49
+ let html = this.template;
50
+ for (let key in this._variables) {
51
+ const value = this._variables[key];
52
+ html = html.replaceAll(this._tag + key + this._tag, value);
53
+ }
54
+ return html;
55
+ }
47
56
  /**
48
- * Creates HTML Response from object properties.
57
+ * Clear template variables.
49
58
  *
50
- * @param object object
59
+ */
60
+ clearVariables() {
61
+ this._variables = {};
62
+ }
63
+ /**
64
+ * Sets template variable from a key-value-pair.
65
+ *
66
+ * @param variable key
67
+ * @param value value
68
+ * @param escapeValue whether to escape value
51
69
  *
52
70
  */
53
- renderFromObject(object) {
54
- let html = this.template;
71
+ setVariableFromValue(variable, value, escapeValue = true) {
72
+ if (escapeValue) {
73
+ value = OINOStr.encode(value, OINOContentType.html);
74
+ }
75
+ this._variables[variable] = value;
76
+ }
77
+ /**
78
+ * Sets template variables from object properties.
79
+ *
80
+ * @param object any object
81
+ * @param escapeValue whether to escape value
82
+ *
83
+ */
84
+ setVariableFromProperties(object, escapeValue = true) {
55
85
  if (object) {
56
86
  for (let key in object) {
57
- const value = object[key];
58
- if (value) {
59
- html = html.replaceAll('###' + key + '###', OINOStr.encode(value.toString(), OINOContentType.html));
87
+ if (escapeValue) {
88
+ this._variables[key] = OINOStr.encode(object[key], OINOContentType.html);
89
+ }
90
+ else {
91
+ this._variables[key] = object[key];
60
92
  }
61
93
  }
62
94
  }
63
- html = html.replace(/###[^#]*###/g, "");
64
- const result = new OINOHttpResult(html);
65
- if (this.expires >= 1) {
66
- result.expires = Math.round(this.expires);
67
- }
68
- if (this.modified >= 1) {
69
- result.lastModified = this.modified;
70
- }
95
+ }
96
+ /**
97
+ * Creates HTML Response from set variables.
98
+ *
99
+ * @param removeUnusedTags whether to remove unused tags
100
+ *
101
+ */
102
+ render(removeUnusedTags = true) {
103
+ return this._createHttpResult(this._renderHtml(), removeUnusedTags);
104
+ }
105
+ /**
106
+ * Creates HTML Response from a key-value-pair.
107
+ *
108
+ * @param key key
109
+ * @param value value
110
+ * @param removeUnusedTags whether to remove unused tags
111
+ *
112
+ */
113
+ renderFromKeyValue(key, value, removeUnusedTags = true) {
114
+ OINOBenchmark.start("OINOHtmlTemplate", "renderFromKeyValue");
115
+ this.setVariableFromValue(key, value);
116
+ const result = this.render(removeUnusedTags);
117
+ OINOBenchmark.end("OINOHtmlTemplate", "renderFromKeyValue");
118
+ return result;
119
+ }
120
+ /**
121
+ * Creates HTML Response from object properties.
122
+ *
123
+ * @param object object
124
+ * @param removeUnusedTags whether to remove unused tags
125
+ *
126
+ */
127
+ renderFromObject(object, removeUnusedTags = true) {
128
+ OINOBenchmark.start("OINOHtmlTemplate", "renderFromObject");
129
+ this.setVariableFromProperties(object);
130
+ const result = this.render(removeUnusedTags);
131
+ OINOBenchmark.end("OINOHtmlTemplate", "renderFromObject");
71
132
  return result;
72
133
  }
73
134
  /**
74
135
  * Creates HTML Response from API result.
75
136
  *
76
137
  * @param result OINOResult-object
138
+ * @param removeUnusedTags whether to remove unused tags
139
+ * @param messageSeparator HTML separator for messages
77
140
  * @param includeErrorMessages include debug messages in result
78
141
  * @param includeWarningMessages include debug messages in result
79
142
  * @param includeInfoMessages include debug messages in result
80
143
  * @param includeDebugMessages include debug messages in result
81
144
  *
82
145
  */
83
- renderFromResult(result, includeErrorMessages = false, includeWarningMessages = false, includeInfoMessages = false, includeDebugMessages = false) {
84
- let html = this.template;
85
- html = html.replaceAll('###statusCode###', OINOStr.encode(result.statusCode.toString(), OINOContentType.html));
86
- html = html.replaceAll('###statusMessage###', OINOStr.encode(result.statusMessage.toString(), OINOContentType.html));
87
- let messages = "";
146
+ renderFromResult(result, removeUnusedTags = true, messageSeparator, includeErrorMessages = false, includeWarningMessages = false, includeInfoMessages = false, includeDebugMessages = false) {
147
+ OINOBenchmark.start("OINOHtmlTemplate", "renderFromResult");
148
+ this.setVariableFromValue("statusCode", result.statusCode.toString());
149
+ this.setVariableFromValue("statusMessage", result.statusMessage.toString());
150
+ let messages = [];
88
151
  for (let i = 0; i < result.messages.length; i++) {
89
152
  if (includeErrorMessages && result.messages[i].startsWith(OINO_ERROR_PREFIX)) {
90
- messages += "<li>" + OINOStr.encode(result.messages[i], OINOContentType.html) + "</li>";
153
+ messages.push(OINOStr.encode(result.messages[i], OINOContentType.html));
91
154
  }
92
155
  if (includeWarningMessages && result.messages[i].startsWith(OINO_WARNING_PREFIX)) {
93
- messages += "<li>" + OINOStr.encode(result.messages[i], OINOContentType.html) + "</li>";
156
+ messages.push(OINOStr.encode(result.messages[i], OINOContentType.html));
94
157
  }
95
158
  if (includeInfoMessages && result.messages[i].startsWith(OINO_INFO_PREFIX)) {
96
- messages += "<li>" + OINOStr.encode(result.messages[i], OINOContentType.html) + "</li>";
159
+ messages.push(OINOStr.encode(result.messages[i], OINOContentType.html));
97
160
  }
98
161
  if (includeDebugMessages && result.messages[i].startsWith(OINO_DEBUG_PREFIX)) {
99
- messages += "<li>" + OINOStr.encode(result.messages[i], OINOContentType.html) + "</li>";
162
+ messages.push(OINOStr.encode(result.messages[i], OINOContentType.html));
100
163
  }
101
164
  }
102
- if (messages) {
103
- html = html.replaceAll('###messages###', "<ul>" + messages + "</ul>");
104
- }
105
- html = html.replace(/###[^#]*###/g, "");
106
- const http_result = new OINOHttpResult(html);
107
- if (this.expires >= 1) {
108
- http_result.expires = Math.round(this.expires);
109
- }
110
- if (this.modified >= 1) {
111
- http_result.lastModified = this.modified;
165
+ if (messages.length > 0) {
166
+ this.setVariableFromValue("messages", messages.join(messageSeparator), false); // messages have been escaped already
112
167
  }
168
+ const http_result = this.render(removeUnusedTags);
169
+ OINOBenchmark.end("OINOHtmlTemplate", "renderFromResult");
113
170
  return http_result;
114
171
  }
115
172
  }
package/dist/esm/index.js CHANGED
@@ -11,7 +11,9 @@ export const OINO_WARNING_PREFIX = "OINO WARNING";
11
11
  export const OINO_INFO_PREFIX = "OINO INFO";
12
12
  /** OINO debug message prefix */
13
13
  export const OINO_DEBUG_PREFIX = "OINO DEBUG";
14
- /** Supported content format mime-types */
14
+ /**
15
+ * Supported content format mime-types
16
+ */
15
17
  export var OINOContentType;
16
18
  (function (OINOContentType) {
17
19
  /** JSON encoded data */
@@ -15,28 +15,32 @@ export declare class OINOBenchmark {
15
15
  /**
16
16
  * Set benchmark names that are enabled.
17
17
  *
18
- * @param names array of those benchmarks that are enabled
18
+ * @param module array of those benchmarks that are enabled
19
19
  */
20
- static setEnabled(names: string[]): void;
20
+ static setEnabled(module: string[]): void;
21
21
  /**
22
22
  * Start benchmark timing.
23
23
  *
24
- * @param name of the benchmark
24
+ * @param module of the benchmark
25
+ * @param method of the benchmark
25
26
  */
26
- static start(name: string): void;
27
+ static start(module: string, method: string): void;
27
28
  /**
28
29
  * Complete benchmark timing
29
30
  *
30
- * @param name of the benchmark
31
+ * @param module of the benchmark
32
+ * @param method of the benchmark
31
33
  * @param category optional subcategory of the benchmark
32
34
  */
33
- static end(name: string, category?: string): number;
35
+ static end(module: string, method: string, category?: string): number;
34
36
  /**
35
37
  * Get given benchmark data.
36
38
  *
37
- * @param name of the benchmark
39
+ * @param module of the benchmark
40
+ * @param method of the benchmark
41
+ *
38
42
  */
39
- static get(name: string): number;
43
+ static get(module: string, method: string): number;
40
44
  /**
41
45
  * Get all benchmark data.
42
46
  *
@@ -3,6 +3,9 @@ import { OINOResult, OINOHttpResult } from ".";
3
3
  * Class for rendering HTML from data.
4
4
  */
5
5
  export declare class OINOHtmlTemplate {
6
+ private _tag;
7
+ private _tagCleanRegex;
8
+ private _variables;
6
9
  /** HTML template string */
7
10
  template: string;
8
11
  /** Cache modified value for template */
@@ -13,37 +16,73 @@ export declare class OINOHtmlTemplate {
13
16
  * Creates HTML Response from a key-value-pair.
14
17
  *
15
18
  * @param template template string
19
+ * @param tag tag to identify variables in template
16
20
  *
17
21
  */
18
- constructor(template: string);
22
+ constructor(template: string, tag?: string);
19
23
  /**
20
24
  * @returns whether template is empty
21
25
  */
22
26
  isEmpty(): boolean;
27
+ protected _createHttpResult(html: string, removeUnusedTags: boolean): OINOHttpResult;
28
+ protected _renderHtml(): string;
29
+ /**
30
+ * Clear template variables.
31
+ *
32
+ */
33
+ clearVariables(): void;
34
+ /**
35
+ * Sets template variable from a key-value-pair.
36
+ *
37
+ * @param variable key
38
+ * @param value value
39
+ * @param escapeValue whether to escape value
40
+ *
41
+ */
42
+ setVariableFromValue(variable: string, value: string, escapeValue?: boolean): void;
43
+ /**
44
+ * Sets template variables from object properties.
45
+ *
46
+ * @param object any object
47
+ * @param escapeValue whether to escape value
48
+ *
49
+ */
50
+ setVariableFromProperties(object: any, escapeValue?: boolean): void;
51
+ /**
52
+ * Creates HTML Response from set variables.
53
+ *
54
+ * @param removeUnusedTags whether to remove unused tags
55
+ *
56
+ */
57
+ render(removeUnusedTags?: boolean): OINOHttpResult;
23
58
  /**
24
59
  * Creates HTML Response from a key-value-pair.
25
60
  *
26
61
  * @param key key
27
62
  * @param value value
63
+ * @param removeUnusedTags whether to remove unused tags
28
64
  *
29
65
  */
30
- renderFromKeyValue(key: string, value: string): OINOHttpResult;
66
+ renderFromKeyValue(key: string, value: string, removeUnusedTags?: boolean): OINOHttpResult;
31
67
  /**
32
68
  * Creates HTML Response from object properties.
33
69
  *
34
70
  * @param object object
71
+ * @param removeUnusedTags whether to remove unused tags
35
72
  *
36
73
  */
37
- renderFromObject(object: any): OINOHttpResult;
74
+ renderFromObject(object: any, removeUnusedTags?: boolean): OINOHttpResult;
38
75
  /**
39
76
  * Creates HTML Response from API result.
40
77
  *
41
78
  * @param result OINOResult-object
79
+ * @param removeUnusedTags whether to remove unused tags
80
+ * @param messageSeparator HTML separator for messages
42
81
  * @param includeErrorMessages include debug messages in result
43
82
  * @param includeWarningMessages include debug messages in result
44
83
  * @param includeInfoMessages include debug messages in result
45
84
  * @param includeDebugMessages include debug messages in result
46
85
  *
47
86
  */
48
- renderFromResult(result: OINOResult, includeErrorMessages?: boolean, includeWarningMessages?: boolean, includeInfoMessages?: boolean, includeDebugMessages?: boolean): OINOHttpResult;
87
+ renderFromResult(result: OINOResult, removeUnusedTags: boolean | undefined, messageSeparator: string, includeErrorMessages?: boolean, includeWarningMessages?: boolean, includeInfoMessages?: boolean, includeDebugMessages?: boolean): OINOHttpResult;
49
88
  }
@@ -11,7 +11,9 @@ export declare const OINO_WARNING_PREFIX = "OINO WARNING";
11
11
  export declare const OINO_INFO_PREFIX = "OINO INFO";
12
12
  /** OINO debug message prefix */
13
13
  export declare const OINO_DEBUG_PREFIX = "OINO DEBUG";
14
- /** Supported content format mime-types */
14
+ /**
15
+ * Supported content format mime-types
16
+ */
15
17
  export declare enum OINOContentType {
16
18
  /** JSON encoded data */
17
19
  json = "application/json",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@oino-ts/types",
3
- "version": "0.0.13",
3
+ "version": "0.0.15",
4
4
  "description": "OINO TS package for types.",
5
5
  "author": "Matias Kiviniemi (pragmatta)",
6
6
  "license": "MPL-2.0",
@@ -27,22 +27,24 @@ export class OINOBenchmark {
27
27
  /**
28
28
  * Set benchmark names that are enabled.
29
29
  *
30
- * @param names array of those benchmarks that are enabled
30
+ * @param module array of those benchmarks that are enabled
31
31
  */
32
- static setEnabled(names:string[]):void {
32
+ static setEnabled(module:string[]):void {
33
33
  this._benchmarkEnabled = {}
34
- names.forEach(name => {
35
- this._benchmarkEnabled[name] = true
34
+ module.forEach(module_name => {
35
+ this._benchmarkEnabled[module_name] = true
36
36
  });
37
37
  }
38
38
 
39
39
  /**
40
40
  * Start benchmark timing.
41
41
  *
42
- * @param name of the benchmark
42
+ * @param module of the benchmark
43
+ * @param method of the benchmark
43
44
  */
44
- static start(name:string):void {
45
- if (this._benchmarkEnabled[name]) {
45
+ static start(module:string, method:string):void {
46
+ const name:string = module + "." + method
47
+ if (this._benchmarkEnabled[module]) {
46
48
  if (this._benchmarkCount[name] == undefined) {
47
49
  this._benchmarkCount[name] = 0
48
50
  this._benchmarkData[name] = 0
@@ -54,12 +56,14 @@ export class OINOBenchmark {
54
56
  /**
55
57
  * Complete benchmark timing
56
58
  *
57
- * @param name of the benchmark
59
+ * @param module of the benchmark
60
+ * @param method of the benchmark
58
61
  * @param category optional subcategory of the benchmark
59
62
  */
60
- static end(name:string, category?:string):number {
63
+ static end(module:string, method:string, category?:string):number {
64
+ const name:string = module + "." + method
61
65
  let result:number = 0
62
- if (this._benchmarkEnabled[name]) {
66
+ if (this._benchmarkEnabled[module]) {
63
67
  const duration = performance.now() - this._benchmarkStart[name]
64
68
  this._benchmarkCount[name] += 1
65
69
  this._benchmarkData[name] += duration
@@ -80,11 +84,14 @@ export class OINOBenchmark {
80
84
  /**
81
85
  * Get given benchmark data.
82
86
  *
83
- * @param name of the benchmark
87
+ * @param module of the benchmark
88
+ * @param method of the benchmark
89
+ *
84
90
  */
85
- static get(name:string):number {
86
- if (this._benchmarkEnabled[name]) {
87
- return this._benchmarkData[name] / this._benchmarkCount[name]
91
+ static get(module:string, method:string):number {
92
+ const name:string = module + "." + method
93
+ if (this._benchmarkEnabled[module]) {
94
+ return this._benchmarkData[module] / this._benchmarkCount[module]
88
95
  }
89
96
  return -1
90
97
  }
@@ -1,9 +1,12 @@
1
- import { OINOStr, OINOContentType, OINOResult, OINOHttpResult, OINO_ERROR_PREFIX, OINO_WARNING_PREFIX, OINO_INFO_PREFIX, OINO_DEBUG_PREFIX } from "."
1
+ import { OINOStr, OINOContentType, OINOResult, OINOHttpResult, OINO_ERROR_PREFIX, OINO_WARNING_PREFIX, OINO_INFO_PREFIX, OINO_DEBUG_PREFIX, OINOBenchmark } from "."
2
2
 
3
3
  /**
4
4
  * Class for rendering HTML from data.
5
5
  */
6
6
  export class OINOHtmlTemplate {
7
+ private _tag:string
8
+ private _tagCleanRegex:RegExp
9
+ private _variables:Record<string, string> = {}
7
10
  /** HTML template string */
8
11
  template: string;
9
12
 
@@ -17,12 +20,15 @@ export class OINOHtmlTemplate {
17
20
  * Creates HTML Response from a key-value-pair.
18
21
  *
19
22
  * @param template template string
23
+ * @param tag tag to identify variables in template
20
24
  *
21
25
  */
22
- constructor (template:string) {
26
+ constructor (template:string, tag:string = "###") {
23
27
  this.template = template
24
28
  this.modified = 0
25
29
  this.expires = 0
30
+ this._tag = tag
31
+ this._tagCleanRegex = new RegExp(tag + ".*" + tag, "g")
26
32
  }
27
33
 
28
34
  /**
@@ -32,22 +38,94 @@ export class OINOHtmlTemplate {
32
38
  return this.template == ""
33
39
  }
34
40
 
41
+ protected _createHttpResult(html:string, removeUnusedTags:boolean):OINOHttpResult {
42
+ if (removeUnusedTags) {
43
+ html = html.replace(this._tagCleanRegex, "")
44
+ }
45
+ const result:OINOHttpResult = new OINOHttpResult(html)
46
+ if (this.expires >= 1) {
47
+ result.expires = Math.round(this.expires)
48
+ }
49
+ if (this.modified >= 1) {
50
+ result.lastModified = this.modified
51
+ }
52
+ return result
53
+ }
54
+
55
+ protected _renderHtml():string {
56
+ let html:string = this.template
57
+ for (let key in this._variables) {
58
+ const value = this._variables[key]
59
+ html = html.replaceAll(this._tag + key + this._tag, value)
60
+ }
61
+ return html
62
+ }
63
+
64
+ /**
65
+ * Clear template variables.
66
+ *
67
+ */
68
+ clearVariables() {
69
+ this._variables = {}
70
+ }
71
+
72
+ /**
73
+ * Sets template variable from a key-value-pair.
74
+ *
75
+ * @param variable key
76
+ * @param value value
77
+ * @param escapeValue whether to escape value
78
+ *
79
+ */
80
+ setVariableFromValue(variable:string, value:string, escapeValue:boolean = true) {
81
+ if (escapeValue) {
82
+ value = OINOStr.encode(value, OINOContentType.html)
83
+ }
84
+ this._variables[variable] = value
85
+ }
86
+
87
+ /**
88
+ * Sets template variables from object properties.
89
+ *
90
+ * @param object any object
91
+ * @param escapeValue whether to escape value
92
+ *
93
+ */
94
+ setVariableFromProperties(object:any, escapeValue:boolean = true) {
95
+ if (object) {
96
+ for (let key in object) {
97
+ if (escapeValue) {
98
+ this._variables[key] = OINOStr.encode(object[key], OINOContentType.html)
99
+ } else {
100
+ this._variables[key] = object[key]
101
+ }
102
+ }
103
+ }
104
+ }
105
+
106
+ /**
107
+ * Creates HTML Response from set variables.
108
+ *
109
+ * @param removeUnusedTags whether to remove unused tags
110
+ *
111
+ */
112
+ render(removeUnusedTags:boolean = true):OINOHttpResult {
113
+ return this._createHttpResult(this._renderHtml(), removeUnusedTags)
114
+ }
115
+
35
116
  /**
36
117
  * Creates HTML Response from a key-value-pair.
37
118
  *
38
119
  * @param key key
39
120
  * @param value value
121
+ * @param removeUnusedTags whether to remove unused tags
40
122
  *
41
123
  */
42
- renderFromKeyValue(key:string, value:string):OINOHttpResult {
43
- const html:string = this.template.replaceAll('###' + key + '###', OINOStr.encode(value, OINOContentType.html))
44
- const result:OINOHttpResult = new OINOHttpResult(html)
45
- if (this.expires >= 1) {
46
- result.expires = Math.round(this.expires)
47
- }
48
- if (this.modified >= 1) {
49
- result.lastModified = this.modified
50
- }
124
+ renderFromKeyValue(key:string, value:string, removeUnusedTags:boolean = true):OINOHttpResult {
125
+ OINOBenchmark.start("OINOHtmlTemplate", "renderFromKeyValue")
126
+ this.setVariableFromValue(key, value)
127
+ const result:OINOHttpResult = this.render(removeUnusedTags)
128
+ OINOBenchmark.end("OINOHtmlTemplate", "renderFromKeyValue")
51
129
  return result
52
130
  }
53
131
 
@@ -55,26 +133,14 @@ export class OINOHtmlTemplate {
55
133
  * Creates HTML Response from object properties.
56
134
  *
57
135
  * @param object object
136
+ * @param removeUnusedTags whether to remove unused tags
58
137
  *
59
138
  */
60
- renderFromObject(object:any):OINOHttpResult {
61
- let html:string = this.template
62
- if (object) {
63
- for (let key in object) {
64
- const value = object[key]
65
- if (value) {
66
- html = html.replaceAll('###' + key + '###', OINOStr.encode(value.toString(), OINOContentType.html))
67
- }
68
- }
69
- }
70
- html = html.replace(/###[^#]*###/g, "")
71
- const result:OINOHttpResult = new OINOHttpResult(html)
72
- if (this.expires >= 1) {
73
- result.expires = Math.round(this.expires)
74
- }
75
- if (this.modified >= 1) {
76
- result.lastModified = this.modified
77
- }
139
+ renderFromObject(object:any, removeUnusedTags:boolean = true):OINOHttpResult {
140
+ OINOBenchmark.start("OINOHtmlTemplate", "renderFromObject")
141
+ this.setVariableFromProperties(object)
142
+ const result:OINOHttpResult = this.render(removeUnusedTags)
143
+ OINOBenchmark.end("OINOHtmlTemplate", "renderFromObject")
78
144
  return result
79
145
  }
80
146
 
@@ -82,43 +148,39 @@ export class OINOHtmlTemplate {
82
148
  * Creates HTML Response from API result.
83
149
  *
84
150
  * @param result OINOResult-object
151
+ * @param removeUnusedTags whether to remove unused tags
152
+ * @param messageSeparator HTML separator for messages
85
153
  * @param includeErrorMessages include debug messages in result
86
154
  * @param includeWarningMessages include debug messages in result
87
155
  * @param includeInfoMessages include debug messages in result
88
156
  * @param includeDebugMessages include debug messages in result
89
157
  *
90
158
  */
91
- renderFromResult(result:OINOResult, includeErrorMessages:boolean=false, includeWarningMessages:boolean=false, includeInfoMessages:boolean=false, includeDebugMessages:boolean=false):OINOHttpResult {
92
- let html:string = this.template
93
- html = html.replaceAll('###statusCode###', OINOStr.encode(result.statusCode.toString(), OINOContentType.html))
94
- html = html.replaceAll('###statusMessage###', OINOStr.encode(result.statusMessage.toString(), OINOContentType.html))
95
- let messages = ""
159
+ renderFromResult(result:OINOResult, removeUnusedTags:boolean=true, messageSeparator:string, includeErrorMessages:boolean=false, includeWarningMessages:boolean=false, includeInfoMessages:boolean=false, includeDebugMessages:boolean=false):OINOHttpResult {
160
+ OINOBenchmark.start("OINOHtmlTemplate", "renderFromResult")
161
+ this.setVariableFromValue("statusCode", result.statusCode.toString())
162
+ this.setVariableFromValue("statusMessage", result.statusMessage.toString())
163
+ let messages:string[] = []
96
164
  for (let i:number = 0; i<result.messages.length; i++) {
97
165
  if (includeErrorMessages && result.messages[i].startsWith(OINO_ERROR_PREFIX)) {
98
- messages += "<li>" + OINOStr.encode(result.messages[i], OINOContentType.html) + "</li>"
166
+ messages.push(OINOStr.encode(result.messages[i], OINOContentType.html))
99
167
  }
100
168
  if (includeWarningMessages && result.messages[i].startsWith(OINO_WARNING_PREFIX)) {
101
- messages += "<li>" + OINOStr.encode(result.messages[i], OINOContentType.html) + "</li>"
169
+ messages.push(OINOStr.encode(result.messages[i], OINOContentType.html))
102
170
  }
103
171
  if (includeInfoMessages && result.messages[i].startsWith(OINO_INFO_PREFIX)) {
104
- messages += "<li>" + OINOStr.encode(result.messages[i], OINOContentType.html) + "</li>"
172
+ messages.push(OINOStr.encode(result.messages[i], OINOContentType.html))
105
173
  }
106
174
  if (includeDebugMessages && result.messages[i].startsWith(OINO_DEBUG_PREFIX)) {
107
- messages += "<li>" + OINOStr.encode(result.messages[i], OINOContentType.html) + "</li>"
175
+ messages.push(OINOStr.encode(result.messages[i], OINOContentType.html))
108
176
  }
109
177
 
110
178
  }
111
- if (messages) {
112
- html = html.replaceAll('###messages###', "<ul>" + messages + "</ul>")
113
- }
114
- html = html.replace(/###[^#]*###/g, "")
115
- const http_result:OINOHttpResult = new OINOHttpResult(html)
116
- if (this.expires >= 1) {
117
- http_result.expires = Math.round(this.expires)
118
- }
119
- if (this.modified >= 1) {
120
- http_result.lastModified = this.modified
121
- }
179
+ if (messages.length > 0) {
180
+ this.setVariableFromValue("messages", messages.join(messageSeparator), false) // messages have been escaped already
181
+ }
182
+ const http_result:OINOHttpResult = this.render(removeUnusedTags)
183
+ OINOBenchmark.end("OINOHtmlTemplate", "renderFromResult")
122
184
  return http_result
123
185
  }
124
186
  };
package/src/index.ts CHANGED
@@ -13,7 +13,9 @@ export const OINO_INFO_PREFIX = "OINO INFO"
13
13
  /** OINO debug message prefix */
14
14
  export const OINO_DEBUG_PREFIX = "OINO DEBUG"
15
15
 
16
- /** Supported content format mime-types */
16
+ /**
17
+ * Supported content format mime-types
18
+ */
17
19
  export enum OINOContentType {
18
20
  /** JSON encoded data */
19
21
  json='application/json',