@oino-ts/common 0.1.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/src/OINOStr.ts ADDED
@@ -0,0 +1,254 @@
1
+ import { OINOContentType } from "."
2
+
3
+ /** Callback to filter data fields */
4
+ export type OINOStrEncoder = (str:string) => string
5
+
6
+ /**
7
+ * Static class string utilities.
8
+ *
9
+ */
10
+ export class OINOStr {
11
+
12
+ /**
13
+ * Split string by the top level of the given type of brackets.
14
+ * E.g. splitByBrackets("a(bc(d))ef(gh)kl", true, true, '(', ')') would return ["a", "bc(d)", "ef", "gh", "kl"]
15
+ *
16
+ * @param str string to split
17
+ * @param includePartsBetweenBlocks whether to include strings between top level brackets
18
+ * @param includeTrailingUnescapedBlock whether to include final block that is missing necessary end brackets
19
+ * @param startBracket starting bracket, e.g. '('
20
+ * @param endBracket ending bracket, e.g. ')'
21
+ *
22
+ */
23
+ static splitByBrackets(str:string, includePartsBetweenBlocks:boolean, includeTrailingUnescapedBlock:boolean, startBracket:string, endBracket:string):string[] {
24
+
25
+ let result:string[] = []
26
+ let parenthesis_count:number = 0
27
+ let start:number = 0
28
+ let end:number = 0
29
+ while (end<str.length) {
30
+ if (str[end] == startBracket) {
31
+ if (parenthesis_count == 0) {
32
+ if ((end > start) && includePartsBetweenBlocks) { // there is some first level string to add to result
33
+ result.push(str.substring(start, end))
34
+ }
35
+ start = end+1
36
+ }
37
+ parenthesis_count++
38
+
39
+ } else if (str[end] == endBracket) {
40
+ parenthesis_count--
41
+ if (parenthesis_count == 0) {
42
+ if (end >= start) {
43
+ result.push(str.substring(start, end))
44
+ }
45
+ start = end+1
46
+ }
47
+ }
48
+ end++
49
+ }
50
+ 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)
51
+ result.push(str.substring(start, end)) // i == str.length
52
+ }
53
+ return result
54
+ }
55
+
56
+ /**
57
+ * Split string by delimeter excluding delimeters inside given brackets.
58
+ * E.g. splitExcludingBrackets("a,(bc,d),ef,(g,h),k", ',', '(', ')') would return ["a", "bc,d", "ef", "g,h", "k"]
59
+ *
60
+ * @param str string to split
61
+ * @param delimeter string to use as delimeter
62
+ * @param startBracket starting bracket, e.g. '('
63
+ * @param endBracket ending bracket, e.g. ')'
64
+ */
65
+ static splitExcludingBrackets(str:string, delimeter:string, startBracket:string, endBracket:string):string[] {
66
+ let result:string[] = []
67
+ let bracket_level:number = 0
68
+ let start:number = 0
69
+ let end:number = 0
70
+ while (end<str.length) {
71
+ if (str[end] == startBracket) {
72
+ bracket_level++
73
+ } else if (str[end] == endBracket) {
74
+ bracket_level--
75
+ } else if ((str[end] == delimeter) && (bracket_level==0)) { // only delimeters at top level will break
76
+ result.push(str.substring(start, end))
77
+ start = end+1
78
+ }
79
+ end++
80
+ }
81
+ if (end > start) {
82
+ result.push(str.substring(start, end)) // i == str.length
83
+ }
84
+ return result
85
+ }
86
+
87
+ /**
88
+ * Encode OINO serialized strings as valid JSON.
89
+ *
90
+ * @param str string to encode
91
+ * @param valueType wether it is a value type
92
+ */
93
+ static encodeJSON(str:string|null|undefined, valueType:boolean = false):string {
94
+ if (str === undefined) { // no undefined literal in JSON
95
+ return "null"
96
+ } else if (str === null) {
97
+ return "null"
98
+ } else {
99
+ if (valueType) {
100
+ return str
101
+ } else {
102
+ return JSON.stringify(str)
103
+ }
104
+ }
105
+ }
106
+
107
+ /**
108
+ * Decode JSON string as OINO serialization.
109
+ *
110
+ * @param str string to decode
111
+ */
112
+ static decodeJSON(str:string):string {
113
+ return str // JSON parsing using JS methods, no need to decode anything
114
+ }
115
+
116
+ /**
117
+ * Encode OINO serialized strings as valid CSV.
118
+ *
119
+ * @param str string to encode
120
+ */
121
+ static encodeCSV(str:string|null|undefined):string {
122
+ if (str === undefined) {
123
+ return ""
124
+ } else if (str === null) {
125
+ return "null"
126
+ } else {
127
+ return "\"" + str.replaceAll("\"", "\"\"") + "\"";
128
+ }
129
+ }
130
+
131
+ /**
132
+ * Decode CSV string as OINO serialization.
133
+ *
134
+ * @param str string to decode
135
+ */
136
+ static decodeCSV(str:string):string {
137
+ return str.replaceAll("\"\"", "\"")
138
+ }
139
+
140
+ /**
141
+ * Encode OINO serialized strings as valid Formdata.
142
+ *
143
+ * @param str string to encode
144
+ */
145
+ static encodeFormdata(str:string|null|undefined):string {
146
+ if (str === undefined) {
147
+ return ""
148
+ } else if (str === null) {
149
+ return ""
150
+ } else {
151
+ return str
152
+ }
153
+ }
154
+
155
+ /**
156
+ * Decode Formdata string as OINO serialization.
157
+ *
158
+ * @param str string to decode
159
+ */
160
+ static decodeFormdata(str:string):string {
161
+ return str
162
+ }
163
+ /**
164
+ * Encode OINO serialized strings as valid Urlencode.
165
+ *
166
+ * @param str string to encode
167
+ */
168
+ static encodeUrlencode(str:string|null|undefined):string {
169
+ if (str === undefined) {
170
+ return ""
171
+ } else if (str === null) {
172
+ return "null"
173
+ } else {
174
+ return encodeURIComponent(str)
175
+ }
176
+ }
177
+
178
+ /**
179
+ * Decode Urlencode string as OINO serialization.
180
+ *
181
+ * @param str string to decode
182
+ */
183
+ static decodeUrlencode(str:string):string {
184
+ return decodeURIComponent(str)
185
+ }
186
+
187
+ /**
188
+ * Encode OINO serialized strings as valid HTML content.
189
+ *
190
+ * @param str string to encode
191
+ */
192
+ static encodeHtml(str:string|null|undefined):string {
193
+ if (str === undefined) {
194
+ return ""
195
+ } else if (str === null) {
196
+ return ""
197
+ } else {
198
+ return str.replaceAll('&', '&amp;').replaceAll('<', '&lt;').replaceAll('>', '&gt;').replaceAll('"', '&quot;').replaceAll("'", '&#039;')
199
+ }
200
+ }
201
+
202
+ /**
203
+ * Decode HTML string as OINO serialization.
204
+ *
205
+ * @param str string to encode
206
+ */
207
+ static decodeHtml(str:string):string {
208
+ return str.replaceAll('&amp;', '&').replaceAll('&lt;', '<').replaceAll('&gt;', '>').replaceAll('&quot;', '"').replaceAll('&#039;', "'")
209
+ }
210
+ /**
211
+ * Decode content type formatted string as OINO serialization.
212
+ *
213
+ * @param str string to decode
214
+ * @param contentType content type for serialization
215
+ *
216
+ */
217
+ static decode(str:string, contentType:OINOContentType):string {
218
+ if (contentType == OINOContentType.csv) {
219
+ return this.decodeCSV(str)
220
+ } else if (contentType == OINOContentType.json) {
221
+ return this.decodeJSON(str)
222
+ } else if (contentType == OINOContentType.formdata) {
223
+ return this.decodeFormdata(str)
224
+ } else if (contentType == OINOContentType.urlencode) {
225
+ return this.decodeUrlencode(str)
226
+ } else if (contentType == OINOContentType.html) {
227
+ return str
228
+ } else {
229
+ return str
230
+ }
231
+ }
232
+ /**
233
+ * Encode OINO serialized string to the content type formatting.
234
+ *
235
+ * @param str string to encode
236
+ * @param contentType content type for serialization
237
+ *
238
+ */
239
+ static encode(str:string|null|undefined, contentType:OINOContentType):string {
240
+ if (contentType == OINOContentType.csv) {
241
+ return this.encodeCSV(str)
242
+ } else if (contentType == OINOContentType.json) {
243
+ return this.encodeJSON(str)
244
+ } else if (contentType == OINOContentType.formdata) {
245
+ return this.encodeFormdata(str)
246
+ } else if (contentType == OINOContentType.urlencode) {
247
+ return this.encodeUrlencode(str)
248
+ } else if (contentType == OINOContentType.html) {
249
+ return this.encodeHtml(str)
250
+ } else {
251
+ return str || ""
252
+ }
253
+ }
254
+ }
package/src/index.ts ADDED
@@ -0,0 +1,31 @@
1
+ export { OINOBenchmark } from "./OINOBenchmark.js"
2
+ export { OINOLog, OINOLogLevel, OINOConsoleLog } from "./OINOLog.js"
3
+ export { OINOResult, OINOHttpResult } from "./OINOResult.js"
4
+ export { OINOStr } from "./OINOStr.js"
5
+ export { OINOHtmlTemplate } from "./OINOHtmlTemplate.js"
6
+
7
+ /** OINO error message prefix */
8
+ export const OINO_ERROR_PREFIX = "OINO ERROR"
9
+ /** OINO warning message prefix */
10
+ export const OINO_WARNING_PREFIX = "OINO WARNING"
11
+ /** OINO info message prefix */
12
+ export const OINO_INFO_PREFIX = "OINO INFO"
13
+ /** OINO debug message prefix */
14
+ export const OINO_DEBUG_PREFIX = "OINO DEBUG"
15
+
16
+ /**
17
+ * Supported content format mime-types
18
+ */
19
+ export enum OINOContentType {
20
+ /** JSON encoded data */
21
+ json='application/json',
22
+ /** CSV encoded data */
23
+ csv='text/csv',
24
+ /** Multipart encoded form data */
25
+ formdata='multipart/form-data',
26
+ /** URL encoded form data */
27
+ urlencode='application/x-www-form-urlencoded',
28
+ /** HTML encoded data (output only) */
29
+ html='text/html'
30
+ }
31
+