@accitdg/web-helpers 0.0.2 → 0.0.4
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 +254 -1
- package/dist/index.esm.ts +58 -1
- package/dist/index.ts +58 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1 +1,254 @@
|
|
|
1
|
-
|
|
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 {
|
|
19
|
+
convertNumberToCurrency,
|
|
20
|
+
arrayBufferToBase64 /* ... */,
|
|
21
|
+
} from "web-helpers";
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
## Functions
|
|
25
|
+
|
|
26
|
+
### `convertNumberToCurrency(numberValue: number): string`
|
|
27
|
+
|
|
28
|
+
Converts a number to Philippine Peso (PHP) currency format.
|
|
29
|
+
|
|
30
|
+
**Parameters:**
|
|
31
|
+
|
|
32
|
+
- `numberValue` (number): The number to convert
|
|
33
|
+
|
|
34
|
+
**Returns:** Formatted currency string
|
|
35
|
+
|
|
36
|
+
**Example:**
|
|
37
|
+
|
|
38
|
+
```javascript
|
|
39
|
+
webHelpers.convertNumberToCurrency(1000);
|
|
40
|
+
// Output: "₱1,000.00"
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
---
|
|
44
|
+
|
|
45
|
+
### `arrayBufferToBase64(buffer: ArrayBuffer): string`
|
|
46
|
+
|
|
47
|
+
Converts an ArrayBuffer to a Base64-encoded string.
|
|
48
|
+
|
|
49
|
+
**Parameters:**
|
|
50
|
+
|
|
51
|
+
- `buffer` (ArrayBuffer): The buffer to encode
|
|
52
|
+
|
|
53
|
+
**Returns:** Base64 encoded string
|
|
54
|
+
|
|
55
|
+
**Example:**
|
|
56
|
+
|
|
57
|
+
```javascript
|
|
58
|
+
const buffer = new ArrayBuffer(8);
|
|
59
|
+
const base64 = webHelpers.arrayBufferToBase64(buffer);
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
---
|
|
63
|
+
|
|
64
|
+
### `cleanBase64(base64: string): string`
|
|
65
|
+
|
|
66
|
+
Removes the data URL prefix from a Base64 string, leaving only the encoded data.
|
|
67
|
+
|
|
68
|
+
**Parameters:**
|
|
69
|
+
|
|
70
|
+
- `base64` (string): The Base64 string with or without prefix
|
|
71
|
+
|
|
72
|
+
**Returns:** Cleaned Base64 string without prefix
|
|
73
|
+
|
|
74
|
+
**Example:**
|
|
75
|
+
|
|
76
|
+
```javascript
|
|
77
|
+
const cleaned = webHelpers.cleanBase64("data:image/png;base64,iVBORw0KG...");
|
|
78
|
+
// Output: "iVBORw0KG..."
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
---
|
|
82
|
+
|
|
83
|
+
### `restoreBase64(cleanedBase64: string, imageType?: string): string`
|
|
84
|
+
|
|
85
|
+
Restores a clean Base64 string to a complete data URL format.
|
|
86
|
+
|
|
87
|
+
**Parameters:**
|
|
88
|
+
|
|
89
|
+
- `cleanedBase64` (string): The Base64 string without prefix
|
|
90
|
+
- `imageType` (string, optional): Image type (default: "png")
|
|
91
|
+
|
|
92
|
+
**Returns:** Complete data URL string
|
|
93
|
+
|
|
94
|
+
**Example:**
|
|
95
|
+
|
|
96
|
+
```javascript
|
|
97
|
+
const dataUrl = webHelpers.restoreBase64("iVBORw0KG...", "jpeg");
|
|
98
|
+
// Output: "data:image/jpeg;base64,iVBORw0KG..."
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
---
|
|
102
|
+
|
|
103
|
+
### `downloadBase64File(base64Data: string, filename: string): void`
|
|
104
|
+
|
|
105
|
+
Triggers a download of a file from Base64-encoded data.
|
|
106
|
+
|
|
107
|
+
**Parameters:**
|
|
108
|
+
|
|
109
|
+
- `base64Data` (string): The Base64 encoded file data
|
|
110
|
+
- `filename` (string): The name of the file to download
|
|
111
|
+
|
|
112
|
+
**Example:**
|
|
113
|
+
|
|
114
|
+
```javascript
|
|
115
|
+
webHelpers.downloadBase64File(
|
|
116
|
+
"data:image/png;base64,iVBORw0KG...",
|
|
117
|
+
"image.png"
|
|
118
|
+
);
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
---
|
|
122
|
+
|
|
123
|
+
### `getFileExtensionFromBase64(base64String: string): string | null`
|
|
124
|
+
|
|
125
|
+
Extracts the file extension from a Base64 data URL.
|
|
126
|
+
|
|
127
|
+
**Parameters:**
|
|
128
|
+
|
|
129
|
+
- `base64String` (string): The Base64 data URL
|
|
130
|
+
|
|
131
|
+
**Returns:** File extension (e.g., "png", "pdf") or null if parsing fails
|
|
132
|
+
|
|
133
|
+
**Example:**
|
|
134
|
+
|
|
135
|
+
```javascript
|
|
136
|
+
const ext = webHelpers.getFileExtensionFromBase64(
|
|
137
|
+
"data:image/png;base64,iVBORw0KG..."
|
|
138
|
+
);
|
|
139
|
+
// Output: "png"
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
---
|
|
143
|
+
|
|
144
|
+
## Configuration
|
|
145
|
+
|
|
146
|
+
createPayload and createPayload2 read applicationId and notificationId from a global config object. Call configureHelpers once during app initialization to set global defaults:
|
|
147
|
+
|
|
148
|
+
```ts
|
|
149
|
+
configureHelpers({
|
|
150
|
+
applicationId: "app-123",
|
|
151
|
+
notificationId: "notif-abc",
|
|
152
|
+
});
|
|
153
|
+
```
|
|
154
|
+
|
|
155
|
+
- These global values are stored on globalThis.\_\_WEB_HELPERS_CONFIG.
|
|
156
|
+
- Individual calls to createPayload / createPayload2 can override these values by passing notifId / applicationId in the arguments.
|
|
157
|
+
|
|
158
|
+
## Payload factories
|
|
159
|
+
|
|
160
|
+
### createPayload(args?)
|
|
161
|
+
|
|
162
|
+
Returns a full, readable payload shape suitable for APIs that expect verbose keys.
|
|
163
|
+
|
|
164
|
+
Signature (TypeScript):
|
|
165
|
+
|
|
166
|
+
```ts
|
|
167
|
+
createPayload(args?: {
|
|
168
|
+
pagination?: { count: number; page: number };
|
|
169
|
+
payload?: Record<string, any>;
|
|
170
|
+
notifId?: string | null;
|
|
171
|
+
})
|
|
172
|
+
```
|
|
173
|
+
|
|
174
|
+
Returns:
|
|
175
|
+
|
|
176
|
+
```ts
|
|
177
|
+
{
|
|
178
|
+
isLazyLoading: boolean;
|
|
179
|
+
notificationId: string | null;
|
|
180
|
+
applicationId: string | null;
|
|
181
|
+
geoLocation: {
|
|
182
|
+
latitude: string;
|
|
183
|
+
longitude: string;
|
|
184
|
+
dateTime: string;
|
|
185
|
+
}
|
|
186
|
+
pagination: {
|
|
187
|
+
count: number;
|
|
188
|
+
page: number;
|
|
189
|
+
}
|
|
190
|
+
payload: Record<string, any>;
|
|
191
|
+
}
|
|
192
|
+
```
|
|
193
|
+
|
|
194
|
+
Example:
|
|
195
|
+
|
|
196
|
+
```ts
|
|
197
|
+
const result = createPayload({ payload: { foo: "bar" } });
|
|
198
|
+
// uses configured notificationId/applicationId unless notifId is provided
|
|
199
|
+
```
|
|
200
|
+
|
|
201
|
+
### createPayload2(options?)
|
|
202
|
+
|
|
203
|
+
Returns a compact payload shape (short keys) for services that use abbreviated keys.
|
|
204
|
+
|
|
205
|
+
Signature (TypeScript):
|
|
206
|
+
|
|
207
|
+
```ts
|
|
208
|
+
createPayload2({
|
|
209
|
+
pagination?: { c: number; p: number },
|
|
210
|
+
payload?: Record<string, any>,
|
|
211
|
+
notifId?: string | null,
|
|
212
|
+
applicationId?: string | null,
|
|
213
|
+
} = {})
|
|
214
|
+
```
|
|
215
|
+
|
|
216
|
+
Returns:
|
|
217
|
+
|
|
218
|
+
```ts
|
|
219
|
+
{
|
|
220
|
+
isLl: boolean;
|
|
221
|
+
nId: string | null;
|
|
222
|
+
aId: string | null;
|
|
223
|
+
gL: {
|
|
224
|
+
lt: string;
|
|
225
|
+
long: string;
|
|
226
|
+
date: string;
|
|
227
|
+
}
|
|
228
|
+
pg: {
|
|
229
|
+
c: number;
|
|
230
|
+
p: number;
|
|
231
|
+
}
|
|
232
|
+
p: Record<string, any>;
|
|
233
|
+
}
|
|
234
|
+
```
|
|
235
|
+
|
|
236
|
+
Example:
|
|
237
|
+
|
|
238
|
+
```ts
|
|
239
|
+
const short = createPayload2({ payload: { x: 1 } });
|
|
240
|
+
// uses configured applicationId/notificationId unless overridden in the call
|
|
241
|
+
```
|
|
242
|
+
|
|
243
|
+
## Supported File Types
|
|
244
|
+
|
|
245
|
+
The `cleanBase64` function supports:
|
|
246
|
+
|
|
247
|
+
- Images: png, jpg, jpeg, gif, webp, etc.
|
|
248
|
+
- PDF documents
|
|
249
|
+
- Word documents (.docx, .dotx)
|
|
250
|
+
- Excel spreadsheets (.xlsx)
|
|
251
|
+
|
|
252
|
+
## License
|
|
253
|
+
|
|
254
|
+
@allcardtech2025
|
package/dist/index.esm.ts
CHANGED
|
@@ -43,6 +43,60 @@ const getFileExtensionFromBase64 = base64String => {
|
|
|
43
43
|
return null;
|
|
44
44
|
}
|
|
45
45
|
};
|
|
46
|
+
const createPayload = args => {
|
|
47
|
+
// ...existing code...
|
|
48
|
+
let param = typeof globalThis.objTrim === "function" ? globalThis.objTrim(args.payload ?? {}) : args.payload ?? {};
|
|
49
|
+
var data = {
|
|
50
|
+
isLazyLoading: false,
|
|
51
|
+
notificationId: args.notifId ?? globalThis.__WEB_HELPERS_CONFIG?.notificationId ?? null,
|
|
52
|
+
applicationId: globalThis.__WEB_HELPERS_CONFIG?.applicationId ?? null,
|
|
53
|
+
geoLocation: {
|
|
54
|
+
latitude: "0",
|
|
55
|
+
longitude: "0",
|
|
56
|
+
dateTime: new Date().toISOString()
|
|
57
|
+
},
|
|
58
|
+
pagination: args.pagination ?? {
|
|
59
|
+
count: 0,
|
|
60
|
+
page: 0
|
|
61
|
+
},
|
|
62
|
+
payload: param
|
|
63
|
+
};
|
|
64
|
+
return data;
|
|
65
|
+
};
|
|
66
|
+
const createPayload2 = ({
|
|
67
|
+
pagination = {
|
|
68
|
+
c: 0,
|
|
69
|
+
p: 0
|
|
70
|
+
},
|
|
71
|
+
payload = {},
|
|
72
|
+
notifId,
|
|
73
|
+
applicationId
|
|
74
|
+
} = {}) => {
|
|
75
|
+
const cfg = globalThis.__WEB_HELPERS_CONFIG || {};
|
|
76
|
+
const nId = notifId ?? cfg.notificationId ?? null;
|
|
77
|
+
const aId = applicationId ?? cfg.applicationId ?? null;
|
|
78
|
+
const objTrimFn = typeof globalThis.objTrim === "function" ? globalThis.objTrim : o => o;
|
|
79
|
+
const param = objTrimFn(payload);
|
|
80
|
+
return {
|
|
81
|
+
isLl: false,
|
|
82
|
+
nId,
|
|
83
|
+
aId,
|
|
84
|
+
gL: {
|
|
85
|
+
lt: "0",
|
|
86
|
+
long: "0",
|
|
87
|
+
date: new Date().toISOString()
|
|
88
|
+
},
|
|
89
|
+
pg: pagination,
|
|
90
|
+
p: param
|
|
91
|
+
};
|
|
92
|
+
};
|
|
93
|
+
// Add this helper to configure global values (call once during app init)
|
|
94
|
+
const configureHelpers = config => {
|
|
95
|
+
globalThis.__WEB_HELPERS_CONFIG = {
|
|
96
|
+
...(globalThis.__WEB_HELPERS_CONFIG || {}),
|
|
97
|
+
...config
|
|
98
|
+
};
|
|
99
|
+
};
|
|
46
100
|
|
|
47
101
|
const webHelpers = {
|
|
48
102
|
convertNumberToCurrency,
|
|
@@ -50,7 +104,10 @@ const webHelpers = {
|
|
|
50
104
|
cleanBase64,
|
|
51
105
|
downloadBase64File,
|
|
52
106
|
restoreBase64,
|
|
53
|
-
getFileExtensionFromBase64
|
|
107
|
+
getFileExtensionFromBase64,
|
|
108
|
+
configureHelpers,
|
|
109
|
+
createPayload,
|
|
110
|
+
createPayload2
|
|
54
111
|
};
|
|
55
112
|
|
|
56
113
|
export { webHelpers as default, webHelpers };
|
package/dist/index.ts
CHANGED
|
@@ -47,6 +47,60 @@ const getFileExtensionFromBase64 = base64String => {
|
|
|
47
47
|
return null;
|
|
48
48
|
}
|
|
49
49
|
};
|
|
50
|
+
const createPayload = args => {
|
|
51
|
+
// ...existing code...
|
|
52
|
+
let param = typeof globalThis.objTrim === "function" ? globalThis.objTrim(args.payload ?? {}) : args.payload ?? {};
|
|
53
|
+
var data = {
|
|
54
|
+
isLazyLoading: false,
|
|
55
|
+
notificationId: args.notifId ?? globalThis.__WEB_HELPERS_CONFIG?.notificationId ?? null,
|
|
56
|
+
applicationId: globalThis.__WEB_HELPERS_CONFIG?.applicationId ?? null,
|
|
57
|
+
geoLocation: {
|
|
58
|
+
latitude: "0",
|
|
59
|
+
longitude: "0",
|
|
60
|
+
dateTime: new Date().toISOString()
|
|
61
|
+
},
|
|
62
|
+
pagination: args.pagination ?? {
|
|
63
|
+
count: 0,
|
|
64
|
+
page: 0
|
|
65
|
+
},
|
|
66
|
+
payload: param
|
|
67
|
+
};
|
|
68
|
+
return data;
|
|
69
|
+
};
|
|
70
|
+
const createPayload2 = ({
|
|
71
|
+
pagination = {
|
|
72
|
+
c: 0,
|
|
73
|
+
p: 0
|
|
74
|
+
},
|
|
75
|
+
payload = {},
|
|
76
|
+
notifId,
|
|
77
|
+
applicationId
|
|
78
|
+
} = {}) => {
|
|
79
|
+
const cfg = globalThis.__WEB_HELPERS_CONFIG || {};
|
|
80
|
+
const nId = notifId ?? cfg.notificationId ?? null;
|
|
81
|
+
const aId = applicationId ?? cfg.applicationId ?? null;
|
|
82
|
+
const objTrimFn = typeof globalThis.objTrim === "function" ? globalThis.objTrim : o => o;
|
|
83
|
+
const param = objTrimFn(payload);
|
|
84
|
+
return {
|
|
85
|
+
isLl: false,
|
|
86
|
+
nId,
|
|
87
|
+
aId,
|
|
88
|
+
gL: {
|
|
89
|
+
lt: "0",
|
|
90
|
+
long: "0",
|
|
91
|
+
date: new Date().toISOString()
|
|
92
|
+
},
|
|
93
|
+
pg: pagination,
|
|
94
|
+
p: param
|
|
95
|
+
};
|
|
96
|
+
};
|
|
97
|
+
// Add this helper to configure global values (call once during app init)
|
|
98
|
+
const configureHelpers = config => {
|
|
99
|
+
globalThis.__WEB_HELPERS_CONFIG = {
|
|
100
|
+
...(globalThis.__WEB_HELPERS_CONFIG || {}),
|
|
101
|
+
...config
|
|
102
|
+
};
|
|
103
|
+
};
|
|
50
104
|
|
|
51
105
|
const webHelpers = {
|
|
52
106
|
convertNumberToCurrency,
|
|
@@ -54,7 +108,10 @@ const webHelpers = {
|
|
|
54
108
|
cleanBase64,
|
|
55
109
|
downloadBase64File,
|
|
56
110
|
restoreBase64,
|
|
57
|
-
getFileExtensionFromBase64
|
|
111
|
+
getFileExtensionFromBase64,
|
|
112
|
+
configureHelpers,
|
|
113
|
+
createPayload,
|
|
114
|
+
createPayload2
|
|
58
115
|
};
|
|
59
116
|
|
|
60
117
|
exports["default"] = webHelpers;
|