@oino-ts/common 0.16.2 → 0.17.1

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.
@@ -1,222 +1,222 @@
1
- import { OINOStr, OINOContentType, OINOResult, OINOHttpResult, OINO_ERROR_PREFIX, OINO_WARNING_PREFIX, OINO_INFO_PREFIX, OINO_DEBUG_PREFIX, OINOBenchmark } from "."
2
- import { OINO_EMPTY_FORMATTER, OINOFormatter } from "./OINOFormatter"
3
-
4
- /**
5
- * Class for rendering HTML from data.
6
- */
7
- export class OINOHtmlTemplate {
8
- private _tagOpen:string
9
- private _tagClose:string
10
- private _variables:Record<string, string> = {}
11
- private _tagStart:number[] = []
12
- private _tagEnd:number[] = []
13
- private _tagVariable:string[] = []
14
- private _tagFormatters:OINOFormatter[] = []
15
- private _tagCount:number = 0
16
-
17
- /** HTML template string */
18
- template: string;
19
-
20
- /** Cache modified value for template */
21
- modified: number;
22
-
23
- /** Cache expiration value for template */
24
- expires: number;
25
-
26
- /**
27
- * Creates HTML Response from a key-value-pair.
28
- *
29
- * @param template template string
30
- * @param tagOpen tag to start variable in template
31
- * @param tagClose tag to end variables in template
32
- */
33
- constructor (template:string, tagOpen:string = "{{{", tagClose:string = "}}}") {
34
- this.template = template
35
- this.modified = 0
36
- this.expires = 0
37
- this._tagOpen = tagOpen
38
- this._tagClose = tagClose
39
- this._parseTemplate()
40
- }
41
-
42
- /**
43
- * @returns whether template is empty
44
- */
45
- isEmpty():boolean {
46
- return this.template == ""
47
- }
48
-
49
- protected _parseTemplate() {
50
- const tag_open_length = this._tagOpen.length
51
- const tag_close_length = this._tagClose.length
52
- let tag_start_pos = this.template.indexOf(this._tagOpen, 0)
53
- let tag_end_pos = this.template.indexOf(this._tagClose, tag_start_pos + tag_open_length) + tag_close_length
54
- while ((tag_start_pos >= 0) && (tag_end_pos > tag_start_pos)) {
55
- this._tagStart.push(tag_start_pos)
56
- this._tagEnd.push(tag_end_pos)
57
- let variable = this.template.slice(tag_start_pos+tag_open_length, tag_end_pos - tag_close_length)
58
- const variable_parts = variable.split("|")
59
- if (variable_parts.length > 1) {
60
- const formatter: OINOFormatter = OINOFormatter.parse(variable_parts.slice(1))
61
- this._tagFormatters.push(formatter)
62
- } else {
63
- this._tagFormatters.push(OINO_EMPTY_FORMATTER)
64
- }
65
- this._tagVariable.push(variable_parts[0])
66
- this._tagCount = this._tagCount + 1
67
- tag_start_pos = this.template.indexOf(this._tagOpen, tag_end_pos)
68
- tag_end_pos = this.template.indexOf(this._tagClose, tag_start_pos + tag_open_length) + tag_close_length
69
- }
70
- }
71
-
72
- protected _createHttpResult(html:string):OINOHttpResult {
73
- const result:OINOHttpResult = new OINOHttpResult(html)
74
- if (this.expires >= 1) {
75
- result.expires = Math.round(this.expires)
76
- }
77
- if (this.modified >= 1) {
78
- result.lastModified = this.modified
79
- }
80
- return result
81
- }
82
-
83
- protected _renderHtml():string {
84
- let html:string = ""
85
- let start_pos = 0
86
- let end_pos = 0
87
- for (let i=0; i<this._tagCount; i++) {
88
- end_pos = this._tagStart[i]
89
- const key = this._tagVariable[i]
90
- const value = this._tagFormatters[i].format(this._variables[key] || "")
91
- html += this.template.slice(start_pos, end_pos) + value
92
- start_pos = this._tagEnd[i]
93
- }
94
- html += this.template.slice(start_pos)
95
- // let html:string = this.template
96
- // for (let key in this._variables) {
97
- // const value = this._variables[key]
98
- // html = html.replaceAll(this._tag + key + this._tag, value)
99
- // }
100
- return html
101
- }
102
-
103
- /**
104
- * Clear template variables.
105
- *
106
- */
107
- clearVariables() {
108
- this._variables = {}
109
- }
110
-
111
- /**
112
- * Sets template variable from a key-value-pair.
113
- *
114
- * @param variable key
115
- * @param value value
116
- * @param escapeValue whether to escape value
117
- *
118
- */
119
- setVariableFromValue(variable:string, value:string, escapeValue:boolean = true) {
120
- if (escapeValue) {
121
- value = OINOStr.encode(value, OINOContentType.html)
122
- }
123
- this._variables[variable] = value
124
- }
125
-
126
- /**
127
- * Sets template variables from object properties.
128
- *
129
- * @param object any object
130
- * @param escapeValue whether to escape value
131
- *
132
- */
133
- setVariableFromProperties(object:any, escapeValue:boolean = true) {
134
- if (object) {
135
- for (let key in object) {
136
- if (escapeValue) {
137
- this._variables[key] = OINOStr.encode(object[key], OINOContentType.html)
138
- } else {
139
- this._variables[key] = object[key]
140
- }
141
- }
142
- }
143
- }
144
-
145
- /**
146
- * Creates HTML Response from set variables.
147
- *
148
- */
149
- render():OINOHttpResult {
150
- const html:string = this._renderHtml()
151
- this.clearVariables() // clear variables after rendering
152
- return this._createHttpResult(html)
153
- }
154
-
155
- /**
156
- * Creates HTML Response from a key-value-pair.
157
- *
158
- * @param key key
159
- * @param value value
160
- *
161
- */
162
- renderFromKeyValue(key:string, value:string):OINOHttpResult {
163
- OINOBenchmark.startMetric("OINOHtmlTemplate", "renderFromKeyValue")
164
- this.setVariableFromValue(key, value)
165
- const result:OINOHttpResult = this.render()
166
- OINOBenchmark.endMetric("OINOHtmlTemplate", "renderFromKeyValue")
167
- return result
168
- }
169
-
170
- /**
171
- * Creates HTML Response from object properties.
172
- *
173
- * @param object object
174
- *
175
- */
176
- renderFromObject(object:any = true):OINOHttpResult {
177
- OINOBenchmark.startMetric("OINOHtmlTemplate", "renderFromObject")
178
- this.setVariableFromProperties(object)
179
- const result:OINOHttpResult = this.render()
180
- OINOBenchmark.endMetric("OINOHtmlTemplate", "renderFromObject")
181
- return result
182
- }
183
-
184
- /**
185
- * Creates HTML Response from API result.
186
- *
187
- * @param result OINOResult-object
188
- * @param messageSeparator HTML separator for messages
189
- * @param includeErrorMessages include debug messages in result
190
- * @param includeWarningMessages include debug messages in result
191
- * @param includeInfoMessages include debug messages in result
192
- * @param includeDebugMessages include debug messages in result
193
- *
194
- */
195
- renderFromResult(result:OINOResult, messageSeparator:string = "", includeErrorMessages:boolean=false, includeWarningMessages:boolean=false, includeInfoMessages:boolean=false, includeDebugMessages:boolean=false):OINOHttpResult {
196
- OINOBenchmark.startMetric("OINOHtmlTemplate", "renderFromResult")
197
- this.setVariableFromValue("statusCode", result.statusCode.toString())
198
- this.setVariableFromValue("statusMessage", result.statusMessage.toString())
199
- let messages:string[] = []
200
- for (let i:number = 0; i<result.messages.length; i++) {
201
- if (includeErrorMessages && result.messages[i].startsWith(OINO_ERROR_PREFIX)) {
202
- messages.push(OINOStr.encode(result.messages[i], OINOContentType.html))
203
- }
204
- if (includeWarningMessages && result.messages[i].startsWith(OINO_WARNING_PREFIX)) {
205
- messages.push(OINOStr.encode(result.messages[i], OINOContentType.html))
206
- }
207
- if (includeInfoMessages && result.messages[i].startsWith(OINO_INFO_PREFIX)) {
208
- messages.push(OINOStr.encode(result.messages[i], OINOContentType.html))
209
- }
210
- if (includeDebugMessages && result.messages[i].startsWith(OINO_DEBUG_PREFIX)) {
211
- messages.push(OINOStr.encode(result.messages[i], OINOContentType.html))
212
- }
213
-
214
- }
215
- if (messageSeparator && (messages.length > 0)) {
216
- this.setVariableFromValue("messages", messages.join(messageSeparator), false) // messages have been escaped already
217
- }
218
- const http_result:OINOHttpResult = this.render()
219
- OINOBenchmark.endMetric("OINOHtmlTemplate", "renderFromResult")
220
- return http_result
221
- }
222
- };
1
+ import { OINOStr, OINOContentType, OINOResult, OINOHttpResult, OINO_ERROR_PREFIX, OINO_WARNING_PREFIX, OINO_INFO_PREFIX, OINO_DEBUG_PREFIX, OINOBenchmark } from "."
2
+ import { OINO_EMPTY_FORMATTER, OINOFormatter } from "./OINOFormatter"
3
+
4
+ /**
5
+ * Class for rendering HTML from data.
6
+ */
7
+ export class OINOHtmlTemplate {
8
+ private _tagOpen:string
9
+ private _tagClose:string
10
+ private _variables:Record<string, string> = {}
11
+ private _tagStart:number[] = []
12
+ private _tagEnd:number[] = []
13
+ private _tagVariable:string[] = []
14
+ private _tagFormatters:OINOFormatter[] = []
15
+ private _tagCount:number = 0
16
+
17
+ /** HTML template string */
18
+ template: string;
19
+
20
+ /** Cache modified value for template */
21
+ modified: number;
22
+
23
+ /** Cache expiration value for template */
24
+ expires: number;
25
+
26
+ /**
27
+ * Creates HTML Response from a key-value-pair.
28
+ *
29
+ * @param template template string
30
+ * @param tagOpen tag to start variable in template
31
+ * @param tagClose tag to end variables in template
32
+ */
33
+ constructor (template:string, tagOpen:string = "{{{", tagClose:string = "}}}") {
34
+ this.template = template
35
+ this.modified = 0
36
+ this.expires = 0
37
+ this._tagOpen = tagOpen
38
+ this._tagClose = tagClose
39
+ this._parseTemplate()
40
+ }
41
+
42
+ /**
43
+ * @returns whether template is empty
44
+ */
45
+ isEmpty():boolean {
46
+ return this.template == ""
47
+ }
48
+
49
+ protected _parseTemplate() {
50
+ const tag_open_length = this._tagOpen.length
51
+ const tag_close_length = this._tagClose.length
52
+ let tag_start_pos = this.template.indexOf(this._tagOpen, 0)
53
+ let tag_end_pos = this.template.indexOf(this._tagClose, tag_start_pos + tag_open_length) + tag_close_length
54
+ while ((tag_start_pos >= 0) && (tag_end_pos > tag_start_pos)) {
55
+ this._tagStart.push(tag_start_pos)
56
+ this._tagEnd.push(tag_end_pos)
57
+ let variable = this.template.slice(tag_start_pos+tag_open_length, tag_end_pos - tag_close_length)
58
+ const variable_parts = variable.split("|")
59
+ if (variable_parts.length > 1) {
60
+ const formatter: OINOFormatter = OINOFormatter.parse(variable_parts.slice(1))
61
+ this._tagFormatters.push(formatter)
62
+ } else {
63
+ this._tagFormatters.push(OINO_EMPTY_FORMATTER)
64
+ }
65
+ this._tagVariable.push(variable_parts[0])
66
+ this._tagCount = this._tagCount + 1
67
+ tag_start_pos = this.template.indexOf(this._tagOpen, tag_end_pos)
68
+ tag_end_pos = this.template.indexOf(this._tagClose, tag_start_pos + tag_open_length) + tag_close_length
69
+ }
70
+ }
71
+
72
+ protected _createHttpResult(html:string):OINOHttpResult {
73
+ const result:OINOHttpResult = new OINOHttpResult({body: html})
74
+ if (this.expires >= 1) {
75
+ result.expires = Math.round(this.expires)
76
+ }
77
+ if (this.modified >= 1) {
78
+ result.lastModified = this.modified
79
+ }
80
+ return result
81
+ }
82
+
83
+ protected _renderHtml():string {
84
+ let html:string = ""
85
+ let start_pos = 0
86
+ let end_pos = 0
87
+ for (let i=0; i<this._tagCount; i++) {
88
+ end_pos = this._tagStart[i]
89
+ const key = this._tagVariable[i]
90
+ const value = this._tagFormatters[i].format(this._variables[key] || "")
91
+ html += this.template.slice(start_pos, end_pos) + value
92
+ start_pos = this._tagEnd[i]
93
+ }
94
+ html += this.template.slice(start_pos)
95
+ // let html:string = this.template
96
+ // for (let key in this._variables) {
97
+ // const value = this._variables[key]
98
+ // html = html.replaceAll(this._tag + key + this._tag, value)
99
+ // }
100
+ return html
101
+ }
102
+
103
+ /**
104
+ * Clear template variables.
105
+ *
106
+ */
107
+ clearVariables() {
108
+ this._variables = {}
109
+ }
110
+
111
+ /**
112
+ * Sets template variable from a key-value-pair.
113
+ *
114
+ * @param variable key
115
+ * @param value value
116
+ * @param escapeValue whether to escape value
117
+ *
118
+ */
119
+ setVariableFromValue(variable:string, value:string, escapeValue:boolean = true) {
120
+ if (escapeValue) {
121
+ value = OINOStr.encode(value, OINOContentType.html)
122
+ }
123
+ this._variables[variable] = value
124
+ }
125
+
126
+ /**
127
+ * Sets template variables from object properties.
128
+ *
129
+ * @param object any object
130
+ * @param escapeValue whether to escape value
131
+ *
132
+ */
133
+ setVariableFromProperties(object:any, escapeValue:boolean = true) {
134
+ if (object) {
135
+ for (let key in object) {
136
+ if (escapeValue) {
137
+ this._variables[key] = OINOStr.encode(object[key], OINOContentType.html)
138
+ } else {
139
+ this._variables[key] = object[key]
140
+ }
141
+ }
142
+ }
143
+ }
144
+
145
+ /**
146
+ * Creates HTML Response from set variables.
147
+ *
148
+ */
149
+ render():OINOHttpResult {
150
+ const html:string = this._renderHtml()
151
+ this.clearVariables() // clear variables after rendering
152
+ return this._createHttpResult(html)
153
+ }
154
+
155
+ /**
156
+ * Creates HTML Response from a key-value-pair.
157
+ *
158
+ * @param key key
159
+ * @param value value
160
+ *
161
+ */
162
+ renderFromKeyValue(key:string, value:string):OINOHttpResult {
163
+ OINOBenchmark.startMetric("OINOHtmlTemplate", "renderFromKeyValue")
164
+ this.setVariableFromValue(key, value)
165
+ const result:OINOHttpResult = this.render()
166
+ OINOBenchmark.endMetric("OINOHtmlTemplate", "renderFromKeyValue")
167
+ return result
168
+ }
169
+
170
+ /**
171
+ * Creates HTML Response from object properties.
172
+ *
173
+ * @param object object
174
+ *
175
+ */
176
+ renderFromObject(object:any = true):OINOHttpResult {
177
+ OINOBenchmark.startMetric("OINOHtmlTemplate", "renderFromObject")
178
+ this.setVariableFromProperties(object)
179
+ const result:OINOHttpResult = this.render()
180
+ OINOBenchmark.endMetric("OINOHtmlTemplate", "renderFromObject")
181
+ return result
182
+ }
183
+
184
+ /**
185
+ * Creates HTML Response from API result.
186
+ *
187
+ * @param result OINOResult-object
188
+ * @param messageSeparator HTML separator for messages
189
+ * @param includeErrorMessages include debug messages in result
190
+ * @param includeWarningMessages include debug messages in result
191
+ * @param includeInfoMessages include debug messages in result
192
+ * @param includeDebugMessages include debug messages in result
193
+ *
194
+ */
195
+ renderFromResult(result:OINOResult, messageSeparator:string = "", includeErrorMessages:boolean=false, includeWarningMessages:boolean=false, includeInfoMessages:boolean=false, includeDebugMessages:boolean=false):OINOHttpResult {
196
+ OINOBenchmark.startMetric("OINOHtmlTemplate", "renderFromResult")
197
+ this.setVariableFromValue("status", result.status.toString())
198
+ this.setVariableFromValue("statusText", result.statusText.toString())
199
+ let messages:string[] = []
200
+ for (let i:number = 0; i<result.messages.length; i++) {
201
+ if (includeErrorMessages && result.messages[i].startsWith(OINO_ERROR_PREFIX)) {
202
+ messages.push(OINOStr.encode(result.messages[i], OINOContentType.html))
203
+ }
204
+ if (includeWarningMessages && result.messages[i].startsWith(OINO_WARNING_PREFIX)) {
205
+ messages.push(OINOStr.encode(result.messages[i], OINOContentType.html))
206
+ }
207
+ if (includeInfoMessages && result.messages[i].startsWith(OINO_INFO_PREFIX)) {
208
+ messages.push(OINOStr.encode(result.messages[i], OINOContentType.html))
209
+ }
210
+ if (includeDebugMessages && result.messages[i].startsWith(OINO_DEBUG_PREFIX)) {
211
+ messages.push(OINOStr.encode(result.messages[i], OINOContentType.html))
212
+ }
213
+
214
+ }
215
+ if (messageSeparator && (messages.length > 0)) {
216
+ this.setVariableFromValue("messages", messages.join(messageSeparator), false) // messages have been escaped already
217
+ }
218
+ const http_result:OINOHttpResult = this.render()
219
+ OINOBenchmark.endMetric("OINOHtmlTemplate", "renderFromResult")
220
+ return http_result
221
+ }
222
+ };