@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.
Files changed (2) hide show
  1. package/dist/index.ts +132 -52
  2. package/package.json +1 -1
package/dist/index.ts CHANGED
@@ -1,35 +1,71 @@
1
- 'use strict';
1
+ export type payloadType =
2
+ | Record<string, any>
3
+ | string
4
+ | boolean
5
+ | number
6
+ | null;
2
7
 
3
- Object.defineProperty(exports, '__esModule', { value: true });
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 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,", "");
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
- const downloadBase64File = (base64Data, filename) => {
17
- const byteCharacters = atob(restoreBase64(cleanBase64(base64Data)).split(",")[1]);
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
- const blob = new Blob([byteArray], {
23
- type: "application/octet-stream"
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
- const arrayBufferToBase64 = buffer => {
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
- const getFileExtensionFromBase64 = base64String => {
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
- const createPayload = args => {
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 = typeof globalThis.objTrim === "function" ? globalThis.objTrim(args.payload ?? {}) : args.payload ?? {};
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: args.notifId ?? globalThis.__WEB_HELPERS_CONFIG?.notificationId ?? null,
56
- applicationId: globalThis.__WEB_HELPERS_CONFIG?.applicationId ?? null,
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
- count: 0,
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
- const createPayload2 = ({
71
- pagination = {
72
- c: 0,
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
- const objTrimFn = typeof globalThis.objTrim === "function" ? globalThis.objTrim : o => o;
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
- globalThis.__WEB_HELPERS_CONFIG = {
100
- ...(globalThis.__WEB_HELPERS_CONFIG || {}),
101
- ...config
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 webHelpers = {
106
- convertNumberToCurrency,
107
- arrayBufferToBase64,
108
- cleanBase64,
109
- downloadBase64File,
110
- restoreBase64,
111
- getFileExtensionFromBase64,
112
- configureHelpers,
113
- createPayload,
114
- createPayload2
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
- exports["default"] = webHelpers;
118
- exports.webHelpers = webHelpers;
197
+ return formattedNumber;
198
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@accitdg/web-helpers",
3
- "version": "0.0.24",
3
+ "version": "0.0.25",
4
4
  "type": "module",
5
5
  "main": "dist/index.cjs.js",
6
6
  "module": "dist/index.esm.js",