@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.
- package/dist/cjs/OINOFormatter.js +161 -0
- package/dist/cjs/OINOHtmlTemplate.js +58 -25
- package/dist/cjs/OINOLog.js +14 -3
- package/dist/cjs/index.js +4 -1
- package/dist/esm/OINOFormatter.js +157 -0
- package/dist/esm/OINOHtmlTemplate.js +58 -25
- package/dist/esm/OINOLog.js +14 -3
- package/dist/esm/index.js +1 -0
- package/dist/types/OINOFormatter.d.ts +45 -0
- package/dist/types/OINOHtmlTemplate.d.ts +14 -13
- package/dist/types/OINOLog.d.ts +8 -1
- package/dist/types/index.d.ts +1 -0
- package/package.json +4 -2
- package/src/OINOFormatter.ts +165 -0
- package/src/OINOHtmlTemplate.test.ts +114 -0
- package/src/OINOHtmlTemplate.ts +59 -25
- package/src/OINOLog.ts +15 -3
- package/src/index.ts +1 -0
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/*
|
|
3
|
+
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
4
|
+
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
5
|
+
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
|
6
|
+
*/
|
|
7
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
8
|
+
exports.OINO_EMPTY_FORMATTER = exports.OINOFormatter = void 0;
|
|
9
|
+
const index_js_1 = require("./index.js");
|
|
10
|
+
/**
|
|
11
|
+
* Class for formatting strings and values.
|
|
12
|
+
*
|
|
13
|
+
*/
|
|
14
|
+
class OINOFormatter {
|
|
15
|
+
static OINO_FORMATTER_REGEXP = /\s?(trim(\(\))?|trimLeft(\(\))?|trimRight(\(\))?|toUpper(\(\))?|toLower(\(\))?|cropLeft\((\d+)\)|cropRight\((\d+)\)|cropToDelimiter\(([^\(\),]+),(\d+)\)|cropFromDelimiter\(([^\(\),]+),(\d+)\)|substring\((\d+),(\d+)\)|replace\(([^\(\),]+),([^\(\),]+)\))\s?$/i;
|
|
16
|
+
_types;
|
|
17
|
+
_params;
|
|
18
|
+
/**
|
|
19
|
+
* Constructor of `OINOFormatter`
|
|
20
|
+
* @param types array of formatter types
|
|
21
|
+
* @param params array of formatter parameters according to type
|
|
22
|
+
*/
|
|
23
|
+
constructor(types, params) {
|
|
24
|
+
this._types = types;
|
|
25
|
+
this._params = params;
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Constructor for `OINOFormatter` as parser of http parameter.
|
|
29
|
+
*
|
|
30
|
+
* @param formatters string or array of strings of serialized representation of formatters with following functions
|
|
31
|
+
* - trim()
|
|
32
|
+
* - trimLeft()
|
|
33
|
+
* - trimRight()
|
|
34
|
+
* - toUpper()
|
|
35
|
+
* - toLower()
|
|
36
|
+
* - cropLeft(charsToCrop)
|
|
37
|
+
* - cropRight(charsToCrop)
|
|
38
|
+
* - cropToDelimiter(delimiter,offsetChars)
|
|
39
|
+
* - cropFromDelimiter(delimiter,offsetChars)
|
|
40
|
+
* - substring(start,end)
|
|
41
|
+
* - replace(search,replace)
|
|
42
|
+
*/
|
|
43
|
+
static parse(formatters) {
|
|
44
|
+
if (typeof formatters === "string") {
|
|
45
|
+
formatters = [formatters];
|
|
46
|
+
}
|
|
47
|
+
if (!formatters || formatters.length === 0) {
|
|
48
|
+
return exports.OINO_EMPTY_FORMATTER;
|
|
49
|
+
}
|
|
50
|
+
else {
|
|
51
|
+
const types = [];
|
|
52
|
+
const params = [];
|
|
53
|
+
for (let i = 0; i < formatters.length; i++) {
|
|
54
|
+
let match = formatters[i]?.match(this.OINO_FORMATTER_REGEXP);
|
|
55
|
+
if (!match) {
|
|
56
|
+
index_js_1.OINOLog.error("@oino-ts/common", "OINOFormatter", "parse", "Invalid formatter string", { formatter: formatters[i] });
|
|
57
|
+
throw new Error(index_js_1.OINO_ERROR_PREFIX + "Invalid formatter: " + formatters[i]);
|
|
58
|
+
}
|
|
59
|
+
else {
|
|
60
|
+
const formatter_type = match[1].toLowerCase().substring(0, match[1].indexOf('('));
|
|
61
|
+
const formatter_params = [];
|
|
62
|
+
if ((formatter_type === "trim") || (formatter_type === "trimleft") || (formatter_type === "trimright") || (formatter_type === "toupper") || (formatter_type === "tolower")) {
|
|
63
|
+
// no parameters
|
|
64
|
+
}
|
|
65
|
+
else if (formatter_type === "cropleft") {
|
|
66
|
+
formatter_params.push(parseInt(match[7]));
|
|
67
|
+
}
|
|
68
|
+
else if (formatter_type === "cropright") {
|
|
69
|
+
formatter_params.push(parseInt(match[8]));
|
|
70
|
+
}
|
|
71
|
+
else if (formatter_type === "croptodelimiter") {
|
|
72
|
+
formatter_params.push(decodeURIComponent(match[9]), parseInt(match[10]));
|
|
73
|
+
}
|
|
74
|
+
else if (formatter_type === "cropfromdelimiter") {
|
|
75
|
+
formatter_params.push(decodeURIComponent(match[11]), parseInt(match[12]));
|
|
76
|
+
}
|
|
77
|
+
else if (formatter_type === "substring") {
|
|
78
|
+
formatter_params.push(parseInt(match[13]), parseInt(match[14]));
|
|
79
|
+
}
|
|
80
|
+
else if (formatter_type === "replace") {
|
|
81
|
+
formatter_params.push(decodeURIComponent(match[15]), decodeURIComponent(match[16]));
|
|
82
|
+
}
|
|
83
|
+
else {
|
|
84
|
+
index_js_1.OINOLog.error("@oino-ts/common", "OINOFormatter", "parse", "Unknown formatter type", { formatter: formatters[i] });
|
|
85
|
+
throw new Error(index_js_1.OINO_ERROR_PREFIX + "Unsupported formatter: " + formatters[i]);
|
|
86
|
+
}
|
|
87
|
+
types.push(formatter_type);
|
|
88
|
+
params.push(formatter_params);
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
return new OINOFormatter(types, params);
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* Does formatter include any operations.
|
|
96
|
+
* @return true if formatter is empty
|
|
97
|
+
*/
|
|
98
|
+
isEmpty() {
|
|
99
|
+
return this._types.length === 0;
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* Applies all formatters in order to given value.
|
|
103
|
+
*
|
|
104
|
+
* @param value string value to be formatted
|
|
105
|
+
* @returns formatted string value
|
|
106
|
+
*/
|
|
107
|
+
format(value) {
|
|
108
|
+
let formatted = value;
|
|
109
|
+
for (let i = 0; i < this._types.length; i++) {
|
|
110
|
+
const formatter_type = this._types[i];
|
|
111
|
+
const formatter_params = this._params[i];
|
|
112
|
+
if (formatter_type === "trim") {
|
|
113
|
+
formatted = formatted.trim();
|
|
114
|
+
}
|
|
115
|
+
else if (formatter_type === "trimleft") {
|
|
116
|
+
formatted = formatted.trimStart();
|
|
117
|
+
}
|
|
118
|
+
else if (formatter_type === "trimright") {
|
|
119
|
+
formatted = formatted.trimEnd();
|
|
120
|
+
}
|
|
121
|
+
else if (formatter_type === "toupper") {
|
|
122
|
+
formatted = formatted.toUpperCase();
|
|
123
|
+
}
|
|
124
|
+
else if (formatter_type === "tolower") {
|
|
125
|
+
formatted = formatted.toLowerCase();
|
|
126
|
+
}
|
|
127
|
+
else if (formatter_type === "cropleft") {
|
|
128
|
+
formatted = formatted.slice(formatter_params[0]);
|
|
129
|
+
}
|
|
130
|
+
else if (formatter_type === "cropright") {
|
|
131
|
+
formatted = formatted.slice(0, formatted.length - formatter_params[0]);
|
|
132
|
+
}
|
|
133
|
+
else if (formatter_type === "croptodelimiter") {
|
|
134
|
+
const to_demilimiter_idx = formatted.indexOf(formatter_params[0]);
|
|
135
|
+
if (to_demilimiter_idx >= 0) {
|
|
136
|
+
formatted = formatted.slice(Math.max(to_demilimiter_idx + formatter_params[0].length + formatter_params[1], 0));
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
else if (formatter_type === "cropfromdelimiter") {
|
|
140
|
+
const from_demilimiter_idx = formatted.indexOf(formatter_params[0]);
|
|
141
|
+
if (from_demilimiter_idx >= 0) {
|
|
142
|
+
formatted = formatted.slice(0, Math.max(from_demilimiter_idx + formatter_params[1], 0));
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
else if (formatter_type === "substring") {
|
|
146
|
+
const start = formatter_params[0] ? parseInt(formatter_params[0]) : 0;
|
|
147
|
+
const end = formatter_params[1] ? parseInt(formatter_params[1]) : formatted.length;
|
|
148
|
+
formatted = formatted.substring(start, end);
|
|
149
|
+
}
|
|
150
|
+
else if (formatter_type === "replace") {
|
|
151
|
+
const search = formatter_params[0];
|
|
152
|
+
const replacement = formatter_params[1];
|
|
153
|
+
formatted = formatted.replaceAll(search, replacement);
|
|
154
|
+
}
|
|
155
|
+
// console.log("formatter:", formatter_type, "params:", formatter_params, "formatted:", formatted)
|
|
156
|
+
}
|
|
157
|
+
return formatted;
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
exports.OINOFormatter = OINOFormatter;
|
|
161
|
+
exports.OINO_EMPTY_FORMATTER = new OINOFormatter([], []);
|
|
@@ -2,13 +2,19 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.OINOHtmlTemplate = void 0;
|
|
4
4
|
const _1 = require(".");
|
|
5
|
+
const OINOFormatter_1 = require("./OINOFormatter");
|
|
5
6
|
/**
|
|
6
7
|
* Class for rendering HTML from data.
|
|
7
8
|
*/
|
|
8
9
|
class OINOHtmlTemplate {
|
|
9
|
-
|
|
10
|
-
|
|
10
|
+
_tagOpen;
|
|
11
|
+
_tagClose;
|
|
11
12
|
_variables = {};
|
|
13
|
+
_tagStart = [];
|
|
14
|
+
_tagEnd = [];
|
|
15
|
+
_tagVariable = [];
|
|
16
|
+
_tagFormatters = [];
|
|
17
|
+
_tagCount = 0;
|
|
12
18
|
/** HTML template string */
|
|
13
19
|
template;
|
|
14
20
|
/** Cache modified value for template */
|
|
@@ -22,12 +28,13 @@ class OINOHtmlTemplate {
|
|
|
22
28
|
* @param tag tag to identify variables in template
|
|
23
29
|
*
|
|
24
30
|
*/
|
|
25
|
-
constructor(template,
|
|
31
|
+
constructor(template, tagOpen = "{{{", tagClose = "}}}") {
|
|
26
32
|
this.template = template;
|
|
27
33
|
this.modified = 0;
|
|
28
34
|
this.expires = 0;
|
|
29
|
-
this.
|
|
30
|
-
this.
|
|
35
|
+
this._tagOpen = tagOpen;
|
|
36
|
+
this._tagClose = tagClose;
|
|
37
|
+
this._parseTemplate();
|
|
31
38
|
}
|
|
32
39
|
/**
|
|
33
40
|
* @returns whether template is empty
|
|
@@ -35,10 +42,30 @@ class OINOHtmlTemplate {
|
|
|
35
42
|
isEmpty() {
|
|
36
43
|
return this.template == "";
|
|
37
44
|
}
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
45
|
+
_parseTemplate() {
|
|
46
|
+
const tag_open_length = this._tagOpen.length;
|
|
47
|
+
const tag_close_length = this._tagClose.length;
|
|
48
|
+
let tag_start_pos = this.template.indexOf(this._tagOpen, 0);
|
|
49
|
+
let tag_end_pos = this.template.indexOf(this._tagClose, tag_start_pos + tag_open_length) + tag_close_length;
|
|
50
|
+
while ((tag_start_pos >= 0) && (tag_end_pos > tag_start_pos)) {
|
|
51
|
+
this._tagStart.push(tag_start_pos);
|
|
52
|
+
this._tagEnd.push(tag_end_pos);
|
|
53
|
+
let variable = this.template.slice(tag_start_pos + tag_open_length, tag_end_pos - tag_close_length);
|
|
54
|
+
const variable_parts = variable.split("|");
|
|
55
|
+
if (variable_parts.length > 1) {
|
|
56
|
+
const formatter = OINOFormatter_1.OINOFormatter.parse(variable_parts.slice(1));
|
|
57
|
+
this._tagFormatters.push(formatter);
|
|
58
|
+
}
|
|
59
|
+
else {
|
|
60
|
+
this._tagFormatters.push(OINOFormatter_1.OINO_EMPTY_FORMATTER);
|
|
61
|
+
}
|
|
62
|
+
this._tagVariable.push(variable_parts[0]);
|
|
63
|
+
this._tagCount = this._tagCount + 1;
|
|
64
|
+
tag_start_pos = this.template.indexOf(this._tagOpen, tag_end_pos);
|
|
65
|
+
tag_end_pos = this.template.indexOf(this._tagClose, tag_start_pos + tag_open_length) + tag_close_length;
|
|
41
66
|
}
|
|
67
|
+
}
|
|
68
|
+
_createHttpResult(html) {
|
|
42
69
|
const result = new _1.OINOHttpResult(html);
|
|
43
70
|
if (this.expires >= 1) {
|
|
44
71
|
result.expires = Math.round(this.expires);
|
|
@@ -49,11 +76,22 @@ class OINOHtmlTemplate {
|
|
|
49
76
|
return result;
|
|
50
77
|
}
|
|
51
78
|
_renderHtml() {
|
|
52
|
-
let html =
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
79
|
+
let html = "";
|
|
80
|
+
let start_pos = 0;
|
|
81
|
+
let end_pos = 0;
|
|
82
|
+
for (let i = 0; i < this._tagCount; i++) {
|
|
83
|
+
end_pos = this._tagStart[i];
|
|
84
|
+
const key = this._tagVariable[i];
|
|
85
|
+
const value = this._tagFormatters[i].format(this._variables[key] || "");
|
|
86
|
+
html += this.template.slice(start_pos, end_pos) + value;
|
|
87
|
+
start_pos = this._tagEnd[i];
|
|
56
88
|
}
|
|
89
|
+
html += this.template.slice(start_pos);
|
|
90
|
+
// let html:string = this.template
|
|
91
|
+
// for (let key in this._variables) {
|
|
92
|
+
// const value = this._variables[key]
|
|
93
|
+
// html = html.replaceAll(this._tag + key + this._tag, value)
|
|
94
|
+
// }
|
|
57
95
|
return html;
|
|
58
96
|
}
|
|
59
97
|
/**
|
|
@@ -99,26 +137,23 @@ class OINOHtmlTemplate {
|
|
|
99
137
|
/**
|
|
100
138
|
* Creates HTML Response from set variables.
|
|
101
139
|
*
|
|
102
|
-
* @param removeUnusedTags whether to remove unused tags
|
|
103
|
-
*
|
|
104
140
|
*/
|
|
105
|
-
render(
|
|
141
|
+
render() {
|
|
106
142
|
const html = this._renderHtml();
|
|
107
143
|
this.clearVariables(); // clear variables after rendering
|
|
108
|
-
return this._createHttpResult(html
|
|
144
|
+
return this._createHttpResult(html);
|
|
109
145
|
}
|
|
110
146
|
/**
|
|
111
147
|
* Creates HTML Response from a key-value-pair.
|
|
112
148
|
*
|
|
113
149
|
* @param key key
|
|
114
150
|
* @param value value
|
|
115
|
-
* @param removeUnusedTags whether to remove unused tags
|
|
116
151
|
*
|
|
117
152
|
*/
|
|
118
|
-
renderFromKeyValue(key, value
|
|
153
|
+
renderFromKeyValue(key, value) {
|
|
119
154
|
_1.OINOBenchmark.startMetric("OINOHtmlTemplate", "renderFromKeyValue");
|
|
120
155
|
this.setVariableFromValue(key, value);
|
|
121
|
-
const result = this.render(
|
|
156
|
+
const result = this.render();
|
|
122
157
|
_1.OINOBenchmark.endMetric("OINOHtmlTemplate", "renderFromKeyValue");
|
|
123
158
|
return result;
|
|
124
159
|
}
|
|
@@ -126,13 +161,12 @@ class OINOHtmlTemplate {
|
|
|
126
161
|
* Creates HTML Response from object properties.
|
|
127
162
|
*
|
|
128
163
|
* @param object object
|
|
129
|
-
* @param removeUnusedTags whether to remove unused tags
|
|
130
164
|
*
|
|
131
165
|
*/
|
|
132
|
-
renderFromObject(object
|
|
166
|
+
renderFromObject(object = true) {
|
|
133
167
|
_1.OINOBenchmark.startMetric("OINOHtmlTemplate", "renderFromObject");
|
|
134
168
|
this.setVariableFromProperties(object);
|
|
135
|
-
const result = this.render(
|
|
169
|
+
const result = this.render();
|
|
136
170
|
_1.OINOBenchmark.endMetric("OINOHtmlTemplate", "renderFromObject");
|
|
137
171
|
return result;
|
|
138
172
|
}
|
|
@@ -140,7 +174,6 @@ class OINOHtmlTemplate {
|
|
|
140
174
|
* Creates HTML Response from API result.
|
|
141
175
|
*
|
|
142
176
|
* @param result OINOResult-object
|
|
143
|
-
* @param removeUnusedTags whether to remove unused tags
|
|
144
177
|
* @param messageSeparator HTML separator for messages
|
|
145
178
|
* @param includeErrorMessages include debug messages in result
|
|
146
179
|
* @param includeWarningMessages include debug messages in result
|
|
@@ -148,7 +181,7 @@ class OINOHtmlTemplate {
|
|
|
148
181
|
* @param includeDebugMessages include debug messages in result
|
|
149
182
|
*
|
|
150
183
|
*/
|
|
151
|
-
renderFromResult(result,
|
|
184
|
+
renderFromResult(result, messageSeparator = "", includeErrorMessages = false, includeWarningMessages = false, includeInfoMessages = false, includeDebugMessages = false) {
|
|
152
185
|
_1.OINOBenchmark.startMetric("OINOHtmlTemplate", "renderFromResult");
|
|
153
186
|
this.setVariableFromValue("statusCode", result.statusCode.toString());
|
|
154
187
|
this.setVariableFromValue("statusMessage", result.statusMessage.toString());
|
|
@@ -170,7 +203,7 @@ class OINOHtmlTemplate {
|
|
|
170
203
|
if (messageSeparator && (messages.length > 0)) {
|
|
171
204
|
this.setVariableFromValue("messages", messages.join(messageSeparator), false); // messages have been escaped already
|
|
172
205
|
}
|
|
173
|
-
const http_result = this.render(
|
|
206
|
+
const http_result = this.render();
|
|
174
207
|
_1.OINOBenchmark.endMetric("OINOHtmlTemplate", "renderFromResult");
|
|
175
208
|
return http_result;
|
|
176
209
|
}
|
package/dist/cjs/OINOLog.js
CHANGED
|
@@ -186,14 +186,13 @@ class OINOLog {
|
|
|
186
186
|
return result;
|
|
187
187
|
}
|
|
188
188
|
/**
|
|
189
|
-
*
|
|
189
|
+
* 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).
|
|
190
190
|
*
|
|
191
191
|
* @param logLevels array of log level objects
|
|
192
192
|
*
|
|
193
193
|
*/
|
|
194
|
-
static
|
|
194
|
+
static setLogLevels(logLevels) {
|
|
195
195
|
if (OINOLog._instance) {
|
|
196
|
-
OINOLog._instance._logLevels = { "||": OINOLog._instance._defaultLogLevel }; // reset to default log level
|
|
197
196
|
for (const logLevel of logLevels) {
|
|
198
197
|
const domain = logLevel.domain || "";
|
|
199
198
|
const channel = logLevel.channel || "";
|
|
@@ -205,6 +204,18 @@ class OINOLog {
|
|
|
205
204
|
}
|
|
206
205
|
}
|
|
207
206
|
}
|
|
207
|
+
/**
|
|
208
|
+
* Import log levels from an array of objects with domain, channel, method and level resetting existing values (i.e. non-existing values get removed).
|
|
209
|
+
*
|
|
210
|
+
* @param logLevels array of log level objects
|
|
211
|
+
*
|
|
212
|
+
*/
|
|
213
|
+
static importLogLevels(logLevels) {
|
|
214
|
+
if (OINOLog._instance) {
|
|
215
|
+
OINOLog._instance._logLevels = { "||": OINOLog._instance._defaultLogLevel }; // reset to default log level
|
|
216
|
+
this.setLogLevels(logLevels);
|
|
217
|
+
}
|
|
218
|
+
}
|
|
208
219
|
}
|
|
209
220
|
exports.OINOLog = OINOLog;
|
|
210
221
|
/**
|
package/dist/cjs/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.OINOContentType = exports.OINO_DEBUG_PREFIX = exports.OINO_INFO_PREFIX = exports.OINO_WARNING_PREFIX = exports.OINO_ERROR_PREFIX = exports.OINOHtmlTemplate = exports.OINOStr = exports.OINOHttpResult = exports.OINOResult = exports.OINOConsoleLog = exports.OINOLogLevel = exports.OINOLog = exports.OINOMemoryBenchmark = exports.OINOBenchmark = void 0;
|
|
3
|
+
exports.OINOContentType = exports.OINO_DEBUG_PREFIX = exports.OINO_INFO_PREFIX = exports.OINO_WARNING_PREFIX = exports.OINO_ERROR_PREFIX = exports.OINO_EMPTY_FORMATTER = exports.OINOFormatter = exports.OINOHtmlTemplate = exports.OINOStr = exports.OINOHttpResult = exports.OINOResult = exports.OINOConsoleLog = exports.OINOLogLevel = exports.OINOLog = exports.OINOMemoryBenchmark = exports.OINOBenchmark = void 0;
|
|
4
4
|
var OINOBenchmark_js_1 = require("./OINOBenchmark.js");
|
|
5
5
|
Object.defineProperty(exports, "OINOBenchmark", { enumerable: true, get: function () { return OINOBenchmark_js_1.OINOBenchmark; } });
|
|
6
6
|
Object.defineProperty(exports, "OINOMemoryBenchmark", { enumerable: true, get: function () { return OINOBenchmark_js_1.OINOMemoryBenchmark; } });
|
|
@@ -15,6 +15,9 @@ var OINOStr_js_1 = require("./OINOStr.js");
|
|
|
15
15
|
Object.defineProperty(exports, "OINOStr", { enumerable: true, get: function () { return OINOStr_js_1.OINOStr; } });
|
|
16
16
|
var OINOHtmlTemplate_js_1 = require("./OINOHtmlTemplate.js");
|
|
17
17
|
Object.defineProperty(exports, "OINOHtmlTemplate", { enumerable: true, get: function () { return OINOHtmlTemplate_js_1.OINOHtmlTemplate; } });
|
|
18
|
+
var OINOFormatter_js_1 = require("./OINOFormatter.js");
|
|
19
|
+
Object.defineProperty(exports, "OINOFormatter", { enumerable: true, get: function () { return OINOFormatter_js_1.OINOFormatter; } });
|
|
20
|
+
Object.defineProperty(exports, "OINO_EMPTY_FORMATTER", { enumerable: true, get: function () { return OINOFormatter_js_1.OINO_EMPTY_FORMATTER; } });
|
|
18
21
|
/** OINO error message prefix */
|
|
19
22
|
exports.OINO_ERROR_PREFIX = "OINO ERROR";
|
|
20
23
|
/** OINO warning message prefix */
|
|
@@ -0,0 +1,157 @@
|
|
|
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
|
+
import { OINO_ERROR_PREFIX, OINOLog } from "./index.js";
|
|
7
|
+
/**
|
|
8
|
+
* Class for formatting strings and values.
|
|
9
|
+
*
|
|
10
|
+
*/
|
|
11
|
+
export class OINOFormatter {
|
|
12
|
+
static OINO_FORMATTER_REGEXP = /\s?(trim(\(\))?|trimLeft(\(\))?|trimRight(\(\))?|toUpper(\(\))?|toLower(\(\))?|cropLeft\((\d+)\)|cropRight\((\d+)\)|cropToDelimiter\(([^\(\),]+),(\d+)\)|cropFromDelimiter\(([^\(\),]+),(\d+)\)|substring\((\d+),(\d+)\)|replace\(([^\(\),]+),([^\(\),]+)\))\s?$/i;
|
|
13
|
+
_types;
|
|
14
|
+
_params;
|
|
15
|
+
/**
|
|
16
|
+
* Constructor of `OINOFormatter`
|
|
17
|
+
* @param types array of formatter types
|
|
18
|
+
* @param params array of formatter parameters according to type
|
|
19
|
+
*/
|
|
20
|
+
constructor(types, params) {
|
|
21
|
+
this._types = types;
|
|
22
|
+
this._params = params;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Constructor for `OINOFormatter` as parser of http parameter.
|
|
26
|
+
*
|
|
27
|
+
* @param formatters string or array of strings of serialized representation of formatters with following functions
|
|
28
|
+
* - trim()
|
|
29
|
+
* - trimLeft()
|
|
30
|
+
* - trimRight()
|
|
31
|
+
* - toUpper()
|
|
32
|
+
* - toLower()
|
|
33
|
+
* - cropLeft(charsToCrop)
|
|
34
|
+
* - cropRight(charsToCrop)
|
|
35
|
+
* - cropToDelimiter(delimiter,offsetChars)
|
|
36
|
+
* - cropFromDelimiter(delimiter,offsetChars)
|
|
37
|
+
* - substring(start,end)
|
|
38
|
+
* - replace(search,replace)
|
|
39
|
+
*/
|
|
40
|
+
static parse(formatters) {
|
|
41
|
+
if (typeof formatters === "string") {
|
|
42
|
+
formatters = [formatters];
|
|
43
|
+
}
|
|
44
|
+
if (!formatters || formatters.length === 0) {
|
|
45
|
+
return OINO_EMPTY_FORMATTER;
|
|
46
|
+
}
|
|
47
|
+
else {
|
|
48
|
+
const types = [];
|
|
49
|
+
const params = [];
|
|
50
|
+
for (let i = 0; i < formatters.length; i++) {
|
|
51
|
+
let match = formatters[i]?.match(this.OINO_FORMATTER_REGEXP);
|
|
52
|
+
if (!match) {
|
|
53
|
+
OINOLog.error("@oino-ts/common", "OINOFormatter", "parse", "Invalid formatter string", { formatter: formatters[i] });
|
|
54
|
+
throw new Error(OINO_ERROR_PREFIX + "Invalid formatter: " + formatters[i]);
|
|
55
|
+
}
|
|
56
|
+
else {
|
|
57
|
+
const formatter_type = match[1].toLowerCase().substring(0, match[1].indexOf('('));
|
|
58
|
+
const formatter_params = [];
|
|
59
|
+
if ((formatter_type === "trim") || (formatter_type === "trimleft") || (formatter_type === "trimright") || (formatter_type === "toupper") || (formatter_type === "tolower")) {
|
|
60
|
+
// no parameters
|
|
61
|
+
}
|
|
62
|
+
else if (formatter_type === "cropleft") {
|
|
63
|
+
formatter_params.push(parseInt(match[7]));
|
|
64
|
+
}
|
|
65
|
+
else if (formatter_type === "cropright") {
|
|
66
|
+
formatter_params.push(parseInt(match[8]));
|
|
67
|
+
}
|
|
68
|
+
else if (formatter_type === "croptodelimiter") {
|
|
69
|
+
formatter_params.push(decodeURIComponent(match[9]), parseInt(match[10]));
|
|
70
|
+
}
|
|
71
|
+
else if (formatter_type === "cropfromdelimiter") {
|
|
72
|
+
formatter_params.push(decodeURIComponent(match[11]), parseInt(match[12]));
|
|
73
|
+
}
|
|
74
|
+
else if (formatter_type === "substring") {
|
|
75
|
+
formatter_params.push(parseInt(match[13]), parseInt(match[14]));
|
|
76
|
+
}
|
|
77
|
+
else if (formatter_type === "replace") {
|
|
78
|
+
formatter_params.push(decodeURIComponent(match[15]), decodeURIComponent(match[16]));
|
|
79
|
+
}
|
|
80
|
+
else {
|
|
81
|
+
OINOLog.error("@oino-ts/common", "OINOFormatter", "parse", "Unknown formatter type", { formatter: formatters[i] });
|
|
82
|
+
throw new Error(OINO_ERROR_PREFIX + "Unsupported formatter: " + formatters[i]);
|
|
83
|
+
}
|
|
84
|
+
types.push(formatter_type);
|
|
85
|
+
params.push(formatter_params);
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
return new OINOFormatter(types, params);
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* Does formatter include any operations.
|
|
93
|
+
* @return true if formatter is empty
|
|
94
|
+
*/
|
|
95
|
+
isEmpty() {
|
|
96
|
+
return this._types.length === 0;
|
|
97
|
+
}
|
|
98
|
+
/**
|
|
99
|
+
* Applies all formatters in order to given value.
|
|
100
|
+
*
|
|
101
|
+
* @param value string value to be formatted
|
|
102
|
+
* @returns formatted string value
|
|
103
|
+
*/
|
|
104
|
+
format(value) {
|
|
105
|
+
let formatted = value;
|
|
106
|
+
for (let i = 0; i < this._types.length; i++) {
|
|
107
|
+
const formatter_type = this._types[i];
|
|
108
|
+
const formatter_params = this._params[i];
|
|
109
|
+
if (formatter_type === "trim") {
|
|
110
|
+
formatted = formatted.trim();
|
|
111
|
+
}
|
|
112
|
+
else if (formatter_type === "trimleft") {
|
|
113
|
+
formatted = formatted.trimStart();
|
|
114
|
+
}
|
|
115
|
+
else if (formatter_type === "trimright") {
|
|
116
|
+
formatted = formatted.trimEnd();
|
|
117
|
+
}
|
|
118
|
+
else if (formatter_type === "toupper") {
|
|
119
|
+
formatted = formatted.toUpperCase();
|
|
120
|
+
}
|
|
121
|
+
else if (formatter_type === "tolower") {
|
|
122
|
+
formatted = formatted.toLowerCase();
|
|
123
|
+
}
|
|
124
|
+
else if (formatter_type === "cropleft") {
|
|
125
|
+
formatted = formatted.slice(formatter_params[0]);
|
|
126
|
+
}
|
|
127
|
+
else if (formatter_type === "cropright") {
|
|
128
|
+
formatted = formatted.slice(0, formatted.length - formatter_params[0]);
|
|
129
|
+
}
|
|
130
|
+
else if (formatter_type === "croptodelimiter") {
|
|
131
|
+
const to_demilimiter_idx = formatted.indexOf(formatter_params[0]);
|
|
132
|
+
if (to_demilimiter_idx >= 0) {
|
|
133
|
+
formatted = formatted.slice(Math.max(to_demilimiter_idx + formatter_params[0].length + formatter_params[1], 0));
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
else if (formatter_type === "cropfromdelimiter") {
|
|
137
|
+
const from_demilimiter_idx = formatted.indexOf(formatter_params[0]);
|
|
138
|
+
if (from_demilimiter_idx >= 0) {
|
|
139
|
+
formatted = formatted.slice(0, Math.max(from_demilimiter_idx + formatter_params[1], 0));
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
else if (formatter_type === "substring") {
|
|
143
|
+
const start = formatter_params[0] ? parseInt(formatter_params[0]) : 0;
|
|
144
|
+
const end = formatter_params[1] ? parseInt(formatter_params[1]) : formatted.length;
|
|
145
|
+
formatted = formatted.substring(start, end);
|
|
146
|
+
}
|
|
147
|
+
else if (formatter_type === "replace") {
|
|
148
|
+
const search = formatter_params[0];
|
|
149
|
+
const replacement = formatter_params[1];
|
|
150
|
+
formatted = formatted.replaceAll(search, replacement);
|
|
151
|
+
}
|
|
152
|
+
// console.log("formatter:", formatter_type, "params:", formatter_params, "formatted:", formatted)
|
|
153
|
+
}
|
|
154
|
+
return formatted;
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
export const OINO_EMPTY_FORMATTER = new OINOFormatter([], []);
|