sf-i-events 1.0.873 → 1.0.874

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,520 +1,520 @@
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
- let fieldsArr = Object.keys(arr[0]);
386
- return array.map((it, index) => {
387
- let strIt = ""
388
- for(let [i,objkey] of Object.keys(it).entries()){
389
- if(index == 0){
390
- strIt += "\"" + it[objkey as any].replace(/"/g,"\"\"") + "\""
391
- }else{
392
- strIt += "\"" + JSON.stringify(it[fieldsArr[i] as any] ?? "").replace(/"/g,"\"\"") + "\""
393
- }
394
- if(i < (Object.keys(it).length - 1)){
395
- strIt += ","
396
- }
397
- }
398
- return strIt
399
- }).join('\n')
400
- }
401
-
402
- function parseCsv(csv:string){
403
- const re = /(,|\r?\n|\r|^)(?:"([^"]*(?:""[^"]*)*)"|([^,\r\n]*))/gi
404
- const result:any = [[]]
405
- let matches:any;
406
- while ((matches = re.exec(csv))) {
407
- if (matches[1].length && matches[1] !== ',') result.push([])
408
- result[result.length - 1].push(
409
- matches[2] !== undefined ? matches[2].replace(/""/g, '"') : matches[3]
410
- )
411
- }
412
- console.log('csv arr result', result, result.length);
413
- let csvResult = arrayToObject(result)
414
- return csvResult
415
- }
416
- function arrayToObject (csvArray:any) {
417
-
418
- //Take the first line (headers) from the array and remove it from the array.
419
- const headers = csvArray.shift()
420
-
421
- // Iterate through the rows and reduce each column to an object
422
-
423
- return csvArray.filter((row:any)=>{
424
- console.log('csv object keys lngth', Object.keys(row).length)
425
- return Object.keys(row).length > 1
426
- }).map((row: { [x: string]: any; }, index: number ) =>{
427
-
428
- return headers.reduce((acc: any, currentHeader: any, i: string | number) => {
429
- console.log('parsing row', row[i], index);
430
- return((currentHeader.indexOf('cols_') >= 0 || currentHeader == '') ? acc : ((row[i] != '' && row[i] != null) ? { ...acc, ...{ [currentHeader]: JSON.parse(row[i] == "TRUE" ? "true" : row[i])}} : acc))
431
- }, {})
432
- })
433
- }
434
-
435
- function titleCase(str: string) {
436
- var splitStr = str.toLowerCase().split(' ');
437
- for (var i = 0; i < splitStr.length; i++) {
438
- // You do not need to check if i is larger than splitStr length, as your for does that for you
439
- // Assign it back to the array
440
- splitStr[i] = splitStr[i].charAt(0).toUpperCase() + splitStr[i].substring(1);
441
- }
442
- // Directly return the joined string
443
- return splitStr.join(' ');
444
- }
445
-
446
- function alphabeticalSort(arr:string[]){
447
- let arrSorted = arr.sort((a,b) => {
448
- return a.trim().toLowerCase().localeCompare(b.trim().toLowerCase())
449
- })
450
- return arrSorted
451
- }
452
-
453
- function percentageString(val: number, valTotal: number){
454
- if(valTotal == 0){
455
- return ""
456
- }
457
- let num = (100 * val) / valTotal
458
- return " (" + (Math.round((num + Number.EPSILON) * 100) / 100) + "%)"
459
- }
460
-
461
- function getCurrentFiscal(){
462
- let date = new Date()
463
- if(date.getMonth() < 4){
464
- return (date.getFullYear() - 1)
465
- }
466
- return date.getFullYear();
467
- }
468
-
469
- function getDateTimeStrings(unixTimestamp: number): string {
470
- // Convert to milliseconds if the timestamp is in seconds
471
- if (unixTimestamp.toString().length === 10) {
472
- unixTimestamp *= 1000;
473
- }
474
-
475
- const date = new Date(unixTimestamp);
476
-
477
- const options: Intl.DateTimeFormatOptions = {
478
- year: 'numeric',
479
- month: '2-digit',
480
- day: '2-digit',
481
- hour: '2-digit',
482
- minute: '2-digit',
483
- second: '2-digit',
484
- hour12: false,
485
- };
486
-
487
- const estFormatter = new Intl.DateTimeFormat('en-US', {
488
- ...options,
489
- timeZone: 'America/New_York',
490
- });
491
-
492
- const istFormatter = new Intl.DateTimeFormat('en-IN', {
493
- ...options,
494
- timeZone: 'Asia/Kolkata',
495
- });
496
-
497
- const [estDate, estTime] = estFormatter.format(date).split(', ');
498
- const [istDate, istTime] = istFormatter.format(date).split(', ');
499
- return `${estDate} ${estTime} EST, ${istDate} ${istTime} IST`;
500
- }
501
-
502
- function getUsermap() {
503
- let userinfo = JSON.parse(localStorage.getItem('userinfo') ?? "{}");
504
- let userMapHome = JSON.parse(JSON.parse((userinfo.user.usermap.S).replace(/_QUOTES_/g, "\\\"")));
505
- return userMapHome;
506
- }
507
-
508
- function setFeatures(features:any){
509
- localStorage.setItem('features', JSON.stringify(features))
510
- }
511
- function getFeatures(){
512
- let features = JSON.parse(localStorage.getItem('features') ?? '["compliances"]')
513
- return features
514
- }
515
-
516
- const exportFunctions = {
517
- callApiPresignedDelete, callApiPresignedGet, callApiPresigned, jsonObjectToHtml, clearListeners, isInteger, callApi, validateName, readCookie, timeSince, createDiagonalPattern1, createDiagonalPattern2, createDiagonalPattern3, getRandomColor, convertToCSV, parseCsv, titleCase, alphabeticalSort, percentageString, getCurrentFiscal, getDateTimeStrings, getUsermap, setFeatures, getFeatures
518
- };
519
-
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
+ let fieldsArr = Object.keys(arr[0]);
386
+ return array.map((it, index) => {
387
+ let strIt = ""
388
+ for(let [i,objkey] of Object.keys(it).entries()){
389
+ if(index == 0){
390
+ strIt += "\"" + it[objkey as any].replace(/"/g,"\"\"") + "\""
391
+ }else{
392
+ strIt += "\"" + JSON.stringify(it[fieldsArr[i] as any] ?? "").replace(/"/g,"\"\"") + "\""
393
+ }
394
+ if(i < (Object.keys(it).length - 1)){
395
+ strIt += ","
396
+ }
397
+ }
398
+ return strIt
399
+ }).join('\n')
400
+ }
401
+
402
+ function parseCsv(csv:string){
403
+ const re = /(,|\r?\n|\r|^)(?:"([^"]*(?:""[^"]*)*)"|([^,\r\n]*))/gi
404
+ const result:any = [[]]
405
+ let matches:any;
406
+ while ((matches = re.exec(csv))) {
407
+ if (matches[1].length && matches[1] !== ',') result.push([])
408
+ result[result.length - 1].push(
409
+ matches[2] !== undefined ? matches[2].replace(/""/g, '"') : matches[3]
410
+ )
411
+ }
412
+ console.log('csv arr result', result, result.length);
413
+ let csvResult = arrayToObject(result)
414
+ return csvResult
415
+ }
416
+ function arrayToObject (csvArray:any) {
417
+
418
+ //Take the first line (headers) from the array and remove it from the array.
419
+ const headers = csvArray.shift()
420
+
421
+ // Iterate through the rows and reduce each column to an object
422
+
423
+ return csvArray.filter((row:any)=>{
424
+ console.log('csv object keys lngth', Object.keys(row).length)
425
+ return Object.keys(row).length > 1
426
+ }).map((row: { [x: string]: any; }, index: number ) =>{
427
+
428
+ return headers.reduce((acc: any, currentHeader: any, i: string | number) => {
429
+ console.log('parsing row', row[i], index);
430
+ return((currentHeader.indexOf('cols_') >= 0 || currentHeader == '') ? acc : ((row[i] != '' && row[i] != null) ? { ...acc, ...{ [currentHeader]: JSON.parse(row[i] == "TRUE" ? "true" : row[i])}} : acc))
431
+ }, {})
432
+ })
433
+ }
434
+
435
+ function titleCase(str: string) {
436
+ var splitStr = str.toLowerCase().split(' ');
437
+ for (var i = 0; i < splitStr.length; i++) {
438
+ // You do not need to check if i is larger than splitStr length, as your for does that for you
439
+ // Assign it back to the array
440
+ splitStr[i] = splitStr[i].charAt(0).toUpperCase() + splitStr[i].substring(1);
441
+ }
442
+ // Directly return the joined string
443
+ return splitStr.join(' ');
444
+ }
445
+
446
+ function alphabeticalSort(arr:string[]){
447
+ let arrSorted = arr.sort((a,b) => {
448
+ return a.trim().toLowerCase().localeCompare(b.trim().toLowerCase())
449
+ })
450
+ return arrSorted
451
+ }
452
+
453
+ function percentageString(val: number, valTotal: number){
454
+ if(valTotal == 0){
455
+ return ""
456
+ }
457
+ let num = (100 * val) / valTotal
458
+ return " (" + (Math.round((num + Number.EPSILON) * 100) / 100) + "%)"
459
+ }
460
+
461
+ function getCurrentFiscal(){
462
+ let date = new Date()
463
+ if(date.getMonth() < 4){
464
+ return (date.getFullYear() - 1)
465
+ }
466
+ return date.getFullYear();
467
+ }
468
+
469
+ function getDateTimeStrings(unixTimestamp: number): string {
470
+ // Convert to milliseconds if the timestamp is in seconds
471
+ if (unixTimestamp.toString().length === 10) {
472
+ unixTimestamp *= 1000;
473
+ }
474
+
475
+ const date = new Date(unixTimestamp);
476
+
477
+ const options: Intl.DateTimeFormatOptions = {
478
+ year: 'numeric',
479
+ month: '2-digit',
480
+ day: '2-digit',
481
+ hour: '2-digit',
482
+ minute: '2-digit',
483
+ second: '2-digit',
484
+ hour12: false,
485
+ };
486
+
487
+ const estFormatter = new Intl.DateTimeFormat('en-US', {
488
+ ...options,
489
+ timeZone: 'America/New_York',
490
+ });
491
+
492
+ const istFormatter = new Intl.DateTimeFormat('en-IN', {
493
+ ...options,
494
+ timeZone: 'Asia/Kolkata',
495
+ });
496
+
497
+ const [estDate, estTime] = estFormatter.format(date).split(', ');
498
+ const [istDate, istTime] = istFormatter.format(date).split(', ');
499
+ return `${estDate} ${estTime} EST, ${istDate} ${istTime} IST`;
500
+ }
501
+
502
+ function getUsermap() {
503
+ let userinfo = JSON.parse(localStorage.getItem('userinfo') ?? "{}");
504
+ let userMapHome = JSON.parse(JSON.parse((userinfo.user.usermap.S).replace(/_QUOTES_/g, "\\\"")));
505
+ return userMapHome;
506
+ }
507
+
508
+ function setFeatures(features:any){
509
+ localStorage.setItem('features', JSON.stringify(features))
510
+ }
511
+ function getFeatures(){
512
+ let features = JSON.parse(localStorage.getItem('features') ?? '["compliances"]')
513
+ return features
514
+ }
515
+
516
+ const exportFunctions = {
517
+ callApiPresignedDelete, callApiPresignedGet, callApiPresigned, jsonObjectToHtml, clearListeners, isInteger, callApi, validateName, readCookie, timeSince, createDiagonalPattern1, createDiagonalPattern2, createDiagonalPattern3, getRandomColor, convertToCSV, parseCsv, titleCase, alphabeticalSort, percentageString, getCurrentFiscal, getDateTimeStrings, getUsermap, setFeatures, getFeatures
518
+ };
519
+
520
520
  export default exportFunctions;