sf-i-events 1.0.909 → 1.0.911

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,271 +1,272 @@
1
+ import * as XLSX from 'xlsx';
1
2
 
2
3
  const validateName = (name: string) => {
3
- if((name + "").length > 2) {
4
- return true;
5
- }
6
- return false;
4
+ if ((name + "").length > 2) {
5
+ return true;
7
6
  }
7
+ return false;
8
+ }
8
9
 
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
- }
10
+ function createDiagonalPattern1(color: string) {
11
+ // create a 10x10 px canvas for the pattern's base shape
12
+ let shape = document.createElement('canvas')
13
+ shape.width = 10
14
+ shape.height = 10
15
+ // get the context for drawing
16
+ let c = shape.getContext('2d')
17
+ // draw 1st line of the shape
18
+ c!.strokeStyle = color
19
+ c!.lineWidth = 3
20
+ c!.beginPath()
21
+ c!.moveTo(2, 0)
22
+ c!.lineTo(10, 8)
23
+ c!.stroke()
24
+ // draw 2nd line of the shape
25
+ c!.beginPath()
26
+ c!.moveTo(2, 10)
27
+ c!.lineTo(0, 8)
28
+ c!.stroke()
29
+ // create the pattern from the shape
30
+ return c!.createPattern(shape, 'repeat')
31
+ }
31
32
 
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
- }
33
+ // function createDiagonalPattern2(color: string) {
34
+ // let shape = document.createElement('canvas')
35
+ // shape.width = 10
36
+ // shape.height = 10
37
+ // // get the context for drawing
38
+ // let c = shape.getContext('2d')
39
+ // // draw 1st line of the shape
40
+ // c!.beginPath();
41
+ // c!.rect(2, 2, 8, 8)
42
+ // c!.fillStyle = color;
43
+ // c!.fill();
44
+ // // create the pattern from the shape
45
+ // return c!.createPattern(shape, 'repeat')
46
+ // }
47
+
48
+ function createDiagonalPattern2(color: string) {
49
+ // create a 10x10 px canvas for the pattern's base shape
50
+ let shape = document.createElement('canvas')
51
+ shape.width = 10
52
+ shape.height = 10
53
+ // get the context for drawing
54
+ let c = shape.getContext('2d')
55
+ // draw 1st line of the shape
56
+ c!.beginPath();
57
+ c!.rect(1, 1, 9, 9)
58
+ c!.fillStyle = color;
59
+ c!.fill();
60
+ c!.lineWidth = 1;
61
+ c!.strokeStyle = color;
62
+ c!.stroke();
63
+ // create the pattern from the shape
64
+
65
+ // create tick
66
+
67
+ //draw tick
68
+ c!.beginPath();
69
+ c!.moveTo(2, 2);
70
+ c!.lineTo(8, 8);
71
+ c!.moveTo(2, 8);
72
+ c!.lineTo(8, 2);
73
+ c!.lineWidth = 2;
74
+ c!.strokeStyle = '#fff';
75
+ c!.stroke();
76
+
77
+ return c!.createPattern(shape, 'repeat')
78
+ }
78
79
 
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
- }
80
+ function createDiagonalPattern3(color: string) {
81
+ // create a 10x10 px canvas for the pattern's base shape
82
+ let shape = document.createElement('canvas')
83
+ shape.width = 10
84
+ shape.height = 10
85
+ // get the context for drawing
86
+ let c = shape.getContext('2d')
87
+ // draw 1st line of the shape
88
+ c!.beginPath();
89
+ c!.rect(1, 1, 9, 9)
90
+ c!.fillStyle = color;
91
+ c!.fill();
92
+ c!.lineWidth = 1;
93
+ c!.strokeStyle = color;
94
+ c!.stroke();
95
+ // create the pattern from the shape
96
+
97
+ // create tick
98
+
99
+ //draw tick
100
+ c!.beginPath();
101
+ c!.moveTo(2, 5);
102
+ c!.lineTo(4, 7);
103
+ c!.lineTo(8, 2);
104
+ c!.lineWidth = 2;
105
+ c!.strokeStyle = '#fff';
106
+ c!.stroke();
107
+
108
+ return c!.createPattern(shape, 'repeat')
109
+ }
109
110
 
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";
111
+ // function createDiagonalPattern3(color: string) {
112
+ // // create a 10x10 px canvas for the pattern's base shape
113
+ // let shape = document.createElement('canvas')
114
+ // shape.width = 14
115
+ // shape.height = 14
116
+ // // get the context for drawing
117
+ // let c = shape.getContext('2d')
118
+ // // draw 1st line of the shape
119
+ // c!.beginPath();
120
+ // c!.arc(7, 7, 6, 0, 2 * Math.PI, false);
121
+ // c!.fillStyle = color;
122
+ // c!.fill();
123
+ // c!.lineWidth = 1;
124
+ // c!.strokeStyle = color;
125
+ // c!.stroke();
126
+ // // create the pattern from the shape
127
+
128
+ // // create tick
129
+
130
+ // //draw tick
131
+ // c!.beginPath();
132
+ // c!.moveTo(4,6);
133
+ // c!.lineTo(6,9);
134
+ // c!.lineTo(10,4);
135
+ // c!.lineWidth = 2;
136
+ // c!.strokeStyle = '#fff';
137
+ // c!.stroke();
138
+
139
+ // return c!.createPattern(shape, 'repeat')
140
+ // }
141
+
142
+ // function createDiagonalPattern3(color: string) {
143
+ // // create a 10x10 px canvas for the pattern's base shape
144
+ // let shape = document.createElement('canvas')
145
+ // shape.width = 10
146
+ // shape.height = 10
147
+ // // get the context for drawing
148
+ // let c = shape.getContext('2d')
149
+ // // draw 1st line of the shape
150
+ // c!.beginPath();
151
+ // c!.arc(5, 5, 4, 0, 2 * Math.PI, false);
152
+ // c!.fillStyle = color;
153
+ // c!.fill();
154
+ // c!.lineWidth = 1;
155
+ // c!.strokeStyle = color;
156
+ // c!.stroke();
157
+ // // create the pattern from the shape
158
+ // return c!.createPattern(shape, 'repeat')
159
+ // }
160
+
161
+ const timeSince = (date: number) => {
162
+
163
+ var seconds = Math.floor((new Date().getTime() - date) / 1000);
164
+
165
+ if (seconds > 0) {
166
+
167
+ var interval = seconds / 31536000;
168
+
169
+ if (interval > 1) {
170
+ return Math.floor(interval) + " years ago";
171
+ }
172
+ interval = seconds / 2592000;
173
+ if (interval > 1) {
174
+ return Math.floor(interval) + " months ago";
175
+ }
176
+ interval = seconds / 86400;
177
+ if (interval > 1) {
178
+ return Math.floor(interval) + " days ago";
179
+ }
180
+ interval = seconds / 3600;
181
+ if (interval > 1) {
182
+ return Math.floor(interval) + " hours ago";
183
+ }
184
+ interval = seconds / 60;
185
+ if (interval > 1) {
186
+ return Math.floor(interval) + " minutes ago";
187
+ }
188
+ return Math.floor(seconds) + " seconds ago";
188
189
 
189
- } else {
190
+ } else {
190
191
 
191
- var interval = Math.abs(seconds) / 31536000;
192
+ var interval = Math.abs(seconds) / 31536000;
192
193
 
193
194
 
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
- }
195
+ console.log('timesince', seconds);
206
196
 
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
197
 
198
+ console.log('interval year', interval);
199
+ if (interval > 1) {
200
+ return Math.floor(interval) + " years later";
224
201
  }
202
+ interval = Math.abs(seconds) / 2592000;
203
+ console.log('interval months', interval);
204
+ if (interval > 1) {
205
+ return Math.floor(interval) + " months later";
206
+ }
207
+
208
+ interval = Math.abs(seconds) / 86400;
209
+ console.log('interval days', interval);
210
+ if (interval > 1) {
211
+ return Math.floor(interval) + " days later";
212
+ }
213
+
214
+ interval = Math.abs(seconds) / 3600;
215
+ console.log('interval hours', interval);
216
+ if (interval > 1) {
217
+ return Math.floor(interval) + " hours later";
218
+ }
219
+ interval = Math.abs(seconds) / 60;
220
+ if (interval > 1) {
221
+ return Math.floor(interval) + " minutes later";
222
+ }
223
+ return Math.floor(Math.abs(seconds)) + " seconds";
224
+
225
225
  }
