@accitdg/web-helpers 0.0.1 → 0.0.3

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/README.md CHANGED
@@ -1 +1,134 @@
1
- WEB HELPERS
1
+ # Web Helpers
2
+
3
+ A collection of utility functions for web development, including currency conversion, Base64 encoding/decoding, and file handling.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ npm install web-helpers
9
+ ```
10
+
11
+ ## Usage
12
+
13
+ ### Import the package
14
+
15
+ ```javascript
16
+ import webHelpers from 'web-helpers';
17
+ // or
18
+ import { convertNumberToCurrency, arrayBufferToBase64, /* ... */ } from 'web-helpers';
19
+ ```
20
+
21
+ ## Functions
22
+
23
+ ### `convertNumberToCurrency(numberValue: number): string`
24
+
25
+ Converts a number to Philippine Peso (PHP) currency format.
26
+
27
+ **Parameters:**
28
+ - `numberValue` (number): The number to convert
29
+
30
+ **Returns:** Formatted currency string
31
+
32
+ **Example:**
33
+ ```javascript
34
+ webHelpers.convertNumberToCurrency(1000);
35
+ // Output: "₱1,000.00"
36
+ ```
37
+
38
+ ---
39
+
40
+ ### `arrayBufferToBase64(buffer: ArrayBuffer): string`
41
+
42
+ Converts an ArrayBuffer to a Base64-encoded string.
43
+
44
+ **Parameters:**
45
+ - `buffer` (ArrayBuffer): The buffer to encode
46
+
47
+ **Returns:** Base64 encoded string
48
+
49
+ **Example:**
50
+ ```javascript
51
+ const buffer = new ArrayBuffer(8);
52
+ const base64 = webHelpers.arrayBufferToBase64(buffer);
53
+ ```
54
+
55
+ ---
56
+
57
+ ### `cleanBase64(base64: string): string`
58
+
59
+ Removes the data URL prefix from a Base64 string, leaving only the encoded data.
60
+
61
+ **Parameters:**
62
+ - `base64` (string): The Base64 string with or without prefix
63
+
64
+ **Returns:** Cleaned Base64 string without prefix
65
+
66
+ **Example:**
67
+ ```javascript
68
+ const cleaned = webHelpers.cleanBase64('data:image/png;base64,iVBORw0KG...');
69
+ // Output: "iVBORw0KG..."
70
+ ```
71
+
72
+ ---
73
+
74
+ ### `restoreBase64(cleanedBase64: string, imageType?: string): string`
75
+
76
+ Restores a clean Base64 string to a complete data URL format.
77
+
78
+ **Parameters:**
79
+ - `cleanedBase64` (string): The Base64 string without prefix
80
+ - `imageType` (string, optional): Image type (default: "png")
81
+
82
+ **Returns:** Complete data URL string
83
+
84
+ **Example:**
85
+ ```javascript
86
+ const dataUrl = webHelpers.restoreBase64('iVBORw0KG...', 'jpeg');
87
+ // Output: "data:image/jpeg;base64,iVBORw0KG..."
88
+ ```
89
+
90
+ ---
91
+
92
+ ### `downloadBase64File(base64Data: string, filename: string): void`
93
+
94
+ Triggers a download of a file from Base64-encoded data.
95
+
96
+ **Parameters:**
97
+ - `base64Data` (string): The Base64 encoded file data
98
+ - `filename` (string): The name of the file to download
99
+
100
+ **Example:**
101
+ ```javascript
102
+ webHelpers.downloadBase64File('data:image/png;base64,iVBORw0KG...', 'image.png');
103
+ ```
104
+
105
+ ---
106
+
107
+ ### `getFileExtensionFromBase64(base64String: string): string | null`
108
+
109
+ Extracts the file extension from a Base64 data URL.
110
+
111
+ **Parameters:**
112
+ - `base64String` (string): The Base64 data URL
113
+
114
+ **Returns:** File extension (e.g., "png", "pdf") or null if parsing fails
115
+
116
+ **Example:**
117
+ ```javascript
118
+ const ext = webHelpers.getFileExtensionFromBase64('data:image/png;base64,iVBORw0KG...');
119
+ // Output: "png"
120
+ ```
121
+
122
+ ---
123
+
124
+ ## Supported File Types
125
+
126
+ The `cleanBase64` function supports:
127
+ - Images: png, jpg, jpeg, gif, webp, etc.
128
+ - PDF documents
129
+ - Word documents (.docx, .dotx)
130
+ - Excel spreadsheets (.xlsx)
131
+
132
+ ## License
133
+
134
+ See LICENSE file for details.
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;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@accitdg/web-helpers",
3
- "version": "0.0.1",
3
+ "version": "0.0.3",
4
4
  "type": "module",
5
5
  "scripts": {
6
6
  "build": "rollup -c",