@oino-ts/types 0.0.11

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.
@@ -0,0 +1,261 @@
1
+ import { OINOContentType } from ".";
2
+ /**
3
+ * Static class string utilities.
4
+ *
5
+ */
6
+ export class OINOStr {
7
+ /**
8
+ * Split string by the top level of the given type of brackets.
9
+ * E.g. splitByBrackets("a(bc(d))ef(gh)kl", true, true, '(', ')') would return ["a", "bc(d)", "ef", "gh", "kl"]
10
+ *
11
+ * @param str string to split
12
+ * @param includePartsBetweenBlocks whether to include strings between top level brackets
13
+ * @param includeTrailingUnescapedBlock whether to include final block that is missing necessary end brackets
14
+ * @param startBracket starting bracket, e.g. '('
15
+ * @param endBracket ending bracket, e.g. ')'
16
+ *
17
+ */
18
+ static splitByBrackets(str, includePartsBetweenBlocks, includeTrailingUnescapedBlock, startBracket, endBracket) {
19
+ let result = [];
20
+ let parenthesis_count = 0;
21
+ let start = 0;
22
+ let end = 0;
23
+ while (end < str.length) {
24
+ if (str[end] == startBracket) {
25
+ if (parenthesis_count == 0) {
26
+ if ((end > start) && includePartsBetweenBlocks) { // there is some first level string to add to result
27
+ result.push(str.substring(start, end));
28
+ }
29
+ start = end + 1;
30
+ }
31
+ parenthesis_count++;
32
+ }
33
+ else if (str[end] == endBracket) {
34
+ parenthesis_count--;
35
+ if (parenthesis_count == 0) {
36
+ if (end >= start) {
37
+ result.push(str.substring(start, end));
38
+ }
39
+ start = end + 1;
40
+ }
41
+ }
42
+ end++;
43
+ }
44
+ if ((end > start) && ((includePartsBetweenBlocks && (parenthesis_count == 0)) || (includeTrailingUnescapedBlock && (parenthesis_count > 0)))) { // if there is stuff after last block or unfinished block (and those are supported)
45
+ result.push(str.substring(start, end)); // i == str.length
46
+ }
47
+ return result;
48
+ }
49
+ /**
50
+ * Split string by delimeter excluding delimeters inside given brackets.
51
+ * E.g. splitExcludingBrackets("a,(bc,d),ef,(g,h),k", ',', '(', ')') would return ["a", "bc,d", "ef", "g,h", "k"]
52
+ *
53
+ * @param str string to split
54
+ * @param delimeter string to use as delimeter
55
+ * @param startBracket starting bracket, e.g. '('
56
+ * @param endBracket ending bracket, e.g. ')'
57
+ */
58
+ static splitExcludingBrackets(str, delimeter, startBracket, endBracket) {
59
+ let result = [];
60
+ let bracket_level = 0;
61
+ let start = 0;
62
+ let end = 0;
63
+ while (end < str.length) {
64
+ if (str[end] == startBracket) {
65
+ bracket_level++;
66
+ }
67
+ else if (str[end] == endBracket) {
68
+ bracket_level--;
69
+ }
70
+ else if ((str[end] == delimeter) && (bracket_level == 0)) { // only delimeters at top level will break
71
+ result.push(str.substring(start, end));
72
+ start = end + 1;
73
+ }
74
+ end++;
75
+ }
76
+ if (end > start) {
77
+ result.push(str.substring(start, end)); // i == str.length
78
+ }
79
+ return result;
80
+ }
81
+ /**
82
+ * Encode OINO serialized strings as valid JSON.
83
+ *
84
+ * @param str string to encode
85
+ * @param valueType wether it is a value type
86
+ */
87
+ static encodeJSON(str, valueType = false) {
88
+ if (str === undefined) { // no undefined literal in JSON
89
+ return "null";
90
+ }
91
+ else if (str === null) {
92
+ return "null";
93
+ }
94
+ else {
95
+ if (valueType) {
96
+ return str;
97
+ }
98
+ else {
99
+ return JSON.stringify(str);
100
+ }
101
+ }
102
+ }
103
+ /**
104
+ * Decode JSON string as OINO serialization.
105
+ *
106
+ * @param str string to decode
107
+ */
108
+ static decodeJSON(str) {
109
+ return str; // JSON parsing using JS methods, no need to decode anything
110
+ }
111
+ /**
112
+ * Encode OINO serialized strings as valid CSV.
113
+ *
114
+ * @param str string to encode
115
+ */
116
+ static encodeCSV(str) {
117
+ if (str === undefined) {
118
+ return "";
119
+ }
120
+ else if (str === null) {
121
+ return "null";
122
+ }
123
+ else {
124
+ return "\"" + str.replaceAll("\"", "\"\"") + "\"";
125
+ }
126
+ }
127
+ /**
128
+ * Decode CSV string as OINO serialization.
129
+ *
130
+ * @param str string to decode
131
+ */
132
+ static decodeCSV(str) {
133
+ return str.replaceAll("\"\"", "\"");
134
+ }
135
+ /**
136
+ * Encode OINO serialized strings as valid Formdata.
137
+ *
138
+ * @param str string to encode
139
+ */
140
+ static encodeFormdata(str) {
141
+ if (str === undefined) {
142
+ return "";
143
+ }
144
+ else if (str === null) {
145
+ return "";
146
+ }
147
+ else {
148
+ return str;
149
+ }
150
+ }
151
+ /**
152
+ * Decode Formdata string as OINO serialization.
153
+ *
154
+ * @param str string to decode
155
+ */
156
+ static decodeFormdata(str) {
157
+ return str;
158
+ }
159
+ /**
160
+ * Encode OINO serialized strings as valid Urlencode.
161
+ *
162
+ * @param str string to encode
163
+ */
164
+ static encodeUrlencode(str) {
165
+ if (str === undefined) {
166
+ return "";
167
+ }
168
+ else if (str === null) {
169
+ return "null";
170
+ }
171
+ else {
172
+ return encodeURIComponent(str);
173
+ }
174
+ }
175
+ /**
176
+ * Decode Urlencode string as OINO serialization.
177
+ *
178
+ * @param str string to decode
179
+ */
180
+ static decodeUrlencode(str) {
181
+ return decodeURIComponent(str);
182
+ }
183
+ /**
184
+ * Encode OINO serialized strings as valid HTML content.
185
+ *
186
+ * @param str string to encode
187
+ */
188
+ static encodeHtml(str) {
189
+ if (str === undefined) {
190
+ return "";
191
+ }
192
+ else if (str === null) {
193
+ return "";
194
+ }
195
+ else {
196
+ return str.replaceAll('&', '&amp;').replaceAll('<', '&lt;').replaceAll('>', '&gt;').replaceAll('"', '&quot;').replaceAll("'", '&#039;');
197
+ }
198
+ }
199
+ /**
200
+ * Decode HTML string as OINO serialization.
201
+ *
202
+ * @param str string to encode
203
+ */
204
+ static decodeHtml(str) {
205
+ return str.replaceAll('&amp;', '&').replaceAll('&lt;', '<').replaceAll('&gt;', '>').replaceAll('&quot;', '"').replaceAll('&#039;', "'");
206
+ }
207
+ /**
208
+ * Decode content type formatted string as OINO serialization.
209
+ *
210
+ * @param str string to decode
211
+ * @param contentType content type for serialization
212
+ *
213
+ */
214
+ static decode(str, contentType) {
215
+ if (contentType == OINOContentType.csv) {
216
+ return this.decodeCSV(str);
217
+ }
218
+ else if (contentType == OINOContentType.json) {
219
+ return this.decodeJSON(str);
220
+ }
221
+ else if (contentType == OINOContentType.formdata) {
222
+ return this.decodeFormdata(str);
223
+ }
224
+ else if (contentType == OINOContentType.urlencode) {
225
+ return this.decodeUrlencode(str);
226
+ }
227
+ else if (contentType == OINOContentType.html) {
228
+ return str;
229
+ }
230
+ else {
231
+ return str;
232
+ }
233
+ }
234
+ /**
235
+ * Encode OINO serialized string to the content type formatting.
236
+ *
237
+ * @param str string to encode
238
+ * @param contentType content type for serialization
239
+ *
240
+ */
241
+ static encode(str, contentType) {
242
+ if (contentType == OINOContentType.csv) {
243
+ return this.encodeCSV(str);
244
+ }
245
+ else if (contentType == OINOContentType.json) {
246
+ return this.encodeJSON(str);
247
+ }
248
+ else if (contentType == OINOContentType.formdata) {
249
+ return this.encodeFormdata(str);
250
+ }
251
+ else if (contentType == OINOContentType.urlencode) {
252
+ return this.encodeUrlencode(str);
253
+ }
254
+ else if (contentType == OINOContentType.html) {
255
+ return this.encodeHtml(str);
256
+ }
257
+ else {
258
+ return str || "";
259
+ }
260
+ }
261
+ }
@@ -0,0 +1,26 @@
1
+ export { OINOBenchmark } from "./OINOBenchmark.js";
2
+ export { OINOLog, OINOLogLevel, OINOConsoleLog } from "./OINOLog.js";
3
+ export { OINOResult } from "./OINOResult.js";
4
+ export { OINOStr } from "./OINOStr.js";
5
+ /** OINO error message prefix */
6
+ export const OINO_ERROR_PREFIX = "OINO ERROR";
7
+ /** OINO warning message prefix */
8
+ export const OINO_WARNING_PREFIX = "OINO WARNING";
9
+ /** OINO info message prefix */
10
+ export const OINO_INFO_PREFIX = "OINO INFO";
11
+ /** OINO debug message prefix */
12
+ export const OINO_DEBUG_PREFIX = "OINO DEBUG";
13
+ /** Supported content format mime-types */
14
+ export var OINOContentType;
15
+ (function (OINOContentType) {
16
+ /** JSON encoded data */
17
+ OINOContentType["json"] = "application/json";
18
+ /** CSV encoded data */
19
+ OINOContentType["csv"] = "text/csv";
20
+ /** Multipart encoded form data */
21
+ OINOContentType["formdata"] = "multipart/form-data";
22
+ /** URL encoded form data */
23
+ OINOContentType["urlencode"] = "application/x-www-form-urlencoded";
24
+ /** HTML encoded data (output only) */
25
+ OINOContentType["html"] = "text/html";
26
+ })(OINOContentType || (OINOContentType = {}));
@@ -0,0 +1,33 @@
1
+ /**
2
+ * Static class for benchmarking functions.
3
+ *
4
+ */
5
+ export declare class OINOBenchmark {
6
+ private static _benchmarkCount;
7
+ private static _benchmarkData;
8
+ private static _benchmarkEnabled;
9
+ private static _benchmarkStart;
10
+ /**
11
+ * Reset benchmark data (but not what is enabled).
12
+ *
13
+ */
14
+ static reset(): void;
15
+ /**
16
+ * Set benchmark names that are enabled.
17
+ *
18
+ * @param names array of those benchmarks that are enabled
19
+ */
20
+ static setEnabled(names: string[]): void;
21
+ /**
22
+ * Start benchmark timing.
23
+ *
24
+ * @param name of the benchmark
25
+ */
26
+ static start(name: string): void;
27
+ /**
28
+ * Complete benchmark timing
29
+ *
30
+ * @param name of the benchmark
31
+ */
32
+ static end(name: string): number;
33
+ }
@@ -0,0 +1,105 @@
1
+ /** Logging levels */
2
+ export declare enum OINOLogLevel {
3
+ /** Debug messages */
4
+ debug = 0,
5
+ /** Informational messages */
6
+ info = 1,
7
+ /** Warning messages */
8
+ warn = 2,
9
+ /** Error messages */
10
+ error = 3
11
+ }
12
+ /**
13
+ * Abstract base class for logging implementations supporting
14
+ * - error, warning, info and debug channels
15
+ * - setting level of logs outputted
16
+ *
17
+ */
18
+ export declare abstract class OINOLog {
19
+ protected static _instance: OINOLog;
20
+ protected _logLevel: OINOLogLevel;
21
+ /**
22
+ * Abstract logging method to implement the actual logging operation.
23
+ *
24
+ * @param logLevel level of the log events
25
+ *
26
+ */
27
+ constructor(logLevel?: OINOLogLevel);
28
+ /**
29
+ * Abstract logging method to implement the actual logging operation.
30
+ *
31
+ * @param levelStr level string of the log event
32
+ * @param message message of the log event
33
+ * @param data structured data associated with the log event
34
+ *
35
+ */
36
+ protected abstract _writeLog(levelStr: string, message: string, data?: any): void;
37
+ /**
38
+ * Abstract logging method to implement the actual logging operation.
39
+ *
40
+ * @param level level of the log event
41
+ * @param levelStr level string of the log event
42
+ * @param message message of the log event
43
+ * @param data structured data associated with the log event
44
+ *
45
+ */
46
+ protected static _log(level: OINOLogLevel, levelStr: string, message: string, data?: any): void;
47
+ /**
48
+ * Set active logger and log level.
49
+ *
50
+ * @param logger logger instance
51
+ *
52
+ */
53
+ static setLogger(logger: OINOLog): void;
54
+ /**
55
+ * Set log level.
56
+ *
57
+ * @param logLevel log level to use
58
+ *
59
+ */
60
+ static setLogLevel(logLevel: OINOLogLevel): void;
61
+ /**
62
+ * Log error event.
63
+ *
64
+ * @param message message of the log event
65
+ * @param data structured data associated with the log event
66
+ *
67
+ */
68
+ static error(message: string, data?: any): void;
69
+ /**
70
+ * Log warning event.
71
+ *
72
+ * @param message message of the log event
73
+ * @param data structured data associated with the log event
74
+ *
75
+ */
76
+ static warning(message: string, data?: any): void;
77
+ /**
78
+ * Log info event.
79
+ *
80
+ * @param message message of the log event
81
+ * @param data structured data associated with the log event
82
+ *
83
+ */
84
+ static info(message: string, data?: any): void;
85
+ /**
86
+ * Log debug event.
87
+ *
88
+ * @param message message of the log event
89
+ * @param data structured data associated with the log event
90
+ *
91
+ */
92
+ static debug(message: string, data?: any): void;
93
+ }
94
+ /**
95
+ * Logging implementation based on console.log.
96
+ *
97
+ */
98
+ export declare class OINOConsoleLog extends OINOLog {
99
+ /**
100
+ * Constructor of `OINOConsoleLog`
101
+ * @param logLevel logging level
102
+ */
103
+ constructor(logLevel?: OINOLogLevel);
104
+ protected _writeLog(level: string, message: string, data?: any): void;
105
+ }
@@ -0,0 +1,69 @@
1
+ /**
2
+ * OINO API request result object with returned data and/or http status code/message and
3
+ * error / warning messages.
4
+ *
5
+ */
6
+ export declare class OINOResult {
7
+ /** Wheter request was successfully executed */
8
+ success: boolean;
9
+ /** HTTP status code */
10
+ statusCode: number;
11
+ /** HTTP status message */
12
+ statusMessage: string;
13
+ /** Error / warning messages */
14
+ messages: string[];
15
+ /**
16
+ * Constructor of OINOResult.
17
+ *
18
+ */
19
+ constructor();
20
+ /**
21
+ * Set HTTP OK status (does not reset messages).
22
+ *
23
+ */
24
+ setOk(): void;
25
+ /**
26
+ * Set HTTP error status using given code and message.
27
+ *
28
+ * @param statusCode HTTP status code
29
+ * @param statusMessage HTTP status message
30
+ * @param operation operation where error occured
31
+ *
32
+ */
33
+ setError(statusCode: number, statusMessage: string, operation: string): void;
34
+ /**
35
+ * Add warning message.
36
+ *
37
+ * @param message HTTP status message
38
+ * @param operation operation where warning occured
39
+ *
40
+ */
41
+ addWarning(message: string, operation: string): void;
42
+ /**
43
+ * Add info message.
44
+ *
45
+ * @param message HTTP status message
46
+ * @param operation operation where info occured
47
+ *
48
+ */
49
+ addInfo(message: string, operation: string): void;
50
+ /**
51
+ * Add debug message.
52
+ *
53
+ * @param message HTTP status message
54
+ * @param operation operation where debug occured
55
+ *
56
+ */
57
+ addDebug(message: string, operation: string): void;
58
+ /**
59
+ * Copy given messages to HTTP headers.
60
+ *
61
+ * @param headers HTTP headers
62
+ * @param copyErrors wether error messages should be copied (default true)
63
+ * @param copyWarnings wether warning messages should be copied (default false)
64
+ * @param copyInfos wether info messages should be copied (default false)
65
+ * @param copyDebug wether debug messages should be copied (default false)
66
+ *
67
+ */
68
+ copyMessagesToHeaders(headers: Headers, copyErrors?: boolean, copyWarnings?: boolean, copyInfos?: boolean, copyDebug?: boolean): void;
69
+ }
@@ -0,0 +1,108 @@
1
+ import { OINOContentType } from ".";
2
+ /** Callback to filter data fields */
3
+ export type OINOStrEncoder = (str: string) => string;
4
+ /**
5
+ * Static class string utilities.
6
+ *
7
+ */
8
+ export declare class OINOStr {
9
+ /**
10
+ * Split string by the top level of the given type of brackets.
11
+ * E.g. splitByBrackets("a(bc(d))ef(gh)kl", true, true, '(', ')') would return ["a", "bc(d)", "ef", "gh", "kl"]
12
+ *
13
+ * @param str string to split
14
+ * @param includePartsBetweenBlocks whether to include strings between top level brackets
15
+ * @param includeTrailingUnescapedBlock whether to include final block that is missing necessary end brackets
16
+ * @param startBracket starting bracket, e.g. '('
17
+ * @param endBracket ending bracket, e.g. ')'
18
+ *
19
+ */
20
+ static splitByBrackets(str: string, includePartsBetweenBlocks: boolean, includeTrailingUnescapedBlock: boolean, startBracket: string, endBracket: string): string[];
21
+ /**
22
+ * Split string by delimeter excluding delimeters inside given brackets.
23
+ * E.g. splitExcludingBrackets("a,(bc,d),ef,(g,h),k", ',', '(', ')') would return ["a", "bc,d", "ef", "g,h", "k"]
24
+ *
25
+ * @param str string to split
26
+ * @param delimeter string to use as delimeter
27
+ * @param startBracket starting bracket, e.g. '('
28
+ * @param endBracket ending bracket, e.g. ')'
29
+ */
30
+ static splitExcludingBrackets(str: string, delimeter: string, startBracket: string, endBracket: string): string[];
31
+ /**
32
+ * Encode OINO serialized strings as valid JSON.
33
+ *
34
+ * @param str string to encode
35
+ * @param valueType wether it is a value type
36
+ */
37
+ static encodeJSON(str: string | null | undefined, valueType?: boolean): string;
38
+ /**
39
+ * Decode JSON string as OINO serialization.
40
+ *
41
+ * @param str string to decode
42
+ */
43
+ static decodeJSON(str: string): string | null | undefined;
44
+ /**
45
+ * Encode OINO serialized strings as valid CSV.
46
+ *
47
+ * @param str string to encode
48
+ */
49
+ static encodeCSV(str: string | null | undefined): string;
50
+ /**
51
+ * Decode CSV string as OINO serialization.
52
+ *
53
+ * @param str string to decode
54
+ */
55
+ static decodeCSV(str: string): string | null | undefined;
56
+ /**
57
+ * Encode OINO serialized strings as valid Formdata.
58
+ *
59
+ * @param str string to encode
60
+ */
61
+ static encodeFormdata(str: string | null | undefined): string;
62
+ /**
63
+ * Decode Formdata string as OINO serialization.
64
+ *
65
+ * @param str string to decode
66
+ */
67
+ static decodeFormdata(str: string): string | null | undefined;
68
+ /**
69
+ * Encode OINO serialized strings as valid Urlencode.
70
+ *
71
+ * @param str string to encode
72
+ */
73
+ static encodeUrlencode(str: string | null | undefined): string;
74
+ /**
75
+ * Decode Urlencode string as OINO serialization.
76
+ *
77
+ * @param str string to decode
78
+ */
79
+ static decodeUrlencode(str: string): string | null | undefined;
80
+ /**
81
+ * Encode OINO serialized strings as valid HTML content.
82
+ *
83
+ * @param str string to encode
84
+ */
85
+ static encodeHtml(str: string | null | undefined): string;
86
+ /**
87
+ * Decode HTML string as OINO serialization.
88
+ *
89
+ * @param str string to encode
90
+ */
91
+ static decodeHtml(str: string): string | null | undefined;
92
+ /**
93
+ * Decode content type formatted string as OINO serialization.
94
+ *
95
+ * @param str string to decode
96
+ * @param contentType content type for serialization
97
+ *
98
+ */
99
+ static decode(str: string, contentType: OINOContentType): string | null | undefined;
100
+ /**
101
+ * Encode OINO serialized string to the content type formatting.
102
+ *
103
+ * @param str string to encode
104
+ * @param contentType content type for serialization
105
+ *
106
+ */
107
+ static encode(str: string | null | undefined, contentType: OINOContentType): string;
108
+ }
@@ -0,0 +1,25 @@
1
+ export { OINOBenchmark } from "./OINOBenchmark.js";
2
+ export { OINOLog, OINOLogLevel, OINOConsoleLog } from "./OINOLog.js";
3
+ export { OINOResult } from "./OINOResult.js";
4
+ export { OINOStr } from "./OINOStr.js";
5
+ /** OINO error message prefix */
6
+ export declare const OINO_ERROR_PREFIX = "OINO ERROR";
7
+ /** OINO warning message prefix */
8
+ export declare const OINO_WARNING_PREFIX = "OINO WARNING";
9
+ /** OINO info message prefix */
10
+ export declare const OINO_INFO_PREFIX = "OINO INFO";
11
+ /** OINO debug message prefix */
12
+ export declare const OINO_DEBUG_PREFIX = "OINO DEBUG";
13
+ /** Supported content format mime-types */
14
+ export declare enum OINOContentType {
15
+ /** JSON encoded data */
16
+ json = "application/json",
17
+ /** CSV encoded data */
18
+ csv = "text/csv",
19
+ /** Multipart encoded form data */
20
+ formdata = "multipart/form-data",
21
+ /** URL encoded form data */
22
+ urlencode = "application/x-www-form-urlencoded",
23
+ /** HTML encoded data (output only) */
24
+ html = "text/html"
25
+ }
package/package.json ADDED
@@ -0,0 +1,29 @@
1
+ {
2
+ "name": "@oino-ts/types",
3
+ "version": "0.0.11",
4
+ "description": "OINO TS package for types.",
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
+ },
23
+ "files": [
24
+ "src/*.ts",
25
+ "dist/cjs/*.js",
26
+ "dist/esm/*.js",
27
+ "dist/types/*.d.ts"
28
+ ]
29
+ }