@accitdg/web-helpers 0.0.24 → 0.0.25
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.ts +132 -52
- package/package.json +1 -1
package/dist/index.ts
CHANGED
|
@@ -1,35 +1,71 @@
|
|
|
1
|
-
|
|
1
|
+
export type payloadType =
|
|
2
|
+
| Record<string, any>
|
|
3
|
+
| string
|
|
4
|
+
| boolean
|
|
5
|
+
| number
|
|
6
|
+
| null;
|
|
2
7
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
const convertNumberToCurrency = numberValue => {
|
|
8
|
+
export const convertNumberToCurrency = (numberValue: number) => {
|
|
6
9
|
let formattedValue = numberValue.toLocaleString("en-PH", {
|
|
7
10
|
style: "currency",
|
|
8
|
-
currency: "PHP"
|
|
11
|
+
currency: "PHP",
|
|
9
12
|
});
|
|
13
|
+
|
|
10
14
|
return formattedValue;
|
|
11
15
|
};
|
|
12
|
-
const
|
|
13
|
-
|
|
14
|
-
|
|
16
|
+
export const formatNumberWithCommas = (number: number) => {
|
|
17
|
+
let numStr = number.toString();
|
|
18
|
+
let parts = numStr.split(".");
|
|
19
|
+
let integerPart = parts[0];
|
|
20
|
+
let pattern = /(\d)(?=(\d{3})+(?!\d))/g;
|
|
21
|
+
integerPart = integerPart.replace(pattern, "$1,");
|
|
22
|
+
|
|
23
|
+
if (parts.length > 1) {
|
|
24
|
+
return integerPart + "." + parts[1];
|
|
25
|
+
} else {
|
|
26
|
+
return integerPart;
|
|
27
|
+
}
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
export const restoreBase64 = (cleanedBase64: string, imageType = "png") =>
|
|
31
|
+
`data:image/${imageType};base64,${cleanedBase64}`;
|
|
32
|
+
|
|
33
|
+
export const cleanBase64 = (base64: string) => {
|
|
34
|
+
return base64
|
|
35
|
+
.replace(/^data:[a-zA-Z0-9\\/\\+\\-]+;base64,/, "")
|
|
36
|
+
.replace(
|
|
37
|
+
/^data:(application\/pdf|application\/vnd.openxmlformats-officedocument.wordprocessingml.document|application\/vnd.openxmlformats-officedocument.spreadsheetml.sheet|image\/[a-z]+);base64,/,
|
|
38
|
+
""
|
|
39
|
+
)
|
|
40
|
+
.replace(
|
|
41
|
+
"data:application/vnd.openxmlformats-officedocument.wordprocessingml.template;base64,",
|
|
42
|
+
""
|
|
43
|
+
);
|
|
15
44
|
};
|
|
16
|
-
|
|
17
|
-
|
|
45
|
+
|
|
46
|
+
export const downloadBase64File = (base64Data: string, filename: string) => {
|
|
47
|
+
const byteCharacters = atob(
|
|
48
|
+
restoreBase64(cleanBase64(base64Data)).split(",")[1]
|
|
49
|
+
);
|
|
50
|
+
|
|
18
51
|
const byteArray = new Uint8Array(byteCharacters.length);
|
|
52
|
+
|
|
19
53
|
for (let i = 0; i < byteCharacters.length; i++) {
|
|
20
54
|
byteArray[i] = byteCharacters.charCodeAt(i);
|
|
21
55
|
}
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
56
|
+
|
|
57
|
+
const blob = new Blob([byteArray], { type: "application/octet-stream" });
|
|
58
|
+
|
|
25
59
|
const link = document.createElement("a");
|
|
26
60
|
link.href = URL.createObjectURL(blob);
|
|
27
61
|
link.download = filename;
|
|
62
|
+
|
|
28
63
|
document.body.appendChild(link);
|
|
29
64
|
link.click();
|
|
30
65
|
document.body.removeChild(link);
|
|
31
66
|
};
|
|
32
|
-
|
|
67
|
+
|
|
68
|
+
export const arrayBufferToBase64 = (buffer: ArrayBuffer): string => {
|
|
33
69
|
let binary = "";
|
|
34
70
|
const bytes = new Uint8Array(buffer);
|
|
35
71
|
const len = bytes.byteLength;
|
|
@@ -38,7 +74,8 @@ const arrayBufferToBase64 = buffer => {
|
|
|
38
74
|
}
|
|
39
75
|
return window.btoa(binary);
|
|
40
76
|
};
|
|
41
|
-
|
|
77
|
+
|
|
78
|
+
export const getFileExtensionFromBase64 = (base64String: string) => {
|
|
42
79
|
try {
|
|
43
80
|
const contentType = base64String.split(";")[0].split(":")[1];
|
|
44
81
|
const extension = contentType.split("/")[1];
|
|
@@ -47,40 +84,60 @@ const getFileExtensionFromBase64 = base64String => {
|
|
|
47
84
|
return null;
|
|
48
85
|
}
|
|
49
86
|
};
|
|
50
|
-
|
|
87
|
+
|
|
88
|
+
export const createPayload = (args: {
|
|
89
|
+
pagination?: { count: number; page: number };
|
|
90
|
+
payload?: payloadType;
|
|
91
|
+
notifId?: string | null;
|
|
92
|
+
}) => {
|
|
51
93
|
// ...existing code...
|
|
52
|
-
let param =
|
|
94
|
+
let param =
|
|
95
|
+
typeof (globalThis as any).objTrim === "function"
|
|
96
|
+
? (globalThis as any).objTrim(args.payload ?? {})
|
|
97
|
+
: args.payload ?? {};
|
|
98
|
+
|
|
53
99
|
var data = {
|
|
54
100
|
isLazyLoading: false,
|
|
55
|
-
notificationId:
|
|
56
|
-
|
|
101
|
+
notificationId:
|
|
102
|
+
args.notifId ??
|
|
103
|
+
(globalThis as any).__WEB_HELPERS_CONFIG?.notificationId ??
|
|
104
|
+
null,
|
|
105
|
+
applicationId:
|
|
106
|
+
(globalThis as any).__WEB_HELPERS_CONFIG?.applicationId ?? null,
|
|
57
107
|
geoLocation: {
|
|
58
108
|
latitude: "0",
|
|
59
109
|
longitude: "0",
|
|
60
|
-
dateTime: new Date().toISOString()
|
|
110
|
+
dateTime: new Date().toISOString(),
|
|
61
111
|
},
|
|
62
|
-
pagination: args.pagination ?? {
|
|
63
|
-
|
|
64
|
-
page: 0
|
|
65
|
-
},
|
|
66
|
-
payload: param
|
|
112
|
+
pagination: args.pagination ?? { count: 0, page: 0 },
|
|
113
|
+
payload: param,
|
|
67
114
|
};
|
|
115
|
+
|
|
68
116
|
return data;
|
|
69
117
|
};
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
p: 0
|
|
74
|
-
},
|
|
118
|
+
|
|
119
|
+
export const createPayload2 = ({
|
|
120
|
+
pagination = { c: 0, p: 0 },
|
|
75
121
|
payload = {},
|
|
76
122
|
notifId,
|
|
77
|
-
applicationId
|
|
123
|
+
applicationId,
|
|
124
|
+
}: {
|
|
125
|
+
pagination?: { c: number; p: number };
|
|
126
|
+
payload?: payloadType;
|
|
127
|
+
notifId?: string | null;
|
|
128
|
+
applicationId?: string | null;
|
|
78
129
|
} = {}) => {
|
|
79
|
-
const cfg = globalThis.__WEB_HELPERS_CONFIG || {};
|
|
130
|
+
const cfg = (globalThis as any).__WEB_HELPERS_CONFIG || {};
|
|
80
131
|
const nId = notifId ?? cfg.notificationId ?? null;
|
|
81
132
|
const aId = applicationId ?? cfg.applicationId ?? null;
|
|
82
|
-
|
|
133
|
+
|
|
134
|
+
const objTrimFn =
|
|
135
|
+
typeof (globalThis as any).objTrim === "function"
|
|
136
|
+
? (globalThis as any).objTrim
|
|
137
|
+
: (o: any) => o;
|
|
138
|
+
|
|
83
139
|
const param = objTrimFn(payload);
|
|
140
|
+
|
|
84
141
|
return {
|
|
85
142
|
isLl: false,
|
|
86
143
|
nId,
|
|
@@ -88,31 +145,54 @@ const createPayload2 = ({
|
|
|
88
145
|
gL: {
|
|
89
146
|
lt: "0",
|
|
90
147
|
long: "0",
|
|
91
|
-
date: new Date().toISOString()
|
|
148
|
+
date: new Date().toISOString(),
|
|
92
149
|
},
|
|
93
150
|
pg: pagination,
|
|
94
|
-
p: param
|
|
151
|
+
p: param,
|
|
95
152
|
};
|
|
96
153
|
};
|
|
97
154
|
// Add this helper to configure global values (call once during app init)
|
|
98
|
-
const configureHelpers = config
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
155
|
+
export const configureHelpers = (config: {
|
|
156
|
+
applicationId?: string;
|
|
157
|
+
notificationId?: string;
|
|
158
|
+
}) => {
|
|
159
|
+
(globalThis as any).__WEB_HELPERS_CONFIG = {
|
|
160
|
+
...((globalThis as any).__WEB_HELPERS_CONFIG || {}),
|
|
161
|
+
...config,
|
|
102
162
|
};
|
|
103
163
|
};
|
|
104
164
|
|
|
105
|
-
const
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
165
|
+
export const titleCase = (str: string) =>
|
|
166
|
+
typeof str === "string"
|
|
167
|
+
? str
|
|
168
|
+
.split(/\s+/)
|
|
169
|
+
.filter(Boolean)
|
|
170
|
+
.map((w) => w.charAt(0).toUpperCase() + w.slice(1).toLowerCase())
|
|
171
|
+
.join(" ")
|
|
172
|
+
: str;
|
|
173
|
+
|
|
174
|
+
export const maskFormatPhilippineNumber = (number: string) => {
|
|
175
|
+
const cleanedNumber = number.replace(/[^0-9+]/g, "");
|
|
176
|
+
|
|
177
|
+
let formattedNumber;
|
|
178
|
+
|
|
179
|
+
if (cleanedNumber.startsWith("+63")) {
|
|
180
|
+
formattedNumber = cleanedNumber.substring(3);
|
|
181
|
+
} else if (cleanedNumber.startsWith("0")) {
|
|
182
|
+
formattedNumber = cleanedNumber.substring(1);
|
|
183
|
+
} else if (cleanedNumber.length === 11) {
|
|
184
|
+
formattedNumber = cleanedNumber;
|
|
185
|
+
} else {
|
|
186
|
+
return number;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
if (formattedNumber.length !== 11) {
|
|
190
|
+
return number;
|
|
191
|
+
}
|
|
192
|
+
formattedNumber = `+63 ${formattedNumber.slice(0, 4)} ${formattedNumber.slice(
|
|
193
|
+
4,
|
|
194
|
+
8
|
|
195
|
+
)} ${formattedNumber.slice(8)}`;
|
|
116
196
|
|
|
117
|
-
|
|
118
|
-
|
|
197
|
+
return formattedNumber;
|
|
198
|
+
};
|