226
+ }
226
227
 
227
228
 
228
229
  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
- }
230
+ let name = key + "=";
231
+ let decodedCookie = decodeURIComponent(document.cookie);
232
+ let ca = decodedCookie.split(';');
233
+ for (let i = 0; i < ca.length; i++) {
234
+ let c = ca[i];
235
+ while (c.charAt(0) == ' ') {
236
+ c = c.substring(1);
237
+ }
238
+ if (c.indexOf(name) == 0) {
239
+ return c.substring(name.length, c.length);
240
240
  }
241
- return "";
241
+ }
242
+ return "";
242
243
  }
243
244
 
244
245
  async function callApi(url: string, data: string, authorization: any) {
245
246
 
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);
247
+ return new Promise((resolve: any) => {
248
+
249
+ const jsonData = JSON.stringify(data);
250
+ var xhr = new XMLHttpRequest();
251
+ xhr.addEventListener("readystatechange", () => {
252
+ if (xhr != null) {
253
+ if (xhr.readyState === 4) {
254
+ resolve(xhr);
263
255
  }
264
- xhr.send(jsonData);
256
+ }
257
+ });
258
+ xhr.open("POST", url);
259
+ xhr.timeout = 1800000;
260
+ xhr.setRequestHeader("Content-Type", "application/json");
261
+ xhr.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
262
+ if (authorization != null) {
263
+ xhr.setRequestHeader('Authorization', 'Basic ' + authorization);
264
+ }
265
+ xhr.send(jsonData);
265
266
 
266
- return xhr;
267
+ return xhr;
267
268
 
268
- })
269
+ })
269
270
 
