@oino-ts/common 0.10.4 → 0.12.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.
@@ -1,12 +1,19 @@
1
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"
2
3
 
3
4
  /**
4
5
  * Class for rendering HTML from data.
5
6
  */
6
7
  export class OINOHtmlTemplate {
7
- private _tag:string
8
- private _tagCleanRegex:RegExp
8
+ private _tagOpen:string
9
+ private _tagClose:string
9
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
+
10
17
  /** HTML template string */
11
18
  template: string;
12
19
 
@@ -23,12 +30,13 @@ export class OINOHtmlTemplate {
23
30
  * @param tag tag to identify variables in template
24
31
  *
25
32
  */
26
- constructor (template:string, tag:string = "###") {
33
+ constructor (template:string, tagOpen:string = "{{{", tagClose:string = "}}}") {
27
34
  this.template = template
28
35
  this.modified = 0
29
36
  this.expires = 0
30
- this._tag = tag
31
- this._tagCleanRegex = new RegExp(tag + ".*" + tag, "g")
37
+ this._tagOpen = tagOpen
38
+ this._tagClose = tagClose
39
+ this._parseTemplate()
32
40
  }
33
41
 
34
42
  /**
@@ -38,10 +46,30 @@ export class OINOHtmlTemplate {
38
46
  return this.template == ""
39
47
  }
40
48
 
41
- protected _createHttpResult(html:string, removeUnusedTags:boolean):OINOHttpResult {
42
- if (removeUnusedTags) {
43
- html = html.replace(this._tagCleanRegex, "")
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
44
69
  }
70
+ }
71
+
72
+ protected _createHttpResult(html:string):OINOHttpResult {
45
73
  const result:OINOHttpResult = new OINOHttpResult(html)
46
74
  if (this.expires >= 1) {
47
75
  result.expires = Math.round(this.expires)
@@ -53,11 +81,22 @@ export class OINOHtmlTemplate {
53
81
  }
54
82
 
55
83
  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)
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]
60
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
+ // }
61
100
  return html
62
101
  }
63
102
 
@@ -105,14 +144,12 @@ export class OINOHtmlTemplate {
105
144
 
106
145
  /**
107
146
  * Creates HTML Response from set variables.
108
- *
109
- * @param removeUnusedTags whether to remove unused tags
110
147
  *
111
148
  */
112
- render(removeUnusedTags:boolean = true):OINOHttpResult {
149
+ render():OINOHttpResult {
113
150
  const html:string = this._renderHtml()
114
151
  this.clearVariables() // clear variables after rendering
115
- return this._createHttpResult(html, removeUnusedTags)
152
+ return this._createHttpResult(html)
116
153
  }
117
154
 
118
155
  /**
@@ -120,13 +157,12 @@ export class OINOHtmlTemplate {
120
157
  *
121
158
  * @param key key
122
159
  * @param value value
123
- * @param removeUnusedTags whether to remove unused tags
124
160
  *
125
161
  */
126
- renderFromKeyValue(key:string, value:string, removeUnusedTags:boolean = true):OINOHttpResult {
162
+ renderFromKeyValue(key:string, value:string):OINOHttpResult {
127
163
  OINOBenchmark.startMetric("OINOHtmlTemplate", "renderFromKeyValue")
128
164
  this.setVariableFromValue(key, value)
129
- const result:OINOHttpResult = this.render(removeUnusedTags)
165
+ const result:OINOHttpResult = this.render()
130
166
  OINOBenchmark.endMetric("OINOHtmlTemplate", "renderFromKeyValue")
131
167
  return result
132
168
  }
@@ -135,13 +171,12 @@ export class OINOHtmlTemplate {
135
171
  * Creates HTML Response from object properties.
136
172
  *
137
173
  * @param object object
138
- * @param removeUnusedTags whether to remove unused tags
139
174
  *
140
175
  */
141
- renderFromObject(object:any, removeUnusedTags:boolean = true):OINOHttpResult {
176
+ renderFromObject(object:any = true):OINOHttpResult {
142
177
  OINOBenchmark.startMetric("OINOHtmlTemplate", "renderFromObject")
143
178
  this.setVariableFromProperties(object)
144
- const result:OINOHttpResult = this.render(removeUnusedTags)
179
+ const result:OINOHttpResult = this.render()
145
180
  OINOBenchmark.endMetric("OINOHtmlTemplate", "renderFromObject")
146
181
  return result
147
182
  }
@@ -150,7 +185,6 @@ export class OINOHtmlTemplate {
150
185
  * Creates HTML Response from API result.
151
186
  *
152
187
  * @param result OINOResult-object
153
- * @param removeUnusedTags whether to remove unused tags
154
188
  * @param messageSeparator HTML separator for messages
155
189
  * @param includeErrorMessages include debug messages in result
156
190
  * @param includeWarningMessages include debug messages in result
@@ -158,7 +192,7 @@ export class OINOHtmlTemplate {
158
192
  * @param includeDebugMessages include debug messages in result
159
193
  *
160
194
  */
161
- renderFromResult(result:OINOResult, removeUnusedTags:boolean=true, messageSeparator:string = "", includeErrorMessages:boolean=false, includeWarningMessages:boolean=false, includeInfoMessages:boolean=false, includeDebugMessages:boolean=false):OINOHttpResult {
195
+ renderFromResult(result:OINOResult, messageSeparator:string = "", includeErrorMessages:boolean=false, includeWarningMessages:boolean=false, includeInfoMessages:boolean=false, includeDebugMessages:boolean=false):OINOHttpResult {
162
196
  OINOBenchmark.startMetric("OINOHtmlTemplate", "renderFromResult")
163
197
  this.setVariableFromValue("statusCode", result.statusCode.toString())
164
198
  this.setVariableFromValue("statusMessage", result.statusMessage.toString())
@@ -181,7 +215,7 @@ export class OINOHtmlTemplate {
181
215
  if (messageSeparator && (messages.length > 0)) {
182
216
  this.setVariableFromValue("messages", messages.join(messageSeparator), false) // messages have been escaped already
183
217
  }
184
- const http_result:OINOHttpResult = this.render(removeUnusedTags)
218
+ const http_result:OINOHttpResult = this.render()
185
219
  OINOBenchmark.endMetric("OINOHtmlTemplate", "renderFromResult")
186
220
  return http_result
187
221
  }
package/src/OINOLog.ts CHANGED
@@ -213,14 +213,13 @@ export abstract class OINOLog {
213
213
 
214
214
 
215
215
  /**
216
- * Import log levels from an array of objects with domain, channel, method and level.
216
+ * Set log levels from an array of objects with domain, channel, method and level overwriting existing values (i.e. non-existing values are not affected).
217
217
  *
218
218
  * @param logLevels array of log level objects
219
219
  *
220
220
  */
221
- static importLogLevels(logLevels:any[]):void {
221
+ static setLogLevels(logLevels:any[]):void {
222
222
  if (OINOLog._instance) {
223
- OINOLog._instance._logLevels = {"||": OINOLog._instance._defaultLogLevel} // reset to default log level
224
223
  for (const logLevel of logLevels) {
225
224
  const domain = logLevel.domain || ""
226
225
  const channel = logLevel.channel || ""
@@ -232,6 +231,19 @@ export abstract class OINOLog {
232
231
  }
233
232
  }
234
233
  }
234
+
235
+ /**
236
+ * Import log levels from an array of objects with domain, channel, method and level resetting existing values (i.e. non-existing values get removed).
237
+ *
238
+ * @param logLevels array of log level objects
239
+ *
240
+ */
241
+ static importLogLevels(logLevels:any[]):void {
242
+ if (OINOLog._instance) {
243
+ OINOLog._instance._logLevels = {"||": OINOLog._instance._defaultLogLevel} // reset to default log level
244
+ this.setLogLevels(logLevels)
245
+ }
246
+ }
235
247
  }
236
248
 
237
249
  /**
package/src/index.ts CHANGED
@@ -3,6 +3,7 @@ export { OINOLog, OINOLogLevel, OINOConsoleLog } from "./OINOLog.js"
3
3
  export { OINOResult, OINOHttpResult } from "./OINOResult.js"
4
4
  export { OINOStr } from "./OINOStr.js"
5
5
  export { OINOHtmlTemplate } from "./OINOHtmlTemplate.js"
6
+ export { OINOFormatter, OINO_EMPTY_FORMATTER } from "./OINOFormatter.js"
6
7
 
7
8
  /** OINO error message prefix */
8
9
  export const OINO_ERROR_PREFIX = "OINO ERROR"