sf-i-events 1.0.748 → 1.0.749

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/util.ts CHANGED
@@ -1,460 +1,460 @@
1
-
2
- const validateName = (name: string) => {
3
- if((name + "").length > 2) {
4
- return true;
5
- }
6
- return false;
7
- }
8
-
9
- function createDiagonalPattern1(color: string) {
10
- // create a 10x10 px canvas for the pattern's base shape
11
- let shape = document.createElement('canvas')
12
- shape.width = 10
13
- shape.height = 10
14
- // get the context for drawing
15
- let c = shape.getContext('2d')
16
- // draw 1st line of the shape
17
- c!.strokeStyle = color
18
- c!.lineWidth = 3
19
- c!.beginPath()
20
- c!.moveTo(2, 0)
21
- c!.lineTo(10, 8)
22
- c!.stroke()
23
- // draw 2nd line of the shape
24
- c!.beginPath()
25
- c!.moveTo(2, 10)
26
- c!.lineTo(0, 8)
27
- c!.stroke()
28
- // create the pattern from the shape
29
- return c!.createPattern(shape, 'repeat')
30
- }
31
-
32
- // function createDiagonalPattern2(color: string) {
33
- // let shape = document.createElement('canvas')
34
- // shape.width = 10
35
- // shape.height = 10
36
- // // get the context for drawing
37
- // let c = shape.getContext('2d')
38
- // // draw 1st line of the shape
39
- // c!.beginPath();
40
- // c!.rect(2, 2, 8, 8)
41
- // c!.fillStyle = color;
42
- // c!.fill();
43
- // // create the pattern from the shape
44
- // return c!.createPattern(shape, 'repeat')
45
- // }
46
-
47
- function createDiagonalPattern2(color: string) {
48
- // create a 10x10 px canvas for the pattern's base shape
49
- let shape = document.createElement('canvas')
50
- shape.width = 10
51
- shape.height = 10
52
- // get the context for drawing
53
- let c = shape.getContext('2d')
54
- // draw 1st line of the shape
55
- c!.beginPath();
56
- c!.rect(1, 1, 9, 9)
57
- c!.fillStyle = color;
58
- c!.fill();
59
- c!.lineWidth = 1;
60
- c!.strokeStyle = color;
61
- c!.stroke();
62
- // create the pattern from the shape
63
-
64
- // create tick
65
-
66
- //draw tick
67
- c!.beginPath();
68
- c!.moveTo(2,2);
69
- c!.lineTo(8,8);
70
- c!.moveTo(2,8);
71
- c!.lineTo(8,2);
72
- c!.lineWidth = 2;
73
- c!.strokeStyle = '#fff';
74
- c!.stroke();
75
-
76
- return c!.createPattern(shape, 'repeat')
77
- }
78
-
79
- function createDiagonalPattern3(color: string) {
80
- // create a 10x10 px canvas for the pattern's base shape
81
- let shape = document.createElement('canvas')
82
- shape.width = 10
83
- shape.height = 10
84
- // get the context for drawing
85
- let c = shape.getContext('2d')
86
- // draw 1st line of the shape
87
- c!.beginPath();
88
- c!.rect(1, 1, 9, 9)
89
- c!.fillStyle = color;
90
- c!.fill();
91
- c!.lineWidth = 1;
92
- c!.strokeStyle = color;
93
- c!.stroke();
94
- // create the pattern from the shape
95
-
96
- // create tick
97
-
98
- //draw tick
99
- c!.beginPath();
100
- c!.moveTo(2,5);
101
- c!.lineTo(4,7);
102
- c!.lineTo(8,2);
103
- c!.lineWidth = 2;
104
- c!.strokeStyle = '#fff';
105
- c!.stroke();
106
-
107
- return c!.createPattern(shape, 'repeat')
108
- }
109
-
110
- // function createDiagonalPattern3(color: string) {
111
- // // create a 10x10 px canvas for the pattern's base shape
112
- // let shape = document.createElement('canvas')
113
- // shape.width = 14
114
- // shape.height = 14
115
- // // get the context for drawing
116
- // let c = shape.getContext('2d')
117
- // // draw 1st line of the shape
118
- // c!.beginPath();
119
- // c!.arc(7, 7, 6, 0, 2 * Math.PI, false);
120
- // c!.fillStyle = color;
121
- // c!.fill();
122
- // c!.lineWidth = 1;
123
- // c!.strokeStyle = color;
124
- // c!.stroke();
125
- // // create the pattern from the shape
126
-
127
- // // create tick
128
-
129
- // //draw tick
130
- // c!.beginPath();
131
- // c!.moveTo(4,6);
132
- // c!.lineTo(6,9);
133
- // c!.lineTo(10,4);
134
- // c!.lineWidth = 2;
135
- // c!.strokeStyle = '#fff';
136
- // c!.stroke();
137
-
138
- // return c!.createPattern(shape, 'repeat')
139
- // }
140
-
141
- // function createDiagonalPattern3(color: string) {
142
- // // create a 10x10 px canvas for the pattern's base shape
143
- // let shape = document.createElement('canvas')
144
- // shape.width = 10
145
- // shape.height = 10
146
- // // get the context for drawing
147
- // let c = shape.getContext('2d')
148
- // // draw 1st line of the shape
149
- // c!.beginPath();
150
- // c!.arc(5, 5, 4, 0, 2 * Math.PI, false);
151
- // c!.fillStyle = color;
152
- // c!.fill();
153
- // c!.lineWidth = 1;
154
- // c!.strokeStyle = color;
155
- // c!.stroke();
156
- // // create the pattern from the shape
157
- // return c!.createPattern(shape, 'repeat')
158
- // }
159
-
160
- const timeSince = (date: number) => {
161
-
162
- var seconds = Math.floor((new Date().getTime() - date) / 1000);
163
-
164
- if(seconds > 0) {
165
-
166
- var interval = seconds / 31536000;
167
-
168
- if (interval > 1) {
169
- return Math.floor(interval) + " years ago";
170
- }
171
- interval = seconds / 2592000;
172
- if (interval > 1) {
173
- return Math.floor(interval) + " months ago";
174
- }
175
- interval = seconds / 86400;
176
- if (interval > 1) {
177
- return Math.floor(interval) + " days ago";
178
- }
179
- interval = seconds / 3600;
180
- if (interval > 1) {
181
- return Math.floor(interval) + " hours ago";
182
- }
183
- interval = seconds / 60;
184
- if (interval > 1) {
185
- return Math.floor(interval) + " minutes ago";
186
- }
187
- return Math.floor(seconds) + " seconds ago";
188
-
189
- } else {
190
-
191
- var interval = Math.abs(seconds) / 31536000;
192
-
193
-
194
- console.log('timesince', seconds);
195
-
196
-
197
- console.log('interval year', interval);
198
- if (interval > 1) {
199
- return Math.floor(interval) + " years later";
200
- }
201
- interval = Math.abs(seconds) / 2592000;
202
- console.log('interval months', interval);
203
- if (interval > 1) {
204
- return Math.floor(interval) + " months later";
205
- }
206
-
207
- interval = Math.abs(seconds) / 86400;
208
- console.log('interval days', interval);
209
- if (interval > 1) {
210
- return Math.floor(interval) + " days later";
211
- }
212
-
213
- interval = Math.abs(seconds) / 3600;
214
- console.log('interval hours', interval);
215
- if (interval > 1) {
216
- return Math.floor(interval) + " hours later";
217
- }
218
- interval = Math.abs(seconds) / 60;
219
- if (interval > 1) {
220
- return Math.floor(interval) + " minutes later";
221
- }
222
- return Math.floor(Math.abs(seconds)) + " seconds";
223
-
224
- }
225
- }
226
-
227
-
228
- function readCookie(key: string) {
229
- let name = key + "=";
230
- let decodedCookie = decodeURIComponent(document.cookie);
231
- let ca = decodedCookie.split(';');
232
- for(let i = 0; i <ca.length; i++) {
233
- let c = ca[i];
234
- while (c.charAt(0) == ' ') {
235
- c = c.substring(1);
236
- }
237
- if (c.indexOf(name) == 0) {
238
- return c.substring(name.length, c.length);
239
- }
240
- }
241
- return "";
242
- }
243
-
244
- async function callApi(url: string, data: string, authorization: any) {
245
-
246
- return new Promise((resolve: any) => {
247
-
248
- const jsonData = JSON.stringify(data);
249
- var xhr = new XMLHttpRequest();
250
- xhr.addEventListener("readystatechange", () => {
251
- if(xhr != null) {
252
- if(xhr.readyState === 4) {
253
- resolve(xhr);
254
- }
255
- }
256
- });
257
- xhr.open("POST", url);
258
- xhr.timeout = 1800000;
259
- xhr.setRequestHeader("Content-Type", "application/json");
260
- xhr.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
261
- if(authorization != null) {
262
- xhr.setRequestHeader('Authorization', 'Basic ' + authorization);
263
- }
264
- xhr.send(jsonData);
265
-
266
- return xhr;
267
-
268
- })
269
-
270
- }
271
-
272
-
273
- async function callApiPresignedDelete(url: string) {
274
-
275
- return new Promise((resolve: any) => {
276
-
277
- var xhr = new XMLHttpRequest();
278
- xhr.addEventListener("readystatechange", () => {
279
- if(xhr != null) {
280
- if(xhr.readyState === 4) {
281
- resolve(xhr);
282
- }
283
- }
284
- });
285
- xhr.open("DELETE", url);
286
- xhr.timeout = 1800000;
287
- xhr.setRequestHeader("Content-Type", "application/json");
288
- xhr.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
289
- xhr.send(null);
290
-
291
- return xhr;
292
-
293
- })
294
-
295
- }
296
-
297
- async function callApiPresignedGet(url: string) {
298
-
299
- return new Promise((resolve: any) => {
300
-
301
- var xhr = new XMLHttpRequest();
302
- xhr.addEventListener("readystatechange", () => {
303
- if(xhr != null) {
304
- if(xhr.readyState === 4) {
305
- resolve(xhr);
306
- }
307
- }
308
- });
309
- xhr.open("GET", url);
310
- xhr.timeout = 1800000;
311
- xhr.setRequestHeader("Content-Type", "application/json");
312
- xhr.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
313
- xhr.send(null);
314
-
315
- return xhr;
316
-
317
- })
318
-
319
- }
320
-
321
- async function callApiPresigned(url: string, data: string) {
322
-
323
- return new Promise((resolve: any) => {
324
-
325
- const jsonData = JSON.stringify(data);
326
- var xhr = new XMLHttpRequest();
327
- xhr.addEventListener("readystatechange", () => {
328
- if(xhr != null) {
329
- if(xhr.readyState === 4) {
330
- resolve(xhr);
331
- }
332
- }
333
- });
334
- xhr.open("PUT", url);
335
- xhr.timeout = 1800000;
336
- xhr.setRequestHeader("Content-Type", "application/json");
337
- xhr.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
338
- xhr.send(jsonData);
339
-
340
- return xhr;
341
-
342
- })
343
-
344
- }
345
-
346
- function getRandomColor() {
347
- var letters = '0123456789ABCDEF';
348
- var color = '#';
349
- for (var i = 0; i < 6; i++) {
350
- color += letters[Math.floor(Math.random() * 16)];
351
- }
352
- return color;
353
- }
354
-
355
- function isInteger(value: string) {
356
- return /^-?\d+$/.test(value);
357
- }
358
-
359
- function clearListeners(old_element: HTMLElement) {
360
- var new_element = old_element.cloneNode(true);
361
- old_element.parentNode?.replaceChild(new_element, old_element);
362
- return new_element;
363
- }
364
-
365
- function jsonObjectToHtml(json: any) {
366
-
367
- var html = '';
368
-
369
-
370
- for(var i = 0; i < Object.keys(json).length; i++) {
371
- var key = Object.keys(json)[i];
372
- html += '<div>';
373
- html += ('<span style="margin-left: 0px; padding-left: 0px">' + key + ':</span>&nbsp;');
374
- html += ('<span class="td-body" part="td-head"><sf-i-elastic-text text="' + json[key].replace(/"/g,'') + '" lineSize="6" minLength="10"></sf-i-elastic-text></span>');
375
- html += '</div>';
376
- }
377
-
378
-
379
- return html;
380
-
381
- }
382
-
383
- function convertToCSV(arr:any[]) {
384
- const array = [Object.keys(arr[0])].concat(arr)
385
- return array.map((it, index) => {
386
- let strIt = ""
387
- for(let [i,objkey] of Object.keys(it).entries()){
388
- if(index == 0){
389
- strIt += "\"" + it[objkey as any].replace(/"/g,"\"\"") + "\""
390
- }else{
391
- strIt += "\"" + JSON.stringify(it[objkey as any]).replace(/"/g,"\"\"") + "\""
392
- }
393
- if(i < (Object.keys(it).length - 1)){
394
- strIt += ","
395
- }
396
- }
397
- return strIt
398
- }).join('\n')
399
- }
400
-
401
- function parseCsv(csv:string){
402
- const re = /(,|\r?\n|\r|^)(?:"([^"]*(?:""[^"]*)*)"|([^,\r\n]*))/gi
403
- const result:any = [[]]
404
- let matches:any;
405
- while ((matches = re.exec(csv))) {
406
- if (matches[1].length && matches[1] !== ',') result.push([])
407
- result[result.length - 1].push(
408
- matches[2] !== undefined ? matches[2].replace(/""/g, '"') : matches[3]
409
- )
410
- }
411
- console.log('csv arr result', result, result.length);
412
- let csvResult = arrayToObject(result)
413
- return csvResult
414
- }
415
- function arrayToObject (csvArray:any) {
416
-
417
- //Take the first line (headers) from the array and remove it from the array.
418
- const headers = csvArray.shift()
419
-
420
- // Iterate through the rows and reduce each column to an object
421
-
422
- return csvArray.filter((row:any)=>{
423
- console.log('csv object keys lngth', Object.keys(row).length)
424
- return Object.keys(row).length > 1
425
- }).map((row: { [x: string]: any; } ) =>{
426
- return headers.reduce((acc: any, currentHeader: any, i: string | number) => ((currentHeader.indexOf('cols_') >= 0 || currentHeader == '') ? acc : ((row[i] != '' && row[i] != null) ? { ...acc, ...{ [currentHeader]: JSON.parse(row[i] == "TRUE" ? "true" : row[i])}} : acc)), {})
427
- })
428
- }
429
-
430
- function titleCase(str: string) {
431
- var splitStr = str.toLowerCase().split(' ');
432
- for (var i = 0; i < splitStr.length; i++) {
433
- // You do not need to check if i is larger than splitStr length, as your for does that for you
434
- // Assign it back to the array
435
- splitStr[i] = splitStr[i].charAt(0).toUpperCase() + splitStr[i].substring(1);
436
- }
437
- // Directly return the joined string
438
- return splitStr.join(' ');
439
- }
440
-
441
- function alphabeticalSort(arr:string[]){
442
- let arrSorted = arr.sort((a,b) => {
443
- return a.trim().toLowerCase().localeCompare(b.trim().toLowerCase())
444
- })
445
- return arrSorted
446
- }
447
-
448
- function percentageString(val: number, valTotal: number){
449
- if(valTotal == 0){
450
- return ""
451
- }
452
- let num = (100 * val) / valTotal
453
- return " (" + (Math.round((num + Number.EPSILON) * 100) / 100) + "%)"
454
- }
455
-
456
- const exportFunctions = {
457
- callApiPresignedDelete, callApiPresignedGet, callApiPresigned, jsonObjectToHtml, clearListeners, isInteger, callApi, validateName, readCookie, timeSince, createDiagonalPattern1, createDiagonalPattern2, createDiagonalPattern3, getRandomColor, convertToCSV, parseCsv, titleCase, alphabeticalSort, percentageString
458
- };
459
-
1
+
2
+ const validateName = (name: string) => {
3
+ if((name + "").length > 2) {
4
+ return true;
5
+ }
6
+ return false;
7
+ }
8
+
9
+ function createDiagonalPattern1(color: string) {
10
+ // create a 10x10 px canvas for the pattern's base shape
11
+ let shape = document.createElement('canvas')
12
+ shape.width = 10
13
+ shape.height = 10
14
+ // get the context for drawing
15
+ let c = shape.getContext('2d')
16
+ // draw 1st line of the shape
17
+ c!.strokeStyle = color
18
+ c!.lineWidth = 3
19
+ c!.beginPath()
20
+ c!.moveTo(2, 0)
21
+ c!.lineTo(10, 8)
22
+ c!.stroke()
23
+ // draw 2nd line of the shape
24
+ c!.beginPath()
25
+ c!.moveTo(2, 10)
26
+ c!.lineTo(0, 8)
27
+ c!.stroke()
28
+ // create the pattern from the shape
29
+ return c!.createPattern(shape, 'repeat')
30
+ }
31
+
32
+ // function createDiagonalPattern2(color: string) {
33
+ // let shape = document.createElement('canvas')
34
+ // shape.width = 10
35
+ // shape.height = 10
36
+ // // get the context for drawing
37
+ // let c = shape.getContext('2d')
38
+ // // draw 1st line of the shape
39
+ // c!.beginPath();
40
+ // c!.rect(2, 2, 8, 8)
41
+ // c!.fillStyle = color;
42
+ // c!.fill();
43
+ // // create the pattern from the shape
44
+ // return c!.createPattern(shape, 'repeat')
45
+ // }
46
+
47
+ function createDiagonalPattern2(color: string) {
48
+ // create a 10x10 px canvas for the pattern's base shape
49
+ let shape = document.createElement('canvas')
50
+ shape.width = 10
51
+ shape.height = 10
52
+ // get the context for drawing
53
+ let c = shape.getContext('2d')
54
+ // draw 1st line of the shape
55
+ c!.beginPath();
56
+ c!.rect(1, 1, 9, 9)
57
+ c!.fillStyle = color;
58
+ c!.fill();
59
+ c!.lineWidth = 1;
60
+ c!.strokeStyle = color;
61
+ c!.stroke();
62
+ // create the pattern from the shape
63
+
64
+ // create tick
65
+
66
+ //draw tick
67
+ c!.beginPath();
68
+ c!.moveTo(2,2);
69
+ c!.lineTo(8,8);
70
+ c!.moveTo(2,8);
71
+ c!.lineTo(8,2);
72
+ c!.lineWidth = 2;
73
+ c!.strokeStyle = '#fff';
74
+ c!.stroke();
75
+
76
+ return c!.createPattern(shape, 'repeat')
77
+ }
78
+
79
+ function createDiagonalPattern3(color: string) {
80
+ // create a 10x10 px canvas for the pattern's base shape
81
+ let shape = document.createElement('canvas')
82
+ shape.width = 10
83
+ shape.height = 10
84
+ // get the context for drawing
85
+ let c = shape.getContext('2d')
86
+ // draw 1st line of the shape
87
+ c!.beginPath();
88
+ c!.rect(1, 1, 9, 9)
89
+ c!.fillStyle = color;
90
+ c!.fill();
91
+ c!.lineWidth = 1;
92
+ c!.strokeStyle = color;
93
+ c!.stroke();
94
+ // create the pattern from the shape
95
+
96
+ // create tick
97
+
98
+ //draw tick
99
+ c!.beginPath();
100
+ c!.moveTo(2,5);
101
+ c!.lineTo(4,7);
102
+ c!.lineTo(8,2);
103
+ c!.lineWidth = 2;
104
+ c!.strokeStyle = '#fff';
105
+ c!.stroke();
106
+
107
+ return c!.createPattern(shape, 'repeat')
108
+ }
109
+
110
+ // function createDiagonalPattern3(color: string) {
111
+ // // create a 10x10 px canvas for the pattern's base shape
112
+ // let shape = document.createElement('canvas')
113
+ // shape.width = 14
114
+ // shape.height = 14
115
+ // // get the context for drawing
116
+ // let c = shape.getContext('2d')
117
+ // // draw 1st line of the shape
118
+ // c!.beginPath();
119
+ // c!.arc(7, 7, 6, 0, 2 * Math.PI, false);
120
+ // c!.fillStyle = color;
121
+ // c!.fill();
122
+ // c!.lineWidth = 1;
123
+ // c!.strokeStyle = color;
124
+ // c!.stroke();
125
+ // // create the pattern from the shape
126
+
127
+ // // create tick
128
+
129
+ // //draw tick
130
+ // c!.beginPath();
131
+ // c!.moveTo(4,6);
132
+ // c!.lineTo(6,9);
133
+ // c!.lineTo(10,4);
134
+ // c!.lineWidth = 2;
135
+ // c!.strokeStyle = '#fff';
136
+ // c!.stroke();
137
+
138
+ // return c!.createPattern(shape, 'repeat')
139
+ // }
140
+
141
+ // function createDiagonalPattern3(color: string) {
142
+ // // create a 10x10 px canvas for the pattern's base shape
143
+ // let shape = document.createElement('canvas')
144
+ // shape.width = 10
145
+ // shape.height = 10
146
+ // // get the context for drawing
147
+ // let c = shape.getContext('2d')
148
+ // // draw 1st line of the shape
149
+ // c!.beginPath();
150
+ // c!.arc(5, 5, 4, 0, 2 * Math.PI, false);
151
+ // c!.fillStyle = color;
152
+ // c!.fill();
153
+ // c!.lineWidth = 1;
154
+ // c!.strokeStyle = color;
155
+ // c!.stroke();
156
+ // // create the pattern from the shape
157
+ // return c!.createPattern(shape, 'repeat')
158
+ // }
159
+
160
+ const timeSince = (date: number) => {
161
+
162
+ var seconds = Math.floor((new Date().getTime() - date) / 1000);
163
+
164
+ if(seconds > 0) {
165
+
166
+ var interval = seconds / 31536000;
167
+
168
+ if (interval > 1) {
169
+ return Math.floor(interval) + " years ago";
170
+ }
171
+ interval = seconds / 2592000;
172
+ if (interval > 1) {
173
+ return Math.floor(interval) + " months ago";
174
+ }
175
+ interval = seconds / 86400;
176
+ if (interval > 1) {
177
+ return Math.floor(interval) + " days ago";
178
+ }
179
+ interval = seconds / 3600;
180
+ if (interval > 1) {
181
+ return Math.floor(interval) + " hours ago";
182
+ }
183
+ interval = seconds / 60;
184
+ if (interval > 1) {
185
+ return Math.floor(interval) + " minutes ago";
186
+ }
187
+ return Math.floor(seconds) + " seconds ago";
188
+
189
+ } else {
190
+
191
+ var interval = Math.abs(seconds) / 31536000;
192
+
193
+
194
+ console.log('timesince', seconds);
195
+
196
+
197
+ console.log('interval year', interval);
198
+ if (interval > 1) {
199
+ return Math.floor(interval) + " years later";
200
+ }
201
+ interval = Math.abs(seconds) / 2592000;
202
+ console.log('interval months', interval);
203
+ if (interval > 1) {
204
+ return Math.floor(interval) + " months later";
205
+ }
206
+
207
+ interval = Math.abs(seconds) / 86400;
208
+ console.log('interval days', interval);
209
+ if (interval > 1) {
210
+ return Math.floor(interval) + " days later";
211
+ }
212
+
213
+ interval = Math.abs(seconds) / 3600;
214
+ console.log('interval hours', interval);
215
+ if (interval > 1) {
216
+ return Math.floor(interval) + " hours later";
217
+ }
218
+ interval = Math.abs(seconds) / 60;
219
+ if (interval > 1) {
220
+ return Math.floor(interval) + " minutes later";
221
+ }
222
+ return Math.floor(Math.abs(seconds)) + " seconds";
223
+
224
+ }
225
+ }
226
+
227
+
228
+ function readCookie(key: string) {
229
+ let name = key + "=";
230
+ let decodedCookie = decodeURIComponent(document.cookie);
231
+ let ca = decodedCookie.split(';');
232
+ for(let i = 0; i <ca.length; i++) {
233
+ let c = ca[i];
234
+ while (c.charAt(0) == ' ') {
235
+ c = c.substring(1);
236
+ }
237
+ if (c.indexOf(name) == 0) {
238
+ return c.substring(name.length, c.length);
239
+ }
240
+ }
241
+ return "";
242
+ }
243
+
244
+ async function callApi(url: string, data: string, authorization: any) {
245
+
246
+ return new Promise((resolve: any) => {
247
+
248
+ const jsonData = JSON.stringify(data);
249
+ var xhr = new XMLHttpRequest();
250
+ xhr.addEventListener("readystatechange", () => {
251
+ if(xhr != null) {
252
+ if(xhr.readyState === 4) {
253
+ resolve(xhr);
254
+ }
255
+ }
256
+ });
257
+ xhr.open("POST", url);
258
+ xhr.timeout = 1800000;
259
+ xhr.setRequestHeader("Content-Type", "application/json");
260
+ xhr.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
261
+ if(authorization != null) {
262
+ xhr.setRequestHeader('Authorization', 'Basic ' + authorization);
263
+ }
264
+ xhr.send(jsonData);
265
+
266
+ return xhr;
267
+
268
+ })
269
+
270
+ }
271
+
272
+
273
+ async function callApiPresignedDelete(url: string) {
274
+
275
+ return new Promise((resolve: any) => {
276
+
277
+ var xhr = new XMLHttpRequest();
278
+ xhr.addEventListener("readystatechange", () => {
279
+ if(xhr != null) {
280
+ if(xhr.readyState === 4) {
281
+ resolve(xhr);
282
+ }
283
+ }
284
+ });
285
+ xhr.open("DELETE", url);
286
+ xhr.timeout = 1800000;
287
+ xhr.setRequestHeader("Content-Type", "application/json");
288
+ xhr.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
289
+ xhr.send(null);
290
+
291
+ return xhr;
292
+
293
+ })
294
+
295
+ }
296
+
297
+ async function callApiPresignedGet(url: string) {
298
+
299
+ return new Promise((resolve: any) => {
300
+
301
+ var xhr = new XMLHttpRequest();
302
+ xhr.addEventListener("readystatechange", () => {
303
+ if(xhr != null) {
304
+ if(xhr.readyState === 4) {
305
+ resolve(xhr);
306
+ }
307
+ }
308
+ });
309
+ xhr.open("GET", url);
310
+ xhr.timeout = 1800000;
311
+ xhr.setRequestHeader("Content-Type", "application/json");
312
+ xhr.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
313
+ xhr.send(null);
314
+
315
+ return xhr;
316
+
317
+ })
318
+
319
+ }
320
+
321
+ async function callApiPresigned(url: string, data: string) {
322
+
323
+ return new Promise((resolve: any) => {
324
+
325
+ const jsonData = JSON.stringify(data);
326
+ var xhr = new XMLHttpRequest();
327
+ xhr.addEventListener("readystatechange", () => {
328
+ if(xhr != null) {
329
+ if(xhr.readyState === 4) {
330
+ resolve(xhr);
331
+ }
332
+ }
333
+ });
334
+ xhr.open("PUT", url);
335
+ xhr.timeout = 1800000;
336
+ xhr.setRequestHeader("Content-Type", "application/json");
337
+ xhr.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
338
+ xhr.send(jsonData);
339
+
340
+ return xhr;
341
+
342
+ })
343
+
344
+ }
345
+
346
+ function getRandomColor() {
347
+ var letters = '0123456789ABCDEF';
348
+ var color = '#';
349
+ for (var i = 0; i < 6; i++) {
350
+ color += letters[Math.floor(Math.random() * 16)];
351
+ }
352
+ return color;
353
+ }
354
+
355
+ function isInteger(value: string) {
356
+ return /^-?\d+$/.test(value);
357
+ }
358
+
359
+ function clearListeners(old_element: HTMLElement) {
360
+ var new_element = old_element.cloneNode(true);
361
+ old_element.parentNode?.replaceChild(new_element, old_element);
362
+ return new_element;
363
+ }
364
+
365
+ function jsonObjectToHtml(json: any) {
366
+
367
+ var html = '';
368
+
369
+
370
+ for(var i = 0; i < Object.keys(json).length; i++) {
371
+ var key = Object.keys(json)[i];
372
+ html += '<div>';
373
+ html += ('<span style="margin-left: 0px; padding-left: 0px">' + key + ':</span>&nbsp;');
374
+ html += ('<span class="td-body" part="td-head"><sf-i-elastic-text text="' + json[key].replace(/"/g,'') + '" lineSize="6" minLength="10"></sf-i-elastic-text></span>');
375
+ html += '</div>';
376
+ }
377
+
378
+
379
+ return html;
380
+
381
+ }
382
+
383
+ function convertToCSV(arr:any[]) {
384
+ const array = [Object.keys(arr[0])].concat(arr)
385
+ return array.map((it, index) => {
386
+ let strIt = ""
387
+ for(let [i,objkey] of Object.keys(it).entries()){
388
+ if(index == 0){
389
+ strIt += "\"" + it[objkey as any].replace(/"/g,"\"\"") + "\""
390
+ }else{
391
+ strIt += "\"" + JSON.stringify(it[objkey as any]).replace(/"/g,"\"\"") + "\""
392
+ }
393
+ if(i < (Object.keys(it).length - 1)){
394
+ strIt += ","
395
+ }
396
+ }
397
+ return strIt
398
+ }).join('\n')
399
+ }
400
+
401
+ function parseCsv(csv:string){
402
+ const re = /(,|\r?\n|\r|^)(?:"([^"]*(?:""[^"]*)*)"|([^,\r\n]*))/gi
403
+ const result:any = [[]]
404
+ let matches:any;
405
+ while ((matches = re.exec(csv))) {
406
+ if (matches[1].length && matches[1] !== ',') result.push([])
407
+ result[result.length - 1].push(
408
+ matches[2] !== undefined ? matches[2].replace(/""/g, '"') : matches[3]
409
+ )
410
+ }
411
+ console.log('csv arr result', result, result.length);
412
+ let csvResult = arrayToObject(result)
413
+ return csvResult
414
+ }
415
+ function arrayToObject (csvArray:any) {
416
+
417
+ //Take the first line (headers) from the array and remove it from the array.
418
+ const headers = csvArray.shift()
419
+
420
+ // Iterate through the rows and reduce each column to an object
421
+
422
+ return csvArray.filter((row:any)=>{
423
+ console.log('csv object keys lngth', Object.keys(row).length)
424
+ return Object.keys(row).length > 1
425
+ }).map((row: { [x: string]: any; } ) =>{
426
+ return headers.reduce((acc: any, currentHeader: any, i: string | number) => ((currentHeader.indexOf('cols_') >= 0 || currentHeader == '') ? acc : ((row[i] != '' && row[i] != null) ? { ...acc, ...{ [currentHeader]: JSON.parse(row[i] == "TRUE" ? "true" : row[i])}} : acc)), {})
427
+ })
428
+ }
429
+
430
+ function titleCase(str: string) {
431
+ var splitStr = str.toLowerCase().split(' ');
432
+ for (var i = 0; i < splitStr.length; i++) {
433
+ // You do not need to check if i is larger than splitStr length, as your for does that for you
434
+ // Assign it back to the array
435
+ splitStr[i] = splitStr[i].charAt(0).toUpperCase() + splitStr[i].substring(1);
436
+ }
437
+ // Directly return the joined string
438
+ return splitStr.join(' ');
439
+ }
440
+
441
+ function alphabeticalSort(arr:string[]){
442
+ let arrSorted = arr.sort((a,b) => {
443
+ return a.trim().toLowerCase().localeCompare(b.trim().toLowerCase())
444
+ })
445
+ return arrSorted
446
+ }
447
+
448
+ function percentageString(val: number, valTotal: number){
449
+ if(valTotal == 0){
450
+ return ""
451
+ }
452
+ let num = (100 * val) / valTotal
453
+ return " (" + (Math.round((num + Number.EPSILON) * 100) / 100) + "%)"
454
+ }
455
+
456
+ const exportFunctions = {
457
+ callApiPresignedDelete, callApiPresignedGet, callApiPresigned, jsonObjectToHtml, clearListeners, isInteger, callApi, validateName, readCookie, timeSince, createDiagonalPattern1, createDiagonalPattern2, createDiagonalPattern3, getRandomColor, convertToCSV, parseCsv, titleCase, alphabeticalSort, percentageString
458
+ };
459
+
460
460
  export default exportFunctions;