@accitdg/web-helpers 0.0.1 → 0.0.2
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/dist/index.esm.ts +44 -1
- package/dist/index.ts +44 -1
- package/package.json +1 -1
package/dist/index.esm.ts
CHANGED
|
@@ -5,9 +5,52 @@ const convertNumberToCurrency = numberValue => {
|
|
|
5
5
|
});
|
|
6
6
|
return formattedValue;
|
|
7
7
|
};
|
|
8
|
+
const restoreBase64 = (cleanedBase64, imageType = "png") => `data:image/${imageType};base64,${cleanedBase64}`;
|
|
9
|
+
const cleanBase64 = base64 => {
|
|
10
|
+
return base64.replace(/^data:[a-zA-Z0-9\\/\\+\\-]+;base64,/, "").replace(/^data:(application\/pdf|application\/vnd.openxmlformats-officedocument.wordprocessingml.document|application\/vnd.openxmlformats-officedocument.spreadsheetml.sheet|image\/[a-z]+);base64,/, "").replace("data:application/vnd.openxmlformats-officedocument.wordprocessingml.template;base64,", "");
|
|
11
|
+
};
|
|
12
|
+
const downloadBase64File = (base64Data, filename) => {
|
|
13
|
+
const byteCharacters = atob(restoreBase64(cleanBase64(base64Data)).split(",")[1]);
|
|
14
|
+
const byteArray = new Uint8Array(byteCharacters.length);
|
|
15
|
+
for (let i = 0; i < byteCharacters.length; i++) {
|
|
16
|
+
byteArray[i] = byteCharacters.charCodeAt(i);
|
|
17
|
+
}
|
|
18
|
+
const blob = new Blob([byteArray], {
|
|
19
|
+
type: "application/octet-stream"
|
|
20
|
+
});
|
|
21
|
+
const link = document.createElement("a");
|
|
22
|
+
link.href = URL.createObjectURL(blob);
|
|
23
|
+
link.download = filename;
|
|
24
|
+
document.body.appendChild(link);
|
|
25
|
+
link.click();
|
|
26
|
+
document.body.removeChild(link);
|
|
27
|
+
};
|
|
28
|
+
const arrayBufferToBase64 = buffer => {
|
|
29
|
+
let binary = "";
|
|
30
|
+
const bytes = new Uint8Array(buffer);
|
|
31
|
+
const len = bytes.byteLength;
|
|
32
|
+
for (let i = 0; i < len; i++) {
|
|
33
|
+
binary += String.fromCharCode(bytes[i]);
|
|
34
|
+
}
|
|
35
|
+
return window.btoa(binary);
|
|
36
|
+
};
|
|
37
|
+
const getFileExtensionFromBase64 = base64String => {
|
|
38
|
+
try {
|
|
39
|
+
const contentType = base64String.split(";")[0].split(":")[1];
|
|
40
|
+
const extension = contentType.split("/")[1];
|
|
41
|
+
return extension;
|
|
42
|
+
} catch (error) {
|
|
43
|
+
return null;
|
|
44
|
+
}
|
|
45
|
+
};
|
|
8
46
|
|
|
9
47
|
const webHelpers = {
|
|
10
|
-
convertNumberToCurrency
|
|
48
|
+
convertNumberToCurrency,
|
|
49
|
+
arrayBufferToBase64,
|
|
50
|
+
cleanBase64,
|
|
51
|
+
downloadBase64File,
|
|
52
|
+
restoreBase64,
|
|
53
|
+
getFileExtensionFromBase64
|
|
11
54
|
};
|
|
12
55
|
|
|
13
56
|
export { webHelpers as default, webHelpers };
|
package/dist/index.ts
CHANGED
|
@@ -9,9 +9,52 @@ const convertNumberToCurrency = numberValue => {
|
|
|
9
9
|
});
|
|
10
10
|
return formattedValue;
|
|
11
11
|
};
|
|
12
|
+
const restoreBase64 = (cleanedBase64, imageType = "png") => `data:image/${imageType};base64,${cleanedBase64}`;
|
|
13
|
+
const cleanBase64 = base64 => {
|
|
14
|
+
return base64.replace(/^data:[a-zA-Z0-9\\/\\+\\-]+;base64,/, "").replace(/^data:(application\/pdf|application\/vnd.openxmlformats-officedocument.wordprocessingml.document|application\/vnd.openxmlformats-officedocument.spreadsheetml.sheet|image\/[a-z]+);base64,/, "").replace("data:application/vnd.openxmlformats-officedocument.wordprocessingml.template;base64,", "");
|
|
15
|
+
};
|
|
16
|
+
const downloadBase64File = (base64Data, filename) => {
|
|
17
|
+
const byteCharacters = atob(restoreBase64(cleanBase64(base64Data)).split(",")[1]);
|
|
18
|
+
const byteArray = new Uint8Array(byteCharacters.length);
|
|
19
|
+
for (let i = 0; i < byteCharacters.length; i++) {
|
|
20
|
+
byteArray[i] = byteCharacters.charCodeAt(i);
|
|
21
|
+
}
|
|
22
|
+
const blob = new Blob([byteArray], {
|
|
23
|
+
type: "application/octet-stream"
|
|
24
|
+
});
|
|
25
|
+
const link = document.createElement("a");
|
|
26
|
+
link.href = URL.createObjectURL(blob);
|
|
27
|
+
link.download = filename;
|
|
28
|
+
document.body.appendChild(link);
|
|
29
|
+
link.click();
|
|
30
|
+
document.body.removeChild(link);
|
|
31
|
+
};
|
|
32
|
+
const arrayBufferToBase64 = buffer => {
|
|
33
|
+
let binary = "";
|
|
34
|
+
const bytes = new Uint8Array(buffer);
|
|
35
|
+
const len = bytes.byteLength;
|
|
36
|
+
for (let i = 0; i < len; i++) {
|
|
37
|
+
binary += String.fromCharCode(bytes[i]);
|
|
38
|
+
}
|
|
39
|
+
return window.btoa(binary);
|
|
40
|
+
};
|
|
41
|
+
const getFileExtensionFromBase64 = base64String => {
|
|
42
|
+
try {
|
|
43
|
+
const contentType = base64String.split(";")[0].split(":")[1];
|
|
44
|
+
const extension = contentType.split("/")[1];
|
|
45
|
+
return extension;
|
|
46
|
+
} catch (error) {
|
|
47
|
+
return null;
|
|
48
|
+
}
|
|
49
|
+
};
|
|
12
50
|
|
|
13
51
|
const webHelpers = {
|
|
14
|
-
convertNumberToCurrency
|
|
52
|
+
convertNumberToCurrency,
|
|
53
|
+
arrayBufferToBase64,
|
|
54
|
+
cleanBase64,
|
|
55
|
+
downloadBase64File,
|
|
56
|
+
restoreBase64,
|
|
57
|
+
getFileExtensionFromBase64
|
|
15
58
|
};
|
|
16
59
|
|
|
17
60
|
exports["default"] = webHelpers;
|