@oino-ts/common 0.3.3 → 0.3.4
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 +30 -30
- package/src/OINOBenchmark.ts +112 -112
- package/src/OINOHtmlTemplate.ts +186 -186
- package/src/OINOLog.ts +168 -168
- package/src/OINOResult.ts +234 -234
- package/src/OINOStr.ts +254 -254
- package/src/index.ts +31 -31
- package/README.md +0 -190
package/package.json
CHANGED
|
@@ -1,30 +1,30 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "@oino-ts/common",
|
|
3
|
-
"version": "0.3.
|
|
4
|
-
"description": "OINO TS package for common classes.",
|
|
5
|
-
"author": "Matias Kiviniemi (pragmatta)",
|
|
6
|
-
"license": "MPL-2.0",
|
|
7
|
-
"repository": {
|
|
8
|
-
"type": "git",
|
|
9
|
-
"url": "https://github.com/pragmatta/oino-ts.git"
|
|
10
|
-
},
|
|
11
|
-
"keywords": [
|
|
12
|
-
"types",
|
|
13
|
-
"typescript",
|
|
14
|
-
"library"
|
|
15
|
-
],
|
|
16
|
-
"main": "./dist/cjs/index.js",
|
|
17
|
-
"module": "./dist/esm/index.js",
|
|
18
|
-
"types": "./dist/types/index.d.ts",
|
|
19
|
-
"dependencies": {
|
|
20
|
-
},
|
|
21
|
-
"devDependencies": {
|
|
22
|
-
"@oino-ts/types": "0.3.
|
|
23
|
-
},
|
|
24
|
-
"files": [
|
|
25
|
-
"src/*.ts",
|
|
26
|
-
"dist/cjs/*.js",
|
|
27
|
-
"dist/esm/*.js",
|
|
28
|
-
"dist/types/*.d.ts"
|
|
29
|
-
]
|
|
30
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"name": "@oino-ts/common",
|
|
3
|
+
"version": "0.3.4",
|
|
4
|
+
"description": "OINO TS package for common classes.",
|
|
5
|
+
"author": "Matias Kiviniemi (pragmatta)",
|
|
6
|
+
"license": "MPL-2.0",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "https://github.com/pragmatta/oino-ts.git"
|
|
10
|
+
},
|
|
11
|
+
"keywords": [
|
|
12
|
+
"types",
|
|
13
|
+
"typescript",
|
|
14
|
+
"library"
|
|
15
|
+
],
|
|
16
|
+
"main": "./dist/cjs/index.js",
|
|
17
|
+
"module": "./dist/esm/index.js",
|
|
18
|
+
"types": "./dist/types/index.d.ts",
|
|
19
|
+
"dependencies": {
|
|
20
|
+
},
|
|
21
|
+
"devDependencies": {
|
|
22
|
+
"@oino-ts/types": "0.3.4"
|
|
23
|
+
},
|
|
24
|
+
"files": [
|
|
25
|
+
"src/*.ts",
|
|
26
|
+
"dist/cjs/*.js",
|
|
27
|
+
"dist/esm/*.js",
|
|
28
|
+
"dist/types/*.d.ts"
|
|
29
|
+
]
|
|
30
|
+
}
|
package/src/OINOBenchmark.ts
CHANGED
|
@@ -1,112 +1,112 @@
|
|
|
1
|
-
/*
|
|
2
|
-
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
3
|
-
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
4
|
-
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
|
5
|
-
*/
|
|
6
|
-
|
|
7
|
-
/**
|
|
8
|
-
* Static class for benchmarking functions.
|
|
9
|
-
*
|
|
10
|
-
*/
|
|
11
|
-
export class OINOBenchmark {
|
|
12
|
-
|
|
13
|
-
private static _benchmarkCount:Record<string, number> = {}
|
|
14
|
-
private static _benchmarkData:Record<string, number> = {}
|
|
15
|
-
private static _benchmarkEnabled:Record<string, boolean> = {}
|
|
16
|
-
private static _benchmarkStart:Record<string, number> = {}
|
|
17
|
-
|
|
18
|
-
/**
|
|
19
|
-
* Reset benchmark data (but not what is enabled).
|
|
20
|
-
*
|
|
21
|
-
*/
|
|
22
|
-
static reset() {
|
|
23
|
-
this._benchmarkData = {}
|
|
24
|
-
this._benchmarkCount = {}
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
/**
|
|
28
|
-
* Set benchmark names that are enabled.
|
|
29
|
-
*
|
|
30
|
-
* @param module array of those benchmarks that are enabled
|
|
31
|
-
*/
|
|
32
|
-
static setEnabled(module:string[]):void {
|
|
33
|
-
this._benchmarkEnabled = {}
|
|
34
|
-
module.forEach(module_name => {
|
|
35
|
-
this._benchmarkEnabled[module_name] = true
|
|
36
|
-
});
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
/**
|
|
40
|
-
* Start benchmark timing.
|
|
41
|
-
*
|
|
42
|
-
* @param module of the benchmark
|
|
43
|
-
* @param method of the benchmark
|
|
44
|
-
*/
|
|
45
|
-
static start(module:string, method:string):void {
|
|
46
|
-
const name:string = module + "." + method
|
|
47
|
-
if (this._benchmarkEnabled[module]) {
|
|
48
|
-
if (this._benchmarkCount[name] == undefined) {
|
|
49
|
-
this._benchmarkCount[name] = 0
|
|
50
|
-
this._benchmarkData[name] = 0
|
|
51
|
-
}
|
|
52
|
-
this._benchmarkStart[name] = performance.now()
|
|
53
|
-
}
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
/**
|
|
57
|
-
* Complete benchmark timing
|
|
58
|
-
*
|
|
59
|
-
* @param module of the benchmark
|
|
60
|
-
* @param method of the benchmark
|
|
61
|
-
* @param category optional subcategory of the benchmark
|
|
62
|
-
*/
|
|
63
|
-
static end(module:string, method:string, category?:string):number {
|
|
64
|
-
const name:string = module + "." + method
|
|
65
|
-
let result:number = 0
|
|
66
|
-
if (this._benchmarkEnabled[module]) {
|
|
67
|
-
const duration = performance.now() - this._benchmarkStart[name]
|
|
68
|
-
this._benchmarkCount[name] += 1
|
|
69
|
-
this._benchmarkData[name] += duration
|
|
70
|
-
if (category) {
|
|
71
|
-
const category_name = name + "." + category
|
|
72
|
-
if (this._benchmarkCount[category_name] == undefined) {
|
|
73
|
-
this._benchmarkCount[category_name] = 0
|
|
74
|
-
this._benchmarkData[category_name] = 0
|
|
75
|
-
}
|
|
76
|
-
this._benchmarkCount[category_name] += 1
|
|
77
|
-
this._benchmarkData[category_name] += duration
|
|
78
|
-
}
|
|
79
|
-
result = this._benchmarkData[name] / this._benchmarkCount[name]
|
|
80
|
-
}
|
|
81
|
-
return result
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
/**
|
|
85
|
-
* Get given benchmark data.
|
|
86
|
-
*
|
|
87
|
-
* @param module of the benchmark
|
|
88
|
-
* @param method of the benchmark
|
|
89
|
-
*
|
|
90
|
-
*/
|
|
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]
|
|
95
|
-
}
|
|
96
|
-
return -1
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
/**
|
|
100
|
-
* Get all benchmark data.
|
|
101
|
-
*
|
|
102
|
-
*/
|
|
103
|
-
static getAll():number {
|
|
104
|
-
let result:any = {}
|
|
105
|
-
for (const name in this._benchmarkData) {
|
|
106
|
-
if (this._benchmarkCount[name] > 0) {
|
|
107
|
-
result[name] = this._benchmarkData[name] / this._benchmarkCount[name]
|
|
108
|
-
}
|
|
109
|
-
}
|
|
110
|
-
return result
|
|
111
|
-
}
|
|
112
|
-
}
|
|
1
|
+
/*
|
|
2
|
+
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
3
|
+
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
4
|
+
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Static class for benchmarking functions.
|
|
9
|
+
*
|
|
10
|
+
*/
|
|
11
|
+
export class OINOBenchmark {
|
|
12
|
+
|
|
13
|
+
private static _benchmarkCount:Record<string, number> = {}
|
|
14
|
+
private static _benchmarkData:Record<string, number> = {}
|
|
15
|
+
private static _benchmarkEnabled:Record<string, boolean> = {}
|
|
16
|
+
private static _benchmarkStart:Record<string, number> = {}
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Reset benchmark data (but not what is enabled).
|
|
20
|
+
*
|
|
21
|
+
*/
|
|
22
|
+
static reset() {
|
|
23
|
+
this._benchmarkData = {}
|
|
24
|
+
this._benchmarkCount = {}
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Set benchmark names that are enabled.
|
|
29
|
+
*
|
|
30
|
+
* @param module array of those benchmarks that are enabled
|
|
31
|
+
*/
|
|
32
|
+
static setEnabled(module:string[]):void {
|
|
33
|
+
this._benchmarkEnabled = {}
|
|
34
|
+
module.forEach(module_name => {
|
|
35
|
+
this._benchmarkEnabled[module_name] = true
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Start benchmark timing.
|
|
41
|
+
*
|
|
42
|
+
* @param module of the benchmark
|
|
43
|
+
* @param method of the benchmark
|
|
44
|
+
*/
|
|
45
|
+
static start(module:string, method:string):void {
|
|
46
|
+
const name:string = module + "." + method
|
|
47
|
+
if (this._benchmarkEnabled[module]) {
|
|
48
|
+
if (this._benchmarkCount[name] == undefined) {
|
|
49
|
+
this._benchmarkCount[name] = 0
|
|
50
|
+
this._benchmarkData[name] = 0
|
|
51
|
+
}
|
|
52
|
+
this._benchmarkStart[name] = performance.now()
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Complete benchmark timing
|
|
58
|
+
*
|
|
59
|
+
* @param module of the benchmark
|
|
60
|
+
* @param method of the benchmark
|
|
61
|
+
* @param category optional subcategory of the benchmark
|
|
62
|
+
*/
|
|
63
|
+
static end(module:string, method:string, category?:string):number {
|
|
64
|
+
const name:string = module + "." + method
|
|
65
|
+
let result:number = 0
|
|
66
|
+
if (this._benchmarkEnabled[module]) {
|
|
67
|
+
const duration = performance.now() - this._benchmarkStart[name]
|
|
68
|
+
this._benchmarkCount[name] += 1
|
|
69
|
+
this._benchmarkData[name] += duration
|
|
70
|
+
if (category) {
|
|
71
|
+
const category_name = name + "." + category
|
|
72
|
+
if (this._benchmarkCount[category_name] == undefined) {
|
|
73
|
+
this._benchmarkCount[category_name] = 0
|
|
74
|
+
this._benchmarkData[category_name] = 0
|
|
75
|
+
}
|
|
76
|
+
this._benchmarkCount[category_name] += 1
|
|
77
|
+
this._benchmarkData[category_name] += duration
|
|
78
|
+
}
|
|
79
|
+
result = this._benchmarkData[name] / this._benchmarkCount[name]
|
|
80
|
+
}
|
|
81
|
+
return result
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* Get given benchmark data.
|
|
86
|
+
*
|
|
87
|
+
* @param module of the benchmark
|
|
88
|
+
* @param method of the benchmark
|
|
89
|
+
*
|
|
90
|
+
*/
|
|
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]
|
|
95
|
+
}
|
|
96
|
+
return -1
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Get all benchmark data.
|
|
101
|
+
*
|
|
102
|
+
*/
|
|
103
|
+
static getAll():number {
|
|
104
|
+
let result:any = {}
|
|
105
|
+
for (const name in this._benchmarkData) {
|
|
106
|
+
if (this._benchmarkCount[name] > 0) {
|
|
107
|
+
result[name] = this._benchmarkData[name] / this._benchmarkCount[name]
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
return result
|
|
111
|
+
}
|
|
112
|
+
}
|
package/src/OINOHtmlTemplate.ts
CHANGED
|
@@ -1,186 +1,186 @@
|
|
|
1
|
-
import { OINOStr, OINOContentType, OINOResult, OINOHttpResult, OINO_ERROR_PREFIX, OINO_WARNING_PREFIX, OINO_INFO_PREFIX, OINO_DEBUG_PREFIX, OINOBenchmark } from "."
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Class for rendering HTML from data.
|
|
5
|
-
*/
|
|
6
|
-
export class OINOHtmlTemplate {
|
|
7
|
-
private _tag:string
|
|
8
|
-
private _tagCleanRegex:RegExp
|
|
9
|
-
private _variables:Record<string, string> = {}
|
|
10
|
-
/** HTML template string */
|
|
11
|
-
template: string;
|
|
12
|
-
|
|
13
|
-
/** Cache modified value for template */
|
|
14
|
-
modified: number;
|
|
15
|
-
|
|
16
|
-
/** Cache expiration value for template */
|
|
17
|
-
expires: number;
|
|
18
|
-
|
|
19
|
-
/**
|
|
20
|
-
* Creates HTML Response from a key-value-pair.
|
|
21
|
-
*
|
|
22
|
-
* @param template template string
|
|
23
|
-
* @param tag tag to identify variables in template
|
|
24
|
-
*
|
|
25
|
-
*/
|
|
26
|
-
constructor (template:string, tag:string = "###") {
|
|
27
|
-
this.template = template
|
|
28
|
-
this.modified = 0
|
|
29
|
-
this.expires = 0
|
|
30
|
-
this._tag = tag
|
|
31
|
-
this._tagCleanRegex = new RegExp(tag + ".*" + tag, "g")
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
/**
|
|
35
|
-
* @returns whether template is empty
|
|
36
|
-
*/
|
|
37
|
-
isEmpty():boolean {
|
|
38
|
-
return this.template == ""
|
|
39
|
-
}
|
|
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
|
-
|
|
116
|
-
/**
|
|
117
|
-
* Creates HTML Response from a key-value-pair.
|
|
118
|
-
*
|
|
119
|
-
* @param key key
|
|
120
|
-
* @param value value
|
|
121
|
-
* @param removeUnusedTags whether to remove unused tags
|
|
122
|
-
*
|
|
123
|
-
*/
|
|
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")
|
|
129
|
-
return result
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
/**
|
|
133
|
-
* Creates HTML Response from object properties.
|
|
134
|
-
*
|
|
135
|
-
* @param object object
|
|
136
|
-
* @param removeUnusedTags whether to remove unused tags
|
|
137
|
-
*
|
|
138
|
-
*/
|
|
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")
|
|
144
|
-
return result
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
/**
|
|
148
|
-
* Creates HTML Response from API result.
|
|
149
|
-
*
|
|
150
|
-
* @param result OINOResult-object
|
|
151
|
-
* @param removeUnusedTags whether to remove unused tags
|
|
152
|
-
* @param messageSeparator HTML separator for messages
|
|
153
|
-
* @param includeErrorMessages include debug messages in result
|
|
154
|
-
* @param includeWarningMessages include debug messages in result
|
|
155
|
-
* @param includeInfoMessages include debug messages in result
|
|
156
|
-
* @param includeDebugMessages include debug messages in result
|
|
157
|
-
*
|
|
158
|
-
*/
|
|
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[] = []
|
|
164
|
-
for (let i:number = 0; i<result.messages.length; i++) {
|
|
165
|
-
if (includeErrorMessages && result.messages[i].startsWith(OINO_ERROR_PREFIX)) {
|
|
166
|
-
messages.push(OINOStr.encode(result.messages[i], OINOContentType.html))
|
|
167
|
-
}
|
|
168
|
-
if (includeWarningMessages && result.messages[i].startsWith(OINO_WARNING_PREFIX)) {
|
|
169
|
-
messages.push(OINOStr.encode(result.messages[i], OINOContentType.html))
|
|
170
|
-
}
|
|
171
|
-
if (includeInfoMessages && result.messages[i].startsWith(OINO_INFO_PREFIX)) {
|
|
172
|
-
messages.push(OINOStr.encode(result.messages[i], OINOContentType.html))
|
|
173
|
-
}
|
|
174
|
-
if (includeDebugMessages && result.messages[i].startsWith(OINO_DEBUG_PREFIX)) {
|
|
175
|
-
messages.push(OINOStr.encode(result.messages[i], OINOContentType.html))
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
}
|
|
179
|
-
if (messageSeparator && (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")
|
|
184
|
-
return http_result
|
|
185
|
-
}
|
|
186
|
-
};
|
|
1
|
+
import { OINOStr, OINOContentType, OINOResult, OINOHttpResult, OINO_ERROR_PREFIX, OINO_WARNING_PREFIX, OINO_INFO_PREFIX, OINO_DEBUG_PREFIX, OINOBenchmark } from "."
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Class for rendering HTML from data.
|
|
5
|
+
*/
|
|
6
|
+
export class OINOHtmlTemplate {
|
|
7
|
+
private _tag:string
|
|
8
|
+
private _tagCleanRegex:RegExp
|
|
9
|
+
private _variables:Record<string, string> = {}
|
|
10
|
+
/** HTML template string */
|
|
11
|
+
template: string;
|
|
12
|
+
|
|
13
|
+
/** Cache modified value for template */
|
|
14
|
+
modified: number;
|
|
15
|
+
|
|
16
|
+
/** Cache expiration value for template */
|
|
17
|
+
expires: number;
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Creates HTML Response from a key-value-pair.
|
|
21
|
+
*
|
|
22
|
+
* @param template template string
|
|
23
|
+
* @param tag tag to identify variables in template
|
|
24
|
+
*
|
|
25
|
+
*/
|
|
26
|
+
constructor (template:string, tag:string = "###") {
|
|
27
|
+
this.template = template
|
|
28
|
+
this.modified = 0
|
|
29
|
+
this.expires = 0
|
|
30
|
+
this._tag = tag
|
|
31
|
+
this._tagCleanRegex = new RegExp(tag + ".*" + tag, "g")
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* @returns whether template is empty
|
|
36
|
+
*/
|
|
37
|
+
isEmpty():boolean {
|
|
38
|
+
return this.template == ""
|
|
39
|
+
}
|
|
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
|
+
|
|
116
|
+
/**
|
|
117
|
+
* Creates HTML Response from a key-value-pair.
|
|
118
|
+
*
|
|
119
|
+
* @param key key
|
|
120
|
+
* @param value value
|
|
121
|
+
* @param removeUnusedTags whether to remove unused tags
|
|
122
|
+
*
|
|
123
|
+
*/
|
|
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")
|
|
129
|
+
return result
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* Creates HTML Response from object properties.
|
|
134
|
+
*
|
|
135
|
+
* @param object object
|
|
136
|
+
* @param removeUnusedTags whether to remove unused tags
|
|
137
|
+
*
|
|
138
|
+
*/
|
|
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")
|
|
144
|
+
return result
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
* Creates HTML Response from API result.
|
|
149
|
+
*
|
|
150
|
+
* @param result OINOResult-object
|
|
151
|
+
* @param removeUnusedTags whether to remove unused tags
|
|
152
|
+
* @param messageSeparator HTML separator for messages
|
|
153
|
+
* @param includeErrorMessages include debug messages in result
|
|
154
|
+
* @param includeWarningMessages include debug messages in result
|
|
155
|
+
* @param includeInfoMessages include debug messages in result
|
|
156
|
+
* @param includeDebugMessages include debug messages in result
|
|
157
|
+
*
|
|
158
|
+
*/
|
|
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[] = []
|
|
164
|
+
for (let i:number = 0; i<result.messages.length; i++) {
|
|
165
|
+
if (includeErrorMessages && result.messages[i].startsWith(OINO_ERROR_PREFIX)) {
|
|
166
|
+
messages.push(OINOStr.encode(result.messages[i], OINOContentType.html))
|
|
167
|
+
}
|
|
168
|
+
if (includeWarningMessages && result.messages[i].startsWith(OINO_WARNING_PREFIX)) {
|
|
169
|
+
messages.push(OINOStr.encode(result.messages[i], OINOContentType.html))
|
|
170
|
+
}
|
|
171
|
+
if (includeInfoMessages && result.messages[i].startsWith(OINO_INFO_PREFIX)) {
|
|
172
|
+
messages.push(OINOStr.encode(result.messages[i], OINOContentType.html))
|
|
173
|
+
}
|
|
174
|
+
if (includeDebugMessages && result.messages[i].startsWith(OINO_DEBUG_PREFIX)) {
|
|
175
|
+
messages.push(OINOStr.encode(result.messages[i], OINOContentType.html))
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
}
|
|
179
|
+
if (messageSeparator && (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")
|
|
184
|
+
return http_result
|
|
185
|
+
}
|
|
186
|
+
};
|