@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.
- package/README.md +183 -0
- package/dist/cjs/OINOHtmlTemplate.js +3 -3
- package/dist/cjs/OINORequest.js +114 -0
- package/dist/cjs/OINOResult.js +33 -39
- package/dist/cjs/index.js +8 -1
- package/dist/esm/OINOHtmlTemplate.js +3 -3
- package/dist/esm/OINORequest.js +109 -0
- package/dist/esm/OINOResult.js +33 -39
- package/dist/esm/index.js +5 -0
- package/dist/types/OINORequest.d.ts +57 -0
- package/dist/types/OINOResult.d.ts +24 -15
- package/dist/types/index.d.ts +6 -1
- package/package.json +32 -32
- package/src/OINOBenchmark.ts +250 -250
- package/src/OINOFormatter.ts +165 -165
- package/src/OINOHtmlTemplate.test.ts +114 -114
- package/src/OINOHtmlTemplate.ts +222 -222
- package/src/OINOLog.ts +292 -292
- package/src/OINORequest.ts +129 -0
- package/src/OINOResult.ts +249 -244
- package/src/OINOStr.ts +254 -254
- package/src/index.ts +37 -32
package/src/OINOFormatter.ts
CHANGED
|
@@ -1,165 +1,165 @@
|
|
|
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
|
-
import { OINO_ERROR_PREFIX, OINOLog } from "./index.js"
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
/**
|
|
12
|
-
* Class for formatting strings and values.
|
|
13
|
-
*
|
|
14
|
-
*/
|
|
15
|
-
export class OINOFormatter {
|
|
16
|
-
static OINO_FORMATTER_REGEXP = /\s?(trim(\(\))?|trimLeft(\(\))?|trimRight(\(\))?|toUpper(\(\))?|toLower(\(\))?|cropLeft\((\d+)\)|cropRight\((\d+)\)|cropToDelimiter\(([^\(\),]+),(\d+)\)|cropFromDelimiter\(([^\(\),]+),(\d+)\)|substring\((\d+),(\d+)\)|replace\(([^\(\),]+),([^\(\),]+)\))\s?$/i
|
|
17
|
-
_types: string[]
|
|
18
|
-
_params: any[][]
|
|
19
|
-
|
|
20
|
-
/**
|
|
21
|
-
* Constructor of `OINOFormatter`
|
|
22
|
-
* @param types array of formatter types
|
|
23
|
-
* @param params array of formatter parameters according to type
|
|
24
|
-
*/
|
|
25
|
-
constructor(types: string[], params: any[][]) {
|
|
26
|
-
this._types = types
|
|
27
|
-
this._params = params
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
/**
|
|
31
|
-
* Constructor for `OINOFormatter` as parser of http parameter.
|
|
32
|
-
*
|
|
33
|
-
* @param formatters string or array of strings of serialized representation of formatters with following functions
|
|
34
|
-
* - trim()
|
|
35
|
-
* - trimLeft()
|
|
36
|
-
* - trimRight()
|
|
37
|
-
* - toUpper()
|
|
38
|
-
* - toLower()
|
|
39
|
-
* - cropLeft(charsToCrop)
|
|
40
|
-
* - cropRight(charsToCrop)
|
|
41
|
-
* - cropToDelimiter(delimiter,offsetChars)
|
|
42
|
-
* - cropFromDelimiter(delimiter,offsetChars)
|
|
43
|
-
* - substring(start,end)
|
|
44
|
-
* - replace(search,replace)
|
|
45
|
-
*/
|
|
46
|
-
static parse(formatters: string|string[]): OINOFormatter {
|
|
47
|
-
if (typeof formatters === "string") {
|
|
48
|
-
formatters = [formatters]
|
|
49
|
-
}
|
|
50
|
-
if (!formatters || formatters.length === 0) {
|
|
51
|
-
return OINO_EMPTY_FORMATTER
|
|
52
|
-
|
|
53
|
-
} else {
|
|
54
|
-
const types:string[] = []
|
|
55
|
-
const params:any[][] = []
|
|
56
|
-
for (let i=0; i<formatters.length; i++) {
|
|
57
|
-
let match = formatters[i]?.match(this.OINO_FORMATTER_REGEXP)
|
|
58
|
-
if (!match) {
|
|
59
|
-
OINOLog.error("@oino-ts/common", "OINOFormatter", "parse", "Invalid formatter string", {formatter:formatters[i]})
|
|
60
|
-
throw new Error(OINO_ERROR_PREFIX + "Invalid formatter: " + formatters[i])
|
|
61
|
-
} else {
|
|
62
|
-
const formatter_type = match[1].toLowerCase().substring(0, match[1].indexOf('('))
|
|
63
|
-
const formatter_params: any[] = []
|
|
64
|
-
if ((formatter_type === "trim") || (formatter_type === "trimleft") || (formatter_type === "trimright") || (formatter_type === "toupper") || (formatter_type === "tolower")) {
|
|
65
|
-
// no parameters
|
|
66
|
-
|
|
67
|
-
} else if (formatter_type=== "cropleft") {
|
|
68
|
-
formatter_params.push(parseInt(match[7]))
|
|
69
|
-
|
|
70
|
-
} else if (formatter_type === "cropright") {
|
|
71
|
-
formatter_params.push(parseInt(match[8]))
|
|
72
|
-
|
|
73
|
-
} else if (formatter_type === "croptodelimiter") {
|
|
74
|
-
formatter_params.push(decodeURIComponent(match[9]), parseInt(match[10]))
|
|
75
|
-
|
|
76
|
-
} else if (formatter_type === "cropfromdelimiter") {
|
|
77
|
-
formatter_params.push(decodeURIComponent(match[11]), parseInt(match[12]))
|
|
78
|
-
|
|
79
|
-
} else if (formatter_type === "substring") {
|
|
80
|
-
formatter_params.push(parseInt(match[13]), parseInt(match[14]))
|
|
81
|
-
|
|
82
|
-
} else if (formatter_type === "replace") {
|
|
83
|
-
formatter_params.push(decodeURIComponent(match[15]), decodeURIComponent(match[16]))
|
|
84
|
-
} else {
|
|
85
|
-
OINOLog.error("@oino-ts/common", "OINOFormatter", "parse", "Unknown formatter type", {formatter:formatters[i]})
|
|
86
|
-
throw new Error(OINO_ERROR_PREFIX + "Unsupported formatter: " + formatters[i])
|
|
87
|
-
}
|
|
88
|
-
types.push(formatter_type)
|
|
89
|
-
params.push(formatter_params)
|
|
90
|
-
}
|
|
91
|
-
}
|
|
92
|
-
return new OINOFormatter(types, params)
|
|
93
|
-
}
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
/**
|
|
97
|
-
* Does formatter include any operations.
|
|
98
|
-
* @return true if formatter is empty
|
|
99
|
-
*/
|
|
100
|
-
isEmpty():boolean {
|
|
101
|
-
return this._types.length === 0
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
/**
|
|
105
|
-
* Applies all formatters in order to given value.
|
|
106
|
-
*
|
|
107
|
-
* @param value string value to be formatted
|
|
108
|
-
* @returns formatted string value
|
|
109
|
-
*/
|
|
110
|
-
format(value: string): string {
|
|
111
|
-
let formatted = value
|
|
112
|
-
for (let i = 0; i < this._types.length; i++) {
|
|
113
|
-
const formatter_type = this._types[i]
|
|
114
|
-
const formatter_params = this._params[i]
|
|
115
|
-
if (formatter_type === "trim") {
|
|
116
|
-
formatted = formatted.trim()
|
|
117
|
-
|
|
118
|
-
} else if (formatter_type === "trimleft") {
|
|
119
|
-
formatted = formatted.trimStart()
|
|
120
|
-
|
|
121
|
-
} else if (formatter_type === "trimright") {
|
|
122
|
-
formatted = formatted.trimEnd()
|
|
123
|
-
|
|
124
|
-
} else if (formatter_type === "toupper") {
|
|
125
|
-
formatted = formatted.toUpperCase()
|
|
126
|
-
|
|
127
|
-
} else if (formatter_type === "tolower") {
|
|
128
|
-
formatted = formatted.toLowerCase()
|
|
129
|
-
|
|
130
|
-
} else if (formatter_type === "cropleft") {
|
|
131
|
-
formatted = formatted.slice(formatter_params[0])
|
|
132
|
-
|
|
133
|
-
} else if (formatter_type === "cropright") {
|
|
134
|
-
formatted = formatted.slice(0, formatted.length-formatter_params[0])
|
|
135
|
-
|
|
136
|
-
} else if (formatter_type === "croptodelimiter") {
|
|
137
|
-
const to_demilimiter_idx = formatted.indexOf(formatter_params[0])
|
|
138
|
-
if (to_demilimiter_idx >= 0) {
|
|
139
|
-
formatted = formatted.slice(Math.max(to_demilimiter_idx + formatter_params[0].length + formatter_params[1], 0))
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
} else if (formatter_type === "cropfromdelimiter") {
|
|
143
|
-
const from_demilimiter_idx = formatted.indexOf(formatter_params[0])
|
|
144
|
-
if (from_demilimiter_idx >= 0) {
|
|
145
|
-
formatted = formatted.slice(0, Math.max(from_demilimiter_idx + formatter_params[1], 0))
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
} else if (formatter_type === "substring") {
|
|
149
|
-
const start = formatter_params[0] ? parseInt(formatter_params[0]) : 0
|
|
150
|
-
const end = formatter_params[1] ? parseInt(formatter_params[1]) : formatted.length
|
|
151
|
-
formatted = formatted.substring(start, end)
|
|
152
|
-
|
|
153
|
-
} else if (formatter_type === "replace") {
|
|
154
|
-
const search = formatter_params[0]
|
|
155
|
-
const replacement = formatter_params[1]
|
|
156
|
-
formatted = formatted.replaceAll(search, replacement)
|
|
157
|
-
}
|
|
158
|
-
// console.log("formatter:", formatter_type, "params:", formatter_params, "formatted:", formatted)
|
|
159
|
-
}
|
|
160
|
-
return formatted
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
export const OINO_EMPTY_FORMATTER = new OINOFormatter([], [])
|
|
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
|
+
import { OINO_ERROR_PREFIX, OINOLog } from "./index.js"
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Class for formatting strings and values.
|
|
13
|
+
*
|
|
14
|
+
*/
|
|
15
|
+
export class OINOFormatter {
|
|
16
|
+
static OINO_FORMATTER_REGEXP = /\s?(trim(\(\))?|trimLeft(\(\))?|trimRight(\(\))?|toUpper(\(\))?|toLower(\(\))?|cropLeft\((\d+)\)|cropRight\((\d+)\)|cropToDelimiter\(([^\(\),]+),(\d+)\)|cropFromDelimiter\(([^\(\),]+),(\d+)\)|substring\((\d+),(\d+)\)|replace\(([^\(\),]+),([^\(\),]+)\))\s?$/i
|
|
17
|
+
_types: string[]
|
|
18
|
+
_params: any[][]
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Constructor of `OINOFormatter`
|
|
22
|
+
* @param types array of formatter types
|
|
23
|
+
* @param params array of formatter parameters according to type
|
|
24
|
+
*/
|
|
25
|
+
constructor(types: string[], params: any[][]) {
|
|
26
|
+
this._types = types
|
|
27
|
+
this._params = params
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Constructor for `OINOFormatter` as parser of http parameter.
|
|
32
|
+
*
|
|
33
|
+
* @param formatters string or array of strings of serialized representation of formatters with following functions
|
|
34
|
+
* - trim()
|
|
35
|
+
* - trimLeft()
|
|
36
|
+
* - trimRight()
|
|
37
|
+
* - toUpper()
|
|
38
|
+
* - toLower()
|
|
39
|
+
* - cropLeft(charsToCrop)
|
|
40
|
+
* - cropRight(charsToCrop)
|
|
41
|
+
* - cropToDelimiter(delimiter,offsetChars)
|
|
42
|
+
* - cropFromDelimiter(delimiter,offsetChars)
|
|
43
|
+
* - substring(start,end)
|
|
44
|
+
* - replace(search,replace)
|
|
45
|
+
*/
|
|
46
|
+
static parse(formatters: string|string[]): OINOFormatter {
|
|
47
|
+
if (typeof formatters === "string") {
|
|
48
|
+
formatters = [formatters]
|
|
49
|
+
}
|
|
50
|
+
if (!formatters || formatters.length === 0) {
|
|
51
|
+
return OINO_EMPTY_FORMATTER
|
|
52
|
+
|
|
53
|
+
} else {
|
|
54
|
+
const types:string[] = []
|
|
55
|
+
const params:any[][] = []
|
|
56
|
+
for (let i=0; i<formatters.length; i++) {
|
|
57
|
+
let match = formatters[i]?.match(this.OINO_FORMATTER_REGEXP)
|
|
58
|
+
if (!match) {
|
|
59
|
+
OINOLog.error("@oino-ts/common", "OINOFormatter", "parse", "Invalid formatter string", {formatter:formatters[i]})
|
|
60
|
+
throw new Error(OINO_ERROR_PREFIX + "Invalid formatter: " + formatters[i])
|
|
61
|
+
} else {
|
|
62
|
+
const formatter_type = match[1].toLowerCase().substring(0, match[1].indexOf('('))
|
|
63
|
+
const formatter_params: any[] = []
|
|
64
|
+
if ((formatter_type === "trim") || (formatter_type === "trimleft") || (formatter_type === "trimright") || (formatter_type === "toupper") || (formatter_type === "tolower")) {
|
|
65
|
+
// no parameters
|
|
66
|
+
|
|
67
|
+
} else if (formatter_type=== "cropleft") {
|
|
68
|
+
formatter_params.push(parseInt(match[7]))
|
|
69
|
+
|
|
70
|
+
} else if (formatter_type === "cropright") {
|
|
71
|
+
formatter_params.push(parseInt(match[8]))
|
|
72
|
+
|
|
73
|
+
} else if (formatter_type === "croptodelimiter") {
|
|
74
|
+
formatter_params.push(decodeURIComponent(match[9]), parseInt(match[10]))
|
|
75
|
+
|
|
76
|
+
} else if (formatter_type === "cropfromdelimiter") {
|
|
77
|
+
formatter_params.push(decodeURIComponent(match[11]), parseInt(match[12]))
|
|
78
|
+
|
|
79
|
+
} else if (formatter_type === "substring") {
|
|
80
|
+
formatter_params.push(parseInt(match[13]), parseInt(match[14]))
|
|
81
|
+
|
|
82
|
+
} else if (formatter_type === "replace") {
|
|
83
|
+
formatter_params.push(decodeURIComponent(match[15]), decodeURIComponent(match[16]))
|
|
84
|
+
} else {
|
|
85
|
+
OINOLog.error("@oino-ts/common", "OINOFormatter", "parse", "Unknown formatter type", {formatter:formatters[i]})
|
|
86
|
+
throw new Error(OINO_ERROR_PREFIX + "Unsupported formatter: " + formatters[i])
|
|
87
|
+
}
|
|
88
|
+
types.push(formatter_type)
|
|
89
|
+
params.push(formatter_params)
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
return new OINOFormatter(types, params)
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* Does formatter include any operations.
|
|
98
|
+
* @return true if formatter is empty
|
|
99
|
+
*/
|
|
100
|
+
isEmpty():boolean {
|
|
101
|
+
return this._types.length === 0
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* Applies all formatters in order to given value.
|
|
106
|
+
*
|
|
107
|
+
* @param value string value to be formatted
|
|
108
|
+
* @returns formatted string value
|
|
109
|
+
*/
|
|
110
|
+
format(value: string): string {
|
|
111
|
+
let formatted = value
|
|
112
|
+
for (let i = 0; i < this._types.length; i++) {
|
|
113
|
+
const formatter_type = this._types[i]
|
|
114
|
+
const formatter_params = this._params[i]
|
|
115
|
+
if (formatter_type === "trim") {
|
|
116
|
+
formatted = formatted.trim()
|
|
117
|
+
|
|
118
|
+
} else if (formatter_type === "trimleft") {
|
|
119
|
+
formatted = formatted.trimStart()
|
|
120
|
+
|
|
121
|
+
} else if (formatter_type === "trimright") {
|
|
122
|
+
formatted = formatted.trimEnd()
|
|
123
|
+
|
|
124
|
+
} else if (formatter_type === "toupper") {
|
|
125
|
+
formatted = formatted.toUpperCase()
|
|
126
|
+
|
|
127
|
+
} else if (formatter_type === "tolower") {
|
|
128
|
+
formatted = formatted.toLowerCase()
|
|
129
|
+
|
|
130
|
+
} else if (formatter_type === "cropleft") {
|
|
131
|
+
formatted = formatted.slice(formatter_params[0])
|
|
132
|
+
|
|
133
|
+
} else if (formatter_type === "cropright") {
|
|
134
|
+
formatted = formatted.slice(0, formatted.length-formatter_params[0])
|
|
135
|
+
|
|
136
|
+
} else if (formatter_type === "croptodelimiter") {
|
|
137
|
+
const to_demilimiter_idx = formatted.indexOf(formatter_params[0])
|
|
138
|
+
if (to_demilimiter_idx >= 0) {
|
|
139
|
+
formatted = formatted.slice(Math.max(to_demilimiter_idx + formatter_params[0].length + formatter_params[1], 0))
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
} else if (formatter_type === "cropfromdelimiter") {
|
|
143
|
+
const from_demilimiter_idx = formatted.indexOf(formatter_params[0])
|
|
144
|
+
if (from_demilimiter_idx >= 0) {
|
|
145
|
+
formatted = formatted.slice(0, Math.max(from_demilimiter_idx + formatter_params[1], 0))
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
} else if (formatter_type === "substring") {
|
|
149
|
+
const start = formatter_params[0] ? parseInt(formatter_params[0]) : 0
|
|
150
|
+
const end = formatter_params[1] ? parseInt(formatter_params[1]) : formatted.length
|
|
151
|
+
formatted = formatted.substring(start, end)
|
|
152
|
+
|
|
153
|
+
} else if (formatter_type === "replace") {
|
|
154
|
+
const search = formatter_params[0]
|
|
155
|
+
const replacement = formatter_params[1]
|
|
156
|
+
formatted = formatted.replaceAll(search, replacement)
|
|
157
|
+
}
|
|
158
|
+
// console.log("formatter:", formatter_type, "params:", formatter_params, "formatted:", formatted)
|
|
159
|
+
}
|
|
160
|
+
return formatted
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
export const OINO_EMPTY_FORMATTER = new OINOFormatter([], [])
|
|
@@ -1,114 +1,114 @@
|
|
|
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
|
-
import { expect, test } from "bun:test";
|
|
8
|
-
|
|
9
|
-
import { OINOHtmlTemplate } from "./OINOHtmlTemplate"
|
|
10
|
-
import { OINOFormatter } from "./OINOFormatter";
|
|
11
|
-
import { OINOLog, OINOConsoleLog, OINOLogLevel } from "./OINOLog"
|
|
12
|
-
|
|
13
|
-
Math.random()
|
|
14
|
-
|
|
15
|
-
OINOLog.setInstance(new OINOConsoleLog(OINOLogLevel.error))
|
|
16
|
-
|
|
17
|
-
const VARIABLE_OPTIONS = [
|
|
18
|
-
"{{{var{i}}}}", // 0 nothing
|
|
19
|
-
"{{{var{i}|trim()}}}", // 1 trim
|
|
20
|
-
"{{{var{i}|trimLeft()}}}", // 2 trimLeft
|
|
21
|
-
"{{{var{i}|trimRight()}}}", // 3 trimRight
|
|
22
|
-
"{{{var{i}|toLower()}}}", // 4 toLower
|
|
23
|
-
"{{{var{i}|cropLeft(1)}}}", // 5 cropLeft
|
|
24
|
-
"{{{var{i}|cropRight(1)}}}", // 6 cropRight
|
|
25
|
-
"{{{var{i}|cropToDelimiter(¤,1)}}}", // 7 cropToDelimiter
|
|
26
|
-
"{{{var{i}|cropFromDelimiter(¤,0)}}}", // 8 cropFromDelimiter
|
|
27
|
-
"{{{var{i}|substring(2,10)}}}", // 9 substring
|
|
28
|
-
"{{{var{i}|replace(¤,a)}}}" // 10 replace
|
|
29
|
-
]
|
|
30
|
-
const VARIABLE_VALUES = [
|
|
31
|
-
"value{i}", // 0 nothing
|
|
32
|
-
" value{i} ", // 1 trim
|
|
33
|
-
" value{i}", // 2 trimLeft
|
|
34
|
-
"value{i} ", // 3 trimRight
|
|
35
|
-
"VALUE{i}", // 4 toLower
|
|
36
|
-
"¤value{i}", // 5 cropLeft
|
|
37
|
-
"value{i}¤", // 6 cropRight
|
|
38
|
-
"¤¤value{i}", // 7 cropToDelimiter
|
|
39
|
-
"value{i}¤!", // 8 cropFromDelimiter
|
|
40
|
-
"!¤value{i}", // 9 substring
|
|
41
|
-
"v¤lue{i}" // 10 replace
|
|
42
|
-
]
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
function _generateTemplateVariable(i:number): string {
|
|
46
|
-
return VARIABLE_OPTIONS[i % VARIABLE_OPTIONS.length].replace(/\{i\}/g, i.toString())
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
function _generateTemplateHtml(variableCount: number): string {
|
|
50
|
-
let template = "{{{header}}}\n<div>\n" // edge case: tag at the start of the template
|
|
51
|
-
for (let i = 1; i <= variableCount; i++) {
|
|
52
|
-
template += `<p>${_generateTemplateVariable(i)}</p>\n`
|
|
53
|
-
}
|
|
54
|
-
template += "</div>\n{{{footer}}}" // edge case: tag at the end of the template
|
|
55
|
-
return template
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
function _generateResultHtml(variableCount: number): string {
|
|
59
|
-
let template = "header\n<div>\n"
|
|
60
|
-
for (let i = 1; i <= variableCount; i++) {
|
|
61
|
-
template += `<p>value${i}</p>\n`
|
|
62
|
-
}
|
|
63
|
-
template += "</div>\nfooter"
|
|
64
|
-
return template
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
function _generateValue(i: number): string {
|
|
69
|
-
return VARIABLE_VALUES[i % VARIABLE_VALUES.length].replace(/\{i\}/g, i.toString())
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
function _generateValues(variableCount: number): any {
|
|
73
|
-
const values: any = { header: "header", footer: "footer" }
|
|
74
|
-
for (let i = 1; i <= variableCount; i++) {
|
|
75
|
-
values[`var${i}`] = _generateValue(i)
|
|
76
|
-
}
|
|
77
|
-
return values
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
function benchmarkOINOHtmlTemplate(variables:number, replacements: number = 1000): number {
|
|
81
|
-
const template = new OINOHtmlTemplate(_generateTemplateHtml(variables))
|
|
82
|
-
const values = _generateValues(variables)
|
|
83
|
-
const result = _generateResultHtml(variables)
|
|
84
|
-
|
|
85
|
-
const start = performance.now();
|
|
86
|
-
|
|
87
|
-
for (let i = 0; i < replacements; i+=variables) {
|
|
88
|
-
template.setVariableFromProperties(values)
|
|
89
|
-
const res = template.render()
|
|
90
|
-
expect(res.body).toBe(result)
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
const end = performance.now()
|
|
94
|
-
const duration = end - start
|
|
95
|
-
return Math.round(replacements / duration)
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
await test("OINOHtmlTemplate render and performance", async () => {
|
|
99
|
-
|
|
100
|
-
let hps_min = Number.MAX_VALUE
|
|
101
|
-
let hps_max = 0
|
|
102
|
-
|
|
103
|
-
for (let j=10; j<=100; j+=10) {
|
|
104
|
-
const rps = benchmarkOINOHtmlTemplate(j, 100000)
|
|
105
|
-
// console.log(`Template variable renders per second with ${j} variables: ${rps}krps`)
|
|
106
|
-
hps_min = Math.min(rps, hps_min)
|
|
107
|
-
hps_max = Math.max(rps, hps_max)
|
|
108
|
-
expect(hps_min).toBeGreaterThanOrEqual(2500)
|
|
109
|
-
expect(hps_max).toBeLessThanOrEqual(5000)
|
|
110
|
-
}
|
|
111
|
-
console.log("OINOHtmlTemplate performance: " + hps_min + "k - " + hps_max + "k renders per second")
|
|
112
|
-
})
|
|
113
|
-
|
|
114
|
-
|
|
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
|
+
import { expect, test } from "bun:test";
|
|
8
|
+
|
|
9
|
+
import { OINOHtmlTemplate } from "./OINOHtmlTemplate"
|
|
10
|
+
import { OINOFormatter } from "./OINOFormatter";
|
|
11
|
+
import { OINOLog, OINOConsoleLog, OINOLogLevel } from "./OINOLog"
|
|
12
|
+
|
|
13
|
+
Math.random()
|
|
14
|
+
|
|
15
|
+
OINOLog.setInstance(new OINOConsoleLog(OINOLogLevel.error))
|
|
16
|
+
|
|
17
|
+
const VARIABLE_OPTIONS = [
|
|
18
|
+
"{{{var{i}}}}", // 0 nothing
|
|
19
|
+
"{{{var{i}|trim()}}}", // 1 trim
|
|
20
|
+
"{{{var{i}|trimLeft()}}}", // 2 trimLeft
|
|
21
|
+
"{{{var{i}|trimRight()}}}", // 3 trimRight
|
|
22
|
+
"{{{var{i}|toLower()}}}", // 4 toLower
|
|
23
|
+
"{{{var{i}|cropLeft(1)}}}", // 5 cropLeft
|
|
24
|
+
"{{{var{i}|cropRight(1)}}}", // 6 cropRight
|
|
25
|
+
"{{{var{i}|cropToDelimiter(¤,1)}}}", // 7 cropToDelimiter
|
|
26
|
+
"{{{var{i}|cropFromDelimiter(¤,0)}}}", // 8 cropFromDelimiter
|
|
27
|
+
"{{{var{i}|substring(2,10)}}}", // 9 substring
|
|
28
|
+
"{{{var{i}|replace(¤,a)}}}" // 10 replace
|
|
29
|
+
]
|
|
30
|
+
const VARIABLE_VALUES = [
|
|
31
|
+
"value{i}", // 0 nothing
|
|
32
|
+
" value{i} ", // 1 trim
|
|
33
|
+
" value{i}", // 2 trimLeft
|
|
34
|
+
"value{i} ", // 3 trimRight
|
|
35
|
+
"VALUE{i}", // 4 toLower
|
|
36
|
+
"¤value{i}", // 5 cropLeft
|
|
37
|
+
"value{i}¤", // 6 cropRight
|
|
38
|
+
"¤¤value{i}", // 7 cropToDelimiter
|
|
39
|
+
"value{i}¤!", // 8 cropFromDelimiter
|
|
40
|
+
"!¤value{i}", // 9 substring
|
|
41
|
+
"v¤lue{i}" // 10 replace
|
|
42
|
+
]
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
function _generateTemplateVariable(i:number): string {
|
|
46
|
+
return VARIABLE_OPTIONS[i % VARIABLE_OPTIONS.length].replace(/\{i\}/g, i.toString())
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
function _generateTemplateHtml(variableCount: number): string {
|
|
50
|
+
let template = "{{{header}}}\n<div>\n" // edge case: tag at the start of the template
|
|
51
|
+
for (let i = 1; i <= variableCount; i++) {
|
|
52
|
+
template += `<p>${_generateTemplateVariable(i)}</p>\n`
|
|
53
|
+
}
|
|
54
|
+
template += "</div>\n{{{footer}}}" // edge case: tag at the end of the template
|
|
55
|
+
return template
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
function _generateResultHtml(variableCount: number): string {
|
|
59
|
+
let template = "header\n<div>\n"
|
|
60
|
+
for (let i = 1; i <= variableCount; i++) {
|
|
61
|
+
template += `<p>value${i}</p>\n`
|
|
62
|
+
}
|
|
63
|
+
template += "</div>\nfooter"
|
|
64
|
+
return template
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
function _generateValue(i: number): string {
|
|
69
|
+
return VARIABLE_VALUES[i % VARIABLE_VALUES.length].replace(/\{i\}/g, i.toString())
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
function _generateValues(variableCount: number): any {
|
|
73
|
+
const values: any = { header: "header", footer: "footer" }
|
|
74
|
+
for (let i = 1; i <= variableCount; i++) {
|
|
75
|
+
values[`var${i}`] = _generateValue(i)
|
|
76
|
+
}
|
|
77
|
+
return values
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
function benchmarkOINOHtmlTemplate(variables:number, replacements: number = 1000): number {
|
|
81
|
+
const template = new OINOHtmlTemplate(_generateTemplateHtml(variables))
|
|
82
|
+
const values = _generateValues(variables)
|
|
83
|
+
const result = _generateResultHtml(variables)
|
|
84
|
+
|
|
85
|
+
const start = performance.now();
|
|
86
|
+
|
|
87
|
+
for (let i = 0; i < replacements; i+=variables) {
|
|
88
|
+
template.setVariableFromProperties(values)
|
|
89
|
+
const res = template.render()
|
|
90
|
+
expect(res.body).toBe(result)
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
const end = performance.now()
|
|
94
|
+
const duration = end - start
|
|
95
|
+
return Math.round(replacements / duration)
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
await test("OINOHtmlTemplate render and performance", async () => {
|
|
99
|
+
|
|
100
|
+
let hps_min = Number.MAX_VALUE
|
|
101
|
+
let hps_max = 0
|
|
102
|
+
|
|
103
|
+
for (let j=10; j<=100; j+=10) {
|
|
104
|
+
const rps = benchmarkOINOHtmlTemplate(j, 100000)
|
|
105
|
+
// console.log(`Template variable renders per second with ${j} variables: ${rps}krps`)
|
|
106
|
+
hps_min = Math.min(rps, hps_min)
|
|
107
|
+
hps_max = Math.max(rps, hps_max)
|
|
108
|
+
expect(hps_min).toBeGreaterThanOrEqual(2500)
|
|
109
|
+
expect(hps_max).toBeLessThanOrEqual(5000)
|
|
110
|
+
}
|
|
111
|
+
console.log("OINOHtmlTemplate performance: " + hps_min + "k - " + hps_max + "k renders per second")
|
|
112
|
+
})
|
|
113
|
+
|
|
114
|
+
|