@react-pakistan/util-functions 1.25.73 → 1.25.75

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.
@@ -0,0 +1,55 @@
1
+ /**
2
+ * URL to Base64 Conversion Utility
3
+ *
4
+ * Functions to convert URLs to base64 encoded strings.
5
+ */
6
+ /**
7
+ * Convert a URL to base64 encoded string
8
+ *
9
+ * @param url - The URL to fetch and convert
10
+ * @returns Promise<string> - Base64 encoded string
11
+ * @throws Error if the URL cannot be fetched or converted
12
+ */
13
+ export declare function convertUrlToBase64(url: string): Promise<string>;
14
+ /**
15
+ * Convert a URL to base64 data URI format
16
+ *
17
+ * @param url - The URL to fetch and convert
18
+ * @param mimeType - Optional MIME type (will try to detect from response if not provided)
19
+ * @returns Promise<string> - Base64 data URI string (e.g., "data:image/png;base64,iVBORw0KGgo...")
20
+ * @throws Error if the URL cannot be fetched or converted
21
+ */
22
+ export declare function convertUrlToBase64DataUri(url: string, mimeType?: string): Promise<string>;
23
+ /**
24
+ * Convert multiple URLs to base64 strings
25
+ *
26
+ * @param urls - Array of URLs to convert
27
+ * @returns Promise<string[]> - Array of base64 encoded strings
28
+ * @throws Error if any URL cannot be fetched or converted
29
+ */
30
+ export declare function convertUrlsToBase64(urls: string[]): Promise<string[]>;
31
+ /**
32
+ * Convert multiple URLs to base64 data URIs
33
+ *
34
+ * @param urls - Array of URLs to convert
35
+ * @param mimeTypes - Optional array of MIME types (will try to detect if not provided)
36
+ * @returns Promise<string[]> - Array of base64 data URI strings
37
+ * @throws Error if any URL cannot be fetched or converted
38
+ */
39
+ export declare function convertUrlsToBase64DataUris(urls: string[], mimeTypes?: (string | undefined)[]): Promise<string[]>;
40
+ /**
41
+ * Check if a string is a valid URL
42
+ *
43
+ * @param url - The string to check
44
+ * @returns boolean - True if the string is a valid URL
45
+ */
46
+ export declare function isValidUrl(url: string): boolean;
47
+ /**
48
+ * Convert a URL to base64 with timeout
49
+ *
50
+ * @param url - The URL to fetch and convert
51
+ * @param timeoutMs - Timeout in milliseconds (default: 10000)
52
+ * @returns Promise<string> - Base64 encoded string
53
+ * @throws Error if the URL cannot be fetched within timeout or converted
54
+ */
55
+ export declare function convertUrlToBase64WithTimeout(url: string, timeoutMs?: number): Promise<string>;
@@ -0,0 +1,231 @@
1
+ "use strict";
2
+ /**
3
+ * URL to Base64 Conversion Utility
4
+ *
5
+ * Functions to convert URLs to base64 encoded strings.
6
+ */
7
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
8
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
9
+ return new (P || (P = Promise))(function (resolve, reject) {
10
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
11
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
12
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
13
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
14
+ });
15
+ };
16
+ var __generator = (this && this.__generator) || function (thisArg, body) {
17
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
18
+ return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
19
+ function verb(n) { return function (v) { return step([n, v]); }; }
20
+ function step(op) {
21
+ if (f) throw new TypeError("Generator is already executing.");
22
+ while (g && (g = 0, op[0] && (_ = 0)), _) try {
23
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
24
+ if (y = 0, t) op = [op[0] & 2, t.value];
25
+ switch (op[0]) {
26
+ case 0: case 1: t = op; break;
27
+ case 4: _.label++; return { value: op[1], done: false };
28
+ case 5: _.label++; y = op[1]; op = [0]; continue;
29
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
30
+ default:
31
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
32
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
33
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
34
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
35
+ if (t[2]) _.ops.pop();
36
+ _.trys.pop(); continue;
37
+ }
38
+ op = body.call(thisArg, _);
39
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
40
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
41
+ }
42
+ };
43
+ Object.defineProperty(exports, "__esModule", { value: true });
44
+ exports.convertUrlToBase64 = convertUrlToBase64;
45
+ exports.convertUrlToBase64DataUri = convertUrlToBase64DataUri;
46
+ exports.convertUrlsToBase64 = convertUrlsToBase64;
47
+ exports.convertUrlsToBase64DataUris = convertUrlsToBase64DataUris;
48
+ exports.isValidUrl = isValidUrl;
49
+ exports.convertUrlToBase64WithTimeout = convertUrlToBase64WithTimeout;
50
+ /**
51
+ * Convert a URL to base64 encoded string
52
+ *
53
+ * @param url - The URL to fetch and convert
54
+ * @returns Promise<string> - Base64 encoded string
55
+ * @throws Error if the URL cannot be fetched or converted
56
+ */
57
+ function convertUrlToBase64(url) {
58
+ return __awaiter(this, void 0, void 0, function () {
59
+ var response, arrayBuffer, uint8Array, binaryString, i, error_1;
60
+ return __generator(this, function (_a) {
61
+ switch (_a.label) {
62
+ case 0:
63
+ _a.trys.push([0, 3, , 4]);
64
+ return [4 /*yield*/, fetch(url)];
65
+ case 1:
66
+ response = _a.sent();
67
+ if (!response.ok) {
68
+ throw new Error("Failed to fetch URL: ".concat(response.status, " ").concat(response.statusText));
69
+ }
70
+ return [4 /*yield*/, response.arrayBuffer()];
71
+ case 2:
72
+ arrayBuffer = _a.sent();
73
+ uint8Array = new Uint8Array(arrayBuffer);
74
+ binaryString = '';
75
+ for (i = 0; i < uint8Array.length; i++) {
76
+ binaryString += String.fromCharCode(uint8Array[i]);
77
+ }
78
+ return [2 /*return*/, btoa(binaryString)];
79
+ case 3:
80
+ error_1 = _a.sent();
81
+ console.error('Error converting URL to base64:', error_1);
82
+ throw new Error("Failed to convert URL to base64: ".concat(error_1 instanceof Error ? error_1.message : 'Unknown error'));
83
+ case 4: return [2 /*return*/];
84
+ }
85
+ });
86
+ });
87
+ }
88
+ /**
89
+ * Convert a URL to base64 data URI format
90
+ *
91
+ * @param url - The URL to fetch and convert
92
+ * @param mimeType - Optional MIME type (will try to detect from response if not provided)
93
+ * @returns Promise<string> - Base64 data URI string (e.g., "data:image/png;base64,iVBORw0KGgo...")
94
+ * @throws Error if the URL cannot be fetched or converted
95
+ */
96
+ function convertUrlToBase64DataUri(url, mimeType) {
97
+ return __awaiter(this, void 0, void 0, function () {
98
+ var response, contentType, arrayBuffer, uint8Array, binaryString, i, base64, error_2;
99
+ return __generator(this, function (_a) {
100
+ switch (_a.label) {
101
+ case 0:
102
+ _a.trys.push([0, 3, , 4]);
103
+ return [4 /*yield*/, fetch(url)];
104
+ case 1:
105
+ response = _a.sent();
106
+ if (!response.ok) {
107
+ throw new Error("Failed to fetch URL: ".concat(response.status, " ").concat(response.statusText));
108
+ }
109
+ contentType = mimeType ||
110
+ response.headers.get('content-type') ||
111
+ 'application/octet-stream';
112
+ return [4 /*yield*/, response.arrayBuffer()];
113
+ case 2:
114
+ arrayBuffer = _a.sent();
115
+ uint8Array = new Uint8Array(arrayBuffer);
116
+ binaryString = '';
117
+ for (i = 0; i < uint8Array.length; i++) {
118
+ binaryString += String.fromCharCode(uint8Array[i]);
119
+ }
120
+ base64 = btoa(binaryString);
121
+ return [2 /*return*/, "data:".concat(contentType, ";base64,").concat(base64)];
122
+ case 3:
123
+ error_2 = _a.sent();
124
+ console.error('Error converting URL to base64 data URI:', error_2);
125
+ throw new Error("Failed to convert URL to base64 data URI: ".concat(error_2 instanceof Error ? error_2.message : 'Unknown error'));
126
+ case 4: return [2 /*return*/];
127
+ }
128
+ });
129
+ });
130
+ }
131
+ /**
132
+ * Convert multiple URLs to base64 strings
133
+ *
134
+ * @param urls - Array of URLs to convert
135
+ * @returns Promise<string[]> - Array of base64 encoded strings
136
+ * @throws Error if any URL cannot be fetched or converted
137
+ */
138
+ function convertUrlsToBase64(urls) {
139
+ return __awaiter(this, void 0, void 0, function () {
140
+ var promises;
141
+ return __generator(this, function (_a) {
142
+ promises = urls.map(function (url) { return convertUrlToBase64(url); });
143
+ return [2 /*return*/, Promise.all(promises)];
144
+ });
145
+ });
146
+ }
147
+ /**
148
+ * Convert multiple URLs to base64 data URIs
149
+ *
150
+ * @param urls - Array of URLs to convert
151
+ * @param mimeTypes - Optional array of MIME types (will try to detect if not provided)
152
+ * @returns Promise<string[]> - Array of base64 data URI strings
153
+ * @throws Error if any URL cannot be fetched or converted
154
+ */
155
+ function convertUrlsToBase64DataUris(urls, mimeTypes) {
156
+ return __awaiter(this, void 0, void 0, function () {
157
+ var promises;
158
+ return __generator(this, function (_a) {
159
+ promises = urls.map(function (url, index) {
160
+ return convertUrlToBase64DataUri(url, mimeTypes === null || mimeTypes === void 0 ? void 0 : mimeTypes[index]);
161
+ });
162
+ return [2 /*return*/, Promise.all(promises)];
163
+ });
164
+ });
165
+ }
166
+ /**
167
+ * Check if a string is a valid URL
168
+ *
169
+ * @param url - The string to check
170
+ * @returns boolean - True if the string is a valid URL
171
+ */
172
+ function isValidUrl(url) {
173
+ try {
174
+ new URL(url);
175
+ return true;
176
+ }
177
+ catch (_a) {
178
+ return false;
179
+ }
180
+ }
181
+ /**
182
+ * Convert a URL to base64 with timeout
183
+ *
184
+ * @param url - The URL to fetch and convert
185
+ * @param timeoutMs - Timeout in milliseconds (default: 10000)
186
+ * @returns Promise<string> - Base64 encoded string
187
+ * @throws Error if the URL cannot be fetched within timeout or converted
188
+ */
189
+ function convertUrlToBase64WithTimeout(url_1) {
190
+ return __awaiter(this, arguments, void 0, function (url, timeoutMs) {
191
+ var controller, timeoutId, response, arrayBuffer, uint8Array, binaryString, i, error_3;
192
+ if (timeoutMs === void 0) { timeoutMs = 10000; }
193
+ return __generator(this, function (_a) {
194
+ switch (_a.label) {
195
+ case 0:
196
+ controller = new AbortController();
197
+ timeoutId = setTimeout(function () { return controller.abort(); }, timeoutMs);
198
+ _a.label = 1;
199
+ case 1:
200
+ _a.trys.push([1, 4, , 5]);
201
+ return [4 /*yield*/, fetch(url, {
202
+ signal: controller.signal,
203
+ })];
204
+ case 2:
205
+ response = _a.sent();
206
+ clearTimeout(timeoutId);
207
+ if (!response.ok) {
208
+ throw new Error("Failed to fetch URL: ".concat(response.status, " ").concat(response.statusText));
209
+ }
210
+ return [4 /*yield*/, response.arrayBuffer()];
211
+ case 3:
212
+ arrayBuffer = _a.sent();
213
+ uint8Array = new Uint8Array(arrayBuffer);
214
+ binaryString = '';
215
+ for (i = 0; i < uint8Array.length; i++) {
216
+ binaryString += String.fromCharCode(uint8Array[i]);
217
+ }
218
+ return [2 /*return*/, btoa(binaryString)];
219
+ case 4:
220
+ error_3 = _a.sent();
221
+ clearTimeout(timeoutId);
222
+ if (error_3 instanceof Error && error_3.name === 'AbortError') {
223
+ throw new Error("Request timed out after ".concat(timeoutMs, "ms"));
224
+ }
225
+ console.error('Error converting URL to base64:', error_3);
226
+ throw new Error("Failed to convert URL to base64: ".concat(error_3 instanceof Error ? error_3.message : 'Unknown error'));
227
+ case 5: return [2 /*return*/];
228
+ }
229
+ });
230
+ });
231
+ }
@@ -0,0 +1 @@
1
+ export declare const formatValue: (v?: string | number | null) => string;
@@ -0,0 +1,7 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.formatValue = void 0;
4
+ var formatValue = function (v) {
5
+ return v === undefined || v === null || v === '' ? '—' : String(v);
6
+ };
7
+ exports.formatValue = formatValue;
@@ -0,0 +1,2 @@
1
+ export declare function normalizePhone(value: unknown): string | null | undefined;
2
+ export declare function formatPhoneDisplay(value: unknown, defaultCountry?: string): string | undefined;
@@ -0,0 +1,40 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.normalizePhone = normalizePhone;
4
+ exports.formatPhoneDisplay = formatPhoneDisplay;
5
+ var libphonenumber_js_1 = require("libphonenumber-js");
6
+ function normalizePhone(value) {
7
+ if (value === null)
8
+ return null;
9
+ if (typeof value !== 'string')
10
+ return undefined;
11
+ var trimmed = value.trim();
12
+ try {
13
+ var pn = (0, libphonenumber_js_1.parsePhoneNumberFromString)(trimmed);
14
+ if (pn && pn.isValid()) {
15
+ // Return E.164 digits without the leading +
16
+ return pn.number.replace(/\+/g, '');
17
+ }
18
+ return undefined;
19
+ }
20
+ catch (_a) {
21
+ return undefined;
22
+ }
23
+ }
24
+ function formatPhoneDisplay(value, defaultCountry) {
25
+ if (value === null)
26
+ return undefined;
27
+ if (typeof value !== 'string')
28
+ return undefined;
29
+ var trimmed = value.trim();
30
+ try {
31
+ var pn = (0, libphonenumber_js_1.parsePhoneNumberFromString)(trimmed, defaultCountry);
32
+ if (pn && pn.isValid()) {
33
+ return pn.formatInternational();
34
+ }
35
+ return undefined;
36
+ }
37
+ catch (_a) {
38
+ return undefined;
39
+ }
40
+ }
package/index.d.ts CHANGED
@@ -1,28 +0,0 @@
1
- export * from './api/stellar-solutions/app-user';
2
- export * from './api/stellar-solutions/bank';
3
- export * from './api/stellar-solutions/branch';
4
- export * from './api/stellar-solutions/company-report';
5
- export * from './api/stellar-solutions/company';
6
- export * from './api/stellar-solutions/constants';
7
- export * from './api/stellar-solutions/constants';
8
- export * from './api/stellar-solutions/contact';
9
- export * from './api/stellar-solutions/currency';
10
- export * from './api/stellar-solutions/customer';
11
- export * from './api/stellar-solutions/expense-category';
12
- export * from './api/stellar-solutions/expense';
13
- export * from './api/stellar-solutions/lead';
14
- export * from './api/stellar-solutions/menu-order';
15
- export * from './api/stellar-solutions/payment-mode';
16
- export * from './api/stellar-solutions/payment';
17
- export * from './api/stellar-solutions/preference';
18
- export * from './api/stellar-solutions/product-category';
19
- export * from './api/stellar-solutions/product';
20
- export * from './api/stellar-solutions/profile';
21
- export * from './api/stellar-solutions/quote-invoice-report';
22
- export * from './api/stellar-solutions/quote-invoice';
23
- export * from './api/stellar-solutions/tax';
24
- export * from './api/stellar-solutions/types';
25
- export * from './constants';
26
- export * from './general';
27
- export * from './local-storage';
28
- export * from './storybook';
package/index.js CHANGED
@@ -1,44 +1,28 @@
1
- "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- var desc = Object.getOwnPropertyDescriptor(m, k);
5
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
- desc = { enumerable: true, get: function() { return m[k]; } };
7
- }
8
- Object.defineProperty(o, k2, desc);
9
- }) : (function(o, m, k, k2) {
10
- if (k2 === undefined) k2 = k;
11
- o[k2] = m[k];
12
- }));
13
- var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
- for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
- };
16
- Object.defineProperty(exports, "__esModule", { value: true });
17
- __exportStar(require("./api/stellar-solutions/app-user"), exports);
18
- __exportStar(require("./api/stellar-solutions/bank"), exports);
19
- __exportStar(require("./api/stellar-solutions/branch"), exports);
20
- __exportStar(require("./api/stellar-solutions/company-report"), exports);
21
- __exportStar(require("./api/stellar-solutions/company"), exports);
22
- __exportStar(require("./api/stellar-solutions/constants"), exports);
23
- __exportStar(require("./api/stellar-solutions/constants"), exports);
24
- __exportStar(require("./api/stellar-solutions/contact"), exports);
25
- __exportStar(require("./api/stellar-solutions/currency"), exports);
26
- __exportStar(require("./api/stellar-solutions/customer"), exports);
27
- __exportStar(require("./api/stellar-solutions/expense-category"), exports);
28
- __exportStar(require("./api/stellar-solutions/expense"), exports);
29
- __exportStar(require("./api/stellar-solutions/lead"), exports);
30
- __exportStar(require("./api/stellar-solutions/menu-order"), exports);
31
- __exportStar(require("./api/stellar-solutions/payment-mode"), exports);
32
- __exportStar(require("./api/stellar-solutions/payment"), exports);
33
- __exportStar(require("./api/stellar-solutions/preference"), exports);
34
- __exportStar(require("./api/stellar-solutions/product-category"), exports);
35
- __exportStar(require("./api/stellar-solutions/product"), exports);
36
- __exportStar(require("./api/stellar-solutions/profile"), exports);
37
- __exportStar(require("./api/stellar-solutions/quote-invoice-report"), exports);
38
- __exportStar(require("./api/stellar-solutions/quote-invoice"), exports);
39
- __exportStar(require("./api/stellar-solutions/tax"), exports);
40
- __exportStar(require("./api/stellar-solutions/types"), exports);
41
- __exportStar(require("./constants"), exports);
42
- __exportStar(require("./general"), exports);
43
- __exportStar(require("./local-storage"), exports);
44
- __exportStar(require("./storybook"), exports);
1
+ // export * from './api/stellar-solutions/app-user';
2
+ // export * from './api/stellar-solutions/bank';
3
+ // export * from './api/stellar-solutions/branch';
4
+ // export * from './api/stellar-solutions/company-report';
5
+ // export * from './api/stellar-solutions/company';
6
+ // export * from './api/stellar-solutions/constants';
7
+ // export * from './api/stellar-solutions/constants';
8
+ // export * from './api/stellar-solutions/contact';
9
+ // export * from './api/stellar-solutions/currency';
10
+ // export * from './api/stellar-solutions/customer';
11
+ // export * from './api/stellar-solutions/expense-category';
12
+ // export * from './api/stellar-solutions/expense';
13
+ // export * from './api/stellar-solutions/lead';
14
+ // export * from './api/stellar-solutions/menu-order';
15
+ // export * from './api/stellar-solutions/payment-mode';
16
+ // export * from './api/stellar-solutions/payment';
17
+ // export * from './api/stellar-solutions/preference';
18
+ // export * from './api/stellar-solutions/product-category';
19
+ // export * from './api/stellar-solutions/product';
20
+ // export * from './api/stellar-solutions/profile';
21
+ // export * from './api/stellar-solutions/quote-invoice-report';
22
+ // export * from './api/stellar-solutions/quote-invoice';
23
+ // export * from './api/stellar-solutions/tax';
24
+ // export * from './api/stellar-solutions/types';
25
+ // export * from './constants';
26
+ // export * from './general';
27
+ // export * from './local-storage';
28
+ // export * from './storybook';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@react-pakistan/util-functions",
3
- "version": "1.25.73",
3
+ "version": "1.25.75",
4
4
  "description": "A library of all util functions",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -23,7 +23,7 @@
23
23
  "publish:github": "yarn build && cd lib && npm publish && cd ..",
24
24
  "publish:npm": "yarn build && cd lib && npm publish --access public && cd ..",
25
25
  "test": "jest --coverage --config jest.config.js",
26
- "test:badges": "yarn test && jest-coverage-badges && yarn clean:badges && mkdir badges && cp ./coverage/*badge*.svg ./badges/",
26
+ "test:badges": "yarn test && yarn clean:badges && mkdir -p badges && cp ./coverage/*badge*.svg ./badges/",
27
27
  "upgrade": "ncu -u"
28
28
  },
29
29
  "repository": {
@@ -69,7 +69,6 @@
69
69
  "globals": "^15.13.0",
70
70
  "husky": "^9.1.7",
71
71
  "jest": "^29.7.0",
72
- "jest-coverage-badges": "^1.1.2",
73
72
  "jest-environment-jsdom": "^29.7.0",
74
73
  "libphonenumber-js": "^1.12.13",
75
74
  "lint-staged": "^15.2.2",