270
271
  }
271
272
 
@@ -274,21 +275,21 @@ async function callApiPresignedDelete(url: string) {
274
275
 
275
276
  return new Promise((resolve: any) => {
276
277
 
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;
278
+ var xhr = new XMLHttpRequest();
279
+ xhr.addEventListener("readystatechange", () => {
280
+ if (xhr != null) {
281
+ if (xhr.readyState === 4) {
282
+ resolve(xhr);
283
+ }
284
+ }
285
+ });
286
+ xhr.open("DELETE", url);
287
+ xhr.timeout = 1800000;
288
+ xhr.setRequestHeader("Content-Type", "application/json");
289
+ xhr.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
290
+ xhr.send(null);
291
+
292
+ return xhr;
292
293
 
293
294
  })
294
295
 
@@ -298,21 +299,21 @@ async function callApiPresignedGet(url: string) {
298
299
 
299
300
  return new Promise((resolve: any) => {
300
301
 
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;
302
+ var xhr = new XMLHttpRequest();
303
+ xhr.addEventListener("readystatechange", () => {
304
+ if (xhr != null) {
305
+ if (xhr.readyState === 4) {
306
+ resolve(xhr);
307
+ }
308
+ }
309
+ });
310
+ xhr.open("GET", url);
311
+ xhr.timeout = 1800000;
312
+ xhr.setRequestHeader("Content-Type", "application/json");
313
+ xhr.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
314
+ xhr.send(null);
315
+
316
+ return xhr;
316
317
 
317
318
  })
318
319
 
@@ -322,22 +323,22 @@ async function callApiPresigned(url: string, data: string) {
322
323
 
323
324
  return new Promise((resolve: any) => {
324
325
 
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;
326
+ const jsonData = JSON.stringify(data);
327
+ var xhr = new XMLHttpRequest();
328
+ xhr.addEventListener("readystatechange", () => {
329
+ if (xhr != null) {
330
+ if (xhr.readyState === 4) {
331
+ resolve(xhr);
332
+ }
333
+ }
334
+ });
335
+ xhr.open("PUT", url);
336
+ xhr.timeout = 1800000;
337
+ xhr.setRequestHeader("Content-Type", "application/json");
338
+ xhr.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
339
+ xhr.send(jsonData);
340
+
341
+ return xhr;
341
342
 
342
343
  })
343
344
 
@@ -366,32 +367,32 @@ function jsonObjectToHtml(json: any) {
366
367
 
367
368
  var html = '';
368
369
 
369
-
370
- for(var i = 0; i < Object.keys(json).length; i++) {
370
+
371
+ for (var i = 0; i < Object.keys(json).length; i++) {
371
372
  var key = Object.keys(json)[i];
372
373
  html += '<div>';
373
374
  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 += ('<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
376
  html += '</div>';
376
377
  }
377
-
378
+
378
379
 
379
380
  return html;
380
381
 
381
382
  }
382
383
 
383
- function convertToCSV(arr:any[]) {
384
+ function convertToCSV(arr: any[]) {
384
385
  const array = [Object.keys(arr[0])].concat(arr)
385
386
  let fieldsArr = Object.keys(arr[0]);
386
387
  return array.map((it, index) => {
387
388
  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,"\"\"") + "\""
389
+ for (let [i, objkey] of Object.keys(it).entries()) {
390
+ if (index == 0) {
391
+ strIt += "\"" + it[objkey as any].replace(/"/g, "\"\"") + "\""
392
+ } else {
393
+ strIt += "\"" + JSON.stringify(it[fieldsArr[i] as any] ?? "").replace(/"/g, "\"\"") + "\""
393
394
  }
394
- if(i < (Object.keys(it).length - 1)){
395
+ if (i < (Object.keys(it).length - 1)) {
395
396
  strIt += ","
396
397
  }
397
398
  }
@@ -399,10 +400,10 @@ function convertToCSV(arr:any[]) {
399
400
  }).join('\n')
400
401
  }
401
402
 
402
- function parseCsv(csv:string){
403
+ function parseCsv(csv: string) {
403
404
  const re = /(,|\r?\n|\r|^)(?:"([^"]*(?:""[^"]*)*)"|([^,\r\n]*))/gi
404
- const result:any = [[]]
405
- let matches:any;
405
+ const result: any = [[]]
406
+ let matches: any;
406
407
  while ((matches = re.exec(csv))) {
407
408
  if (matches[1].length && matches[1] !== ',') result.push([])
408
409
  result[result.length - 1].push(
@@ -413,55 +414,55 @@ function parseCsv(csv:string){
413
414
  let csvResult = arrayToObject(result)
414
415
  return csvResult
415
416
  }
416
- function arrayToObject (csvArray:any) {
417
+ function arrayToObject(csvArray: any) {
417
418
 
418
419
  //Take the first line (headers) from the array and remove it from the array.
419
- const headers = csvArray.shift()
420
+ const headers = csvArray.shift()
420
421
 
421
422
  // Iterate through the rows and reduce each column to an object
422
-
423
- return csvArray.filter((row:any)=>{
423
+
424
+ return csvArray.filter((row: any) => {
424
425
  console.log('csv object keys lngth', Object.keys(row).length)
425
426
  return Object.keys(row).length > 1
426
- }).map((row: { [x: string]: any; } ) =>{
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
- }, {})
427
+ }).map((row: { [x: string]: any; }) => {
428
+
429
+ return headers.reduce((acc: any, currentHeader: any, i: string | number) => {
430
+ // console.log('parsing row', row[i], index);
431
+ return ((currentHeader.indexOf('cols_') >= 0 || currentHeader == '') ? acc : ((row[i] != '' && row[i] != null) ? { ...acc, ...{ [currentHeader]: JSON.parse(row[i] == "TRUE" ? "true" : row[i]) } } : acc))
432
+ }, {})
432
433
  })
433
434
  }
434
435
 
435
436
  function titleCase(str: string) {
436
437
  var splitStr = str.toLowerCase().split(' ');
437
438
  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);
439
+ // You do not need to check if i is larger than splitStr length, as your for does that for you
440
+ // Assign it back to the array
441
+ splitStr[i] = splitStr[i].charAt(0).toUpperCase() + splitStr[i].substring(1);
441
442
  }
442
443
  // Directly return the joined string
443
- return splitStr.join(' ');
444
+ return splitStr.join(' ');
444
445
  }
445
446
 
446
- function alphabeticalSort(arr:string[]){
447
- let arrSorted = arr.sort((a,b) => {
447
+ function alphabeticalSort(arr: string[]) {
448
+ let arrSorted = arr.sort((a, b) => {
448
449
  return a.trim().toLowerCase().localeCompare(b.trim().toLowerCase())
449
450
  })
450
451
  return arrSorted
451
452
  }
452
453
 
453
- function percentageString(val: number, valTotal: number){
454
- if(valTotal == 0){
454
+ function percentageString(val: number, valTotal: number) {
455
+ if (valTotal == 0) {
455
456
  return ""
456
457
  }
457
458
  let num = (100 * val) / valTotal
458
459
  return " (" + (Math.round((num + Number.EPSILON) * 100) / 100) + "%)"
459
460
  }
460
461
 
461
- function getCurrentFiscal(){
462
+ function getCurrentFiscal() {
462
463
  let date = new Date()
463
- if(date.getMonth() < 4){
464
- return (date.getFullYear() - 1)
464
+ if (date.getMonth() < 4) {
465
+ return (date.getFullYear() - 1)
465
466
  }
466
467
  return date.getFullYear();
467
468
  }
@@ -510,16 +511,61 @@ function getProjectUsermap() {
510
511
  return projectUsermap;
511
512
  }
512
513
 
513
- function setFeatures(features:any){
514
+ function setFeatures(features: any) {
514
515
  localStorage.setItem('features', JSON.stringify(features))
515
516
  }
516
- function getFeatures(){
517
+ function getFeatures() {
517
518
  let features = JSON.parse(localStorage.getItem('features') ?? '["compliances"]')
518
519
  return features
519
520
  }
520
521
 
522
+ export const downloadExcelFromCSV = (csvString: string, fileName: string = 'report.xlsx', title: string, projectname: string): void => {
523
+ const coverSheetData = [
524
+ [title],
525
+ [],
526
+ ['Project:', projectname],
527
+ ['Date:', getDateTimeStrings(new Date().getTime())],
528
+ ['Classification:', 'Confidential'],
529
+ ['Confidentiality Notice:', 'This document is intended only for the recipient(s) and may contain confidential information. Unauthorized use or disclosure is prohibited. If you are not the intended recipient, please notify the sender and delete this document.']
530
+ ];
531
+
532
+ // Create cover sheet
533
+ const coverSheet = XLSX.utils.aoa_to_sheet(coverSheetData);
534
+
535
+ // Create CSV data sheet
536
+ const csvSheet = XLSX.read(csvString, { type: 'string' }).Sheets.Sheet1;
537
+
538
+ // Create a workbook and append both sheets
539
+ const workbook: XLSX.WorkBook = XLSX.utils.book_new();
540
+ XLSX.utils.book_append_sheet(workbook, coverSheet, 'Doc');
541
+ XLSX.utils.book_append_sheet(workbook, csvSheet, title);
542
+
543
+ // Write workbook to binary
544
+ const wbout: string = XLSX.write(workbook, { bookType: 'xlsx', type: 'binary' });
545
+
546
+ const s2ab = (s: string): ArrayBuffer => {
547
+ const buf = new ArrayBuffer(s.length);
548
+ const view = new Uint8Array(buf);
549
+ for (let i = 0; i < s.length; i++) {
550
+ view[i] = s.charCodeAt(i) & 0xff;
551
+ }
552
+ return buf;
553
+ };
554
+
555
+ const blob = new Blob([s2ab(wbout)], {
556
+ type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
557
+ });
558
+
559
+ const link = document.createElement('a');
560
+ link.href = URL.createObjectURL(blob);
561
+ link.download = fileName;
562
+ document.body.appendChild(link);
563
+ link.click();
564
+ document.body.removeChild(link);
565
+ };
566
+
521
567
  const exportFunctions = {
522
- callApiPresignedDelete, callApiPresignedGet, callApiPresigned, jsonObjectToHtml, clearListeners, isInteger, callApi, validateName, readCookie, timeSince, createDiagonalPattern1, createDiagonalPattern2, createDiagonalPattern3, getRandomColor, convertToCSV, parseCsv, titleCase, alphabeticalSort, percentageString, getCurrentFiscal, getDateTimeStrings, getUsermap, setFeatures, getFeatures, getProjectUsermap
568
+ callApiPresignedDelete, callApiPresignedGet, callApiPresigned, jsonObjectToHtml, clearListeners, isInteger, callApi, validateName, readCookie, timeSince, createDiagonalPattern1, createDiagonalPattern2, createDiagonalPattern3, getRandomColor, convertToCSV, parseCsv, titleCase, alphabeticalSort, percentageString, getCurrentFiscal, getDateTimeStrings, getUsermap, setFeatures, getFeatures, getProjectUsermap, downloadExcelFromCSV
523
569
  };
524
570
 
525
571
  export default exportFunctions;