dhavaltank-js-utils 0.0.3 → 0.0.5
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/.nvmrc +1 -0
- package/README.md +140 -7
- package/dist/index.cjs +231 -48
- package/dist/index.d.cts +46 -7
- package/dist/index.d.ts +46 -7
- package/dist/index.js +220 -47
- package/package.json +6 -5
- package/test/index.test.ts +391 -0
package/.nvmrc
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
v26.13.1
|
package/README.md
CHANGED
|
@@ -33,20 +33,37 @@ yarn add dhavaltank-js-utils
|
|
|
33
33
|
<li><strong>isFalsy</strong></li>
|
|
34
34
|
<li><strong>isTruthy</strong></li>
|
|
35
35
|
<li><strong>groupBy</strong></li>
|
|
36
|
+
<li><strong>capitalizeEveryWord</strong></li>
|
|
37
|
+
<li><strong>camelCase</strong></li>
|
|
38
|
+
<li><strong>pascalCase</strong></li>
|
|
39
|
+
<li><strong>truncate</strong></li>
|
|
40
|
+
<li><strong>formatDate</strong></li>
|
|
41
|
+
<li><strong>deepClone</strong></li>
|
|
42
|
+
<li><strong>debounce</strong></li>
|
|
43
|
+
<li><strong>formatDateRelative</strong></li>
|
|
44
|
+
<li><strong>capitalizeEveryWord</strong></li>
|
|
45
|
+
<li><strong>camelCase</strong></li>
|
|
46
|
+
<li><strong>pascalCase</strong></li>
|
|
47
|
+
<li><strong>truncate</strong></li>
|
|
48
|
+
<li><strong>formatDate</strong></li>
|
|
49
|
+
<li><strong>deepClone</strong></li>
|
|
50
|
+
<li><strong>debounce</strong></li>
|
|
51
|
+
<li><strong>formatDateRelative</strong></li>
|
|
36
52
|
</ol>
|
|
37
53
|
<h1 id="documentation">Documentation</h1>
|
|
38
54
|
<h2 id="capitalizefirstletter-function">capitalizeFirstLetter(str)</h2>
|
|
39
55
|
<p><strong>Function Description:</strong> </p>
|
|
40
|
-
<p>The capitalizeFirstLetter function
|
|
56
|
+
<p>The capitalizeFirstLetter function capitalizes the first letter of a string. It trims whitespace from the input and handles edge cases like empty strings.</p>
|
|
41
57
|
<p><strong>Parameters</strong></p>
|
|
42
58
|
<ul>
|
|
43
|
-
<li>str (string): The input string to be capitalized.</li>
|
|
59
|
+
<li>str (string, nullable): The input string to be capitalized. Can be null or empty.</li>
|
|
44
60
|
</ul>
|
|
45
61
|
<p><strong>Return Value</strong></p>
|
|
46
|
-
<p>Returns a new string with the first letter capitalized.</p>
|
|
47
|
-
<pre><code>import { capitalizeFirstLetter } from
|
|
48
|
-
capitalizeFirstLetter(
|
|
49
|
-
|
|
62
|
+
<p>Returns a new string with the first letter capitalized. Returns null if input is null, empty string if input is empty/whitespace-only.</p>
|
|
63
|
+
<pre><code>import { capitalizeFirstLetter } from "dhavaltank-js-utils";
|
|
64
|
+
capitalizeFirstLetter("hello world"); // 8216;Hello world8217;
|
|
65
|
+
capitalizeFirstLetter(null); // null
|
|
66
|
+
capitalizeFirstLetter(""); // 8216;8217;
|
|
50
67
|
<h2 id="titlecasestr"><strong>titleCase(str)</strong></h2>
|
|
51
68
|
<p><strong>Function Description:</strong> </p>
|
|
52
69
|
<p>The titleCase function takes a string (str) and an optional key (key) as parameters. The purpose of this function is to convert the input string into a title case, where the first letter of each word is capitalized. Additionally, the function allows for a special case where if the key parameter is set to "And," it replaces the occurrence of the word "And" with the ampersand "&" in the final title case string.</p>
|
|
@@ -395,7 +412,123 @@ const exampleData = [
|
|
|
395
412
|
];
|
|
396
413
|
groupBy(exampleData, 'category'); // { A: [{ id: 1, category: 'A', value: 42 }, { id: 3, category: 'A', value: 78 }], B: [{ id: 2, category: 'B', value: 65 }] }
|
|
397
414
|
</code></pre>
|
|
398
|
-
|
|
415
|
+
|
|
416
|
+
<h2 id="capitalizeeveryword-function">capitalizeEveryWord(str)</h2>
|
|
417
|
+
<p><strong>Function Description:</strong> </p>
|
|
418
|
+
<p>The capitalizeEveryWord function takes a string as input and returns a new string with the first letter of each word capitalized.</p>
|
|
419
|
+
<p><strong>Parameters</strong></p>
|
|
420
|
+
<ul>
|
|
421
|
+
<li>str (string): The input string to be capitalized.</li>
|
|
422
|
+
</ul>
|
|
423
|
+
<p><strong>Return Value</strong></p>
|
|
424
|
+
<p>Returns a new string with the first letter of each word capitalized.</p>
|
|
425
|
+
<pre><code>import { capitalizeEveryWord } from "dhavaltank-js-utils";
|
|
426
|
+
capitalizeEveryWord("hello world"); // 'Hello World'
|
|
427
|
+
capitalizeEveryWord("javascript utility package"); // 'JavaScript Utility Package'
|
|
428
|
+
</code></pre>
|
|
429
|
+
|
|
430
|
+
<h2 id="camelcase-function">camelCase(str)</h2>
|
|
431
|
+
<p><strong>Function Description:</strong> </p>
|
|
432
|
+
<p>The camelCase function converts a string to camelCase format, where the first word is lowercase and the first letter of each subsequent word is capitalized.</p>
|
|
433
|
+
<p><strong>Parameters</strong></p>
|
|
434
|
+
<ul>
|
|
435
|
+
<li>str (string): The input string to be converted to camelCase.</li>
|
|
436
|
+
</ul>
|
|
437
|
+
<p><strong>Return Value</strong></p>
|
|
438
|
+
<p>Returns the input string converted to camelCase.</p>
|
|
439
|
+
<pre><code>import { camelCase } from "dhavaltank-js-utils";
|
|
440
|
+
camelCase("hello world"); // 'helloWorld'
|
|
441
|
+
camelCase("javascript utility package"); // 'javascriptUtilityPackage'
|
|
442
|
+
</code></pre>
|
|
443
|
+
|
|
444
|
+
<h2 id="pascalcase-function">pascalCase(str)</h2>
|
|
445
|
+
<p><strong>Function Description:</strong> </p>
|
|
446
|
+
<p>The pascalCase function converts a string to PascalCase format, where the first letter of each word is capitalized.</p>
|
|
447
|
+
<p><strong>Parameters</strong></p>
|
|
448
|
+
<ul>
|
|
449
|
+
<li>str (string): The input string to be converted to PascalCase.</li>
|
|
450
|
+
</ul>
|
|
451
|
+
<p><strong>Return Value</strong></p>
|
|
452
|
+
<p>Returns the input string converted to PascalCase.</p>
|
|
453
|
+
<pre><code>import { pascalCase } from "dhavaltank-js-utils";
|
|
454
|
+
pascalCase("hello world"); // 'HelloWorld'
|
|
455
|
+
pascalCase("javascript utility package"); // 'JavascriptUtilityPackage'
|
|
456
|
+
</code></pre>
|
|
457
|
+
|
|
458
|
+
<h2 id="truncate-function">truncate(str, maxLength)</h2>
|
|
459
|
+
<p><strong>Function Description:</strong> </p>
|
|
460
|
+
<p>The truncate function truncates a string to a specified maximum length and appends an ellipsis ("...") if the original string length exceeds the specified maximum length.</p>
|
|
461
|
+
<p><strong>Parameters</strong></p>
|
|
462
|
+
<ul>
|
|
463
|
+
<li>str (string): The input string that may need truncation.</li>
|
|
464
|
+
<li>maxLength (number, optional): The maximum length to which the input string should be truncated. Defaults to 20 if not provided.</li>
|
|
465
|
+
</ul>
|
|
466
|
+
<p><strong>Return Value</strong></p>
|
|
467
|
+
<p>The function returns the truncated string, followed by an ellipsis if the original string length exceeds the specified maximum length. If the input string is empty or falsy, an empty string is returned.</p>
|
|
468
|
+
<pre><code>import { truncate } from "dhavaltank-js-utils";
|
|
469
|
+
truncate("Hello World User"); // 'Hello World U...'
|
|
470
|
+
truncate("Hello World User", 5); // 'Hello...'
|
|
471
|
+
</code></pre>
|
|
472
|
+
|
|
473
|
+
<h2 id="formatdate-function">formatDate(date, format)</h2>
|
|
474
|
+
<p><strong>Function Description:</strong> </p>
|
|
475
|
+
<p>The formatDate function formats a date object to a readable string with a specified format.</p>
|
|
476
|
+
<p><strong>Parameters</strong></p>
|
|
477
|
+
<ul>
|
|
478
|
+
<li>date (Date | string | number): The date to be formatted.</li>
|
|
479
|
+
<li>format (string, optional): The format string with placeholders YYYY, MM, DD, HH, mm, ss. Defaults to "YYYY-MM-DD" if not provided.</li>
|
|
480
|
+
</ul>
|
|
481
|
+
<p><strong>Return Value</strong></p>
|
|
482
|
+
<p>Returns the formatted date string.</p>
|
|
483
|
+
<pre><code>import { formatDate } from "dhavaltank-js-utils";
|
|
484
|
+
formatDate(new Date()); // '2024-01-15' (example output)
|
|
485
|
+
formatDate("2024-01-15T10:30:00", "YYYY/MM/DD"); // '2024/01/15'
|
|
486
|
+
</code></pre>
|
|
487
|
+
|
|
488
|
+
<h2 id="deepclone-function">deepClone(obj)</h2>
|
|
489
|
+
<p><strong>Function Description:</strong> </p>
|
|
490
|
+
<p>The deepClone function creates a deep copy of an object, including nested objects and arrays.</p>
|
|
491
|
+
<p><strong>Parameters</strong></p>
|
|
492
|
+
<ul>
|
|
493
|
+
<li>obj (required): The input object to be deep cloned.</li>
|
|
494
|
+
</ul>
|
|
495
|
+
<p><strong>Return Value</strong></p>
|
|
496
|
+
<p>Returns a new object that is a deep copy of the input object.</p>
|
|
497
|
+
<pre><code>import { deepClone } from "dhavaltank-js-utils";
|
|
498
|
+
const original = { a: 1, b: { c: 2 }, d: [1, 2, 3] };
|
|
499
|
+
const cloned = deepClone(original);
|
|
500
|
+
cloned.b.c = 99;
|
|
501
|
+
console.log(original.b.c); // 2 (original is unchanged)
|
|
502
|
+
</code></pre>
|
|
503
|
+
|
|
504
|
+
<h2 id="debounce-function">debounce(func, wait)</h2>
|
|
505
|
+
<p><strong>Function Description:</strong> </p>
|
|
506
|
+
<p>The debounce function creates a debounced function that delays invocation of the provided function until after a specified wait time has elapsed since the last time it was invoked.</p>
|
|
507
|
+
<p><strong>Parameters</strong></p>
|
|
508
|
+
<ul>
|
|
509
|
+
<li>func (function): The function to debounce.</li>
|
|
510
|
+
<li>wait (number): The number of milliseconds to delay the function invocation.</li>
|
|
511
|
+
</ul>
|
|
512
|
+
<p><strong>Return Value</strong></p>
|
|
513
|
+
<p>Returns a new debounced function.</p>
|
|
514
|
+
<pre><code>import { debounce } from "dhavaltank-js-utils";
|
|
515
|
+
const handleResize = debounce(() => { console.log("window resized"); }, 300);
|
|
516
|
+
window.addEventListener("resize", handleResize);
|
|
517
|
+
</code></pre>
|
|
518
|
+
|
|
519
|
+
<h2 id="formatdaterelative-function">formatDateRelative(date)</h2>
|
|
520
|
+
<p><strong>Function Description:</strong> </p>
|
|
521
|
+
<p>The formatDateRelative function formats a date as a relative string (e.g., "just now", "2 hours ago", "3 days ago").</p>
|
|
522
|
+
<p><strong>Parameters</strong></p>
|
|
523
|
+
<ul>
|
|
524
|
+
<li>date (Date | string | number): The date to be formatted as a relative string.</li>
|
|
525
|
+
</ul>
|
|
526
|
+
<p><strong>Return Value</strong></p>
|
|
527
|
+
<p>Returns a relative time string representing how long ago the date was.</p>
|
|
528
|
+
<pre><code>import { formatDateRelative } from "dhavaltank-js-utils";
|
|
529
|
+
formatDateRelative(new Date()); // 'just now' (if called immediately)
|
|
530
|
+
formatDateRelative(new Date(Date.now() - 2 * 60 * 60 * 1000)); // '2 hours ago'
|
|
531
|
+
</code></pre><h2 id="authors">Authors</h2>
|
|
399
532
|
<ul>
|
|
400
533
|
<li><a href="https://www.github.com/dhavaltank01">Dhaval Tank</a></li>
|
|
401
534
|
</ul>
|
package/dist/index.cjs
CHANGED
|
@@ -20,29 +20,39 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
20
20
|
// src/index.ts
|
|
21
21
|
var src_exports = {};
|
|
22
22
|
__export(src_exports, {
|
|
23
|
+
CURRENCYCODE: () => CURRENCYCODE,
|
|
24
|
+
camelCase: () => camelCase,
|
|
25
|
+
capitalizeEveryWord: () => capitalizeEveryWord,
|
|
23
26
|
capitalizeFirstLetter: () => capitalizeFirstLetter,
|
|
24
27
|
checkNotNullAndNotEmpty: () => checkNotNullAndNotEmpty,
|
|
25
28
|
clearLocalStorage: () => clearLocalStorage,
|
|
26
29
|
compareArrays: () => compareArrays,
|
|
30
|
+
debounce: () => debounce,
|
|
31
|
+
deepClone: () => deepClone,
|
|
27
32
|
formatCurrency: () => formatCurrency,
|
|
33
|
+
formatDate: () => formatDate,
|
|
34
|
+
formatDateRelative: () => formatDateRelative,
|
|
28
35
|
generateOTPWithLength: () => generateOTPWithLength,
|
|
29
36
|
generateRandomPassword: () => generateRandomPassword,
|
|
30
37
|
getGreeting: () => getGreeting,
|
|
31
38
|
getInitials: () => getInitials,
|
|
32
39
|
getLocalStorage: () => getLocalStorage,
|
|
33
40
|
getTruncateDescription: () => getTruncateDescription,
|
|
41
|
+
getValueByKey: () => getValueByKey,
|
|
34
42
|
groupBy: () => groupBy,
|
|
35
43
|
handleNaN: () => handleNaN,
|
|
36
44
|
isFalsy: () => isFalsy,
|
|
37
45
|
isObjectValuesEmpty: () => isObjectValuesEmpty,
|
|
38
46
|
isTruthy: () => isTruthy,
|
|
47
|
+
pascalCase: () => pascalCase,
|
|
39
48
|
removeDuplicatesFromArray: () => removeDuplicatesFromArray,
|
|
40
49
|
removeLocalStorage: () => removeLocalStorage,
|
|
41
50
|
reverseString: () => reverseString,
|
|
42
51
|
setLocalStorage: () => setLocalStorage,
|
|
43
52
|
slugify: () => slugify,
|
|
44
|
-
|
|
53
|
+
sortArrayByDirection: () => sortArrayByDirection,
|
|
45
54
|
titleCase: () => titleCase,
|
|
55
|
+
truncate: () => truncate,
|
|
46
56
|
validateEmail: () => validateEmail,
|
|
47
57
|
validateMobileNumber: () => validateMobileNumber,
|
|
48
58
|
validatePassword: () => validatePassword,
|
|
@@ -53,20 +63,23 @@ module.exports = __toCommonJS(src_exports);
|
|
|
53
63
|
// src/functions/index.ts
|
|
54
64
|
var capitalizeFirstLetter = (str) => {
|
|
55
65
|
try {
|
|
56
|
-
if (
|
|
66
|
+
if (str == null || str === "")
|
|
57
67
|
return null;
|
|
58
|
-
|
|
68
|
+
const trimmed = str.trim();
|
|
69
|
+
if (trimmed.length === 0)
|
|
70
|
+
return "";
|
|
71
|
+
return trimmed.charAt(0).toUpperCase() + trimmed.slice(1);
|
|
59
72
|
} catch (error) {
|
|
60
73
|
console.error(error);
|
|
61
74
|
return null;
|
|
62
75
|
}
|
|
63
76
|
};
|
|
64
|
-
var titleCase = (str,
|
|
77
|
+
var titleCase = (str, separator = " ") => {
|
|
65
78
|
try {
|
|
66
|
-
if (
|
|
79
|
+
if (str == null || str === "")
|
|
67
80
|
return null;
|
|
68
|
-
const
|
|
69
|
-
return
|
|
81
|
+
const normalized = str.replace(/[-_]/g, separator).split(separator).filter((word) => word.length > 0);
|
|
82
|
+
return normalized.map((word) => word.charAt(0).toUpperCase() + word.slice(1)).join(separator);
|
|
70
83
|
} catch (error) {
|
|
71
84
|
console.error(error);
|
|
72
85
|
return null;
|
|
@@ -76,26 +89,48 @@ var getInitials = (name) => {
|
|
|
76
89
|
try {
|
|
77
90
|
if (typeof name !== "string" || name.trim() === "")
|
|
78
91
|
return "?";
|
|
79
|
-
const words = name.trim().split(
|
|
80
|
-
|
|
92
|
+
const words = name.trim().split(/[\s_-]+/).filter((value) => value.length > 0);
|
|
93
|
+
if (words.length === 0)
|
|
94
|
+
return "?";
|
|
95
|
+
const initials = words.map((word) => word.charAt(0)?.toUpperCase() ?? "").join("");
|
|
96
|
+
if (words.length === 1) {
|
|
97
|
+
const word = words[0];
|
|
98
|
+
if (word.length <= 1)
|
|
99
|
+
return word.toUpperCase();
|
|
100
|
+
return word.charAt(0).toUpperCase() + word.slice(-1).toUpperCase();
|
|
101
|
+
}
|
|
102
|
+
return initials;
|
|
81
103
|
} catch (error) {
|
|
82
104
|
console.error(error);
|
|
105
|
+
return "?";
|
|
83
106
|
}
|
|
84
107
|
};
|
|
85
|
-
var getTruncateDescription = (text, maxLength =
|
|
108
|
+
var getTruncateDescription = (text, maxLength = 50) => {
|
|
86
109
|
try {
|
|
87
|
-
|
|
110
|
+
if (!text)
|
|
111
|
+
return "";
|
|
112
|
+
if (text.length <= maxLength)
|
|
113
|
+
return text;
|
|
114
|
+
return `${text.substring(0, maxLength)}...`;
|
|
88
115
|
} catch (error) {
|
|
89
116
|
console.error(error);
|
|
90
|
-
return
|
|
117
|
+
return "";
|
|
91
118
|
}
|
|
92
119
|
};
|
|
93
120
|
var compareArrays = (arr1, arr2) => {
|
|
94
121
|
try {
|
|
95
|
-
|
|
122
|
+
if (arr1 == null || arr2 == null)
|
|
123
|
+
return false;
|
|
124
|
+
const array1 = Array.isArray(arr1) ? arr1.flat(Infinity).slice() : [arr1];
|
|
125
|
+
const array2 = Array.isArray(arr2) ? arr2.flat(Infinity).slice() : [arr2];
|
|
126
|
+
const sorted1 = array1.sort();
|
|
127
|
+
const sorted2 = array2.sort();
|
|
128
|
+
if (sorted1.length !== sorted2.length)
|
|
129
|
+
return false;
|
|
130
|
+
return sorted1.every((value, index) => value === sorted2[index]);
|
|
96
131
|
} catch (error) {
|
|
97
132
|
console.error(error);
|
|
98
|
-
return
|
|
133
|
+
return false;
|
|
99
134
|
}
|
|
100
135
|
};
|
|
101
136
|
var generateOTPWithLength = (length, isAlphaNumeric = false) => {
|
|
@@ -132,7 +167,7 @@ var generateRandomPassword = (length = 8) => {
|
|
|
132
167
|
};
|
|
133
168
|
var slugify = (string, separator = "-") => {
|
|
134
169
|
try {
|
|
135
|
-
return string
|
|
170
|
+
return string?.trim()?.length > 0 ? string?.toString()?.toLowerCase()?.trim().replace(/\s+/g, separator)?.replace(/[^\w\-]+/g, "")?.replace(/\_/g, separator)?.replace(/\-\-+/g, separator)?.replace(/\-$/g, "") : "";
|
|
136
171
|
} catch (error) {
|
|
137
172
|
console.error(error);
|
|
138
173
|
return null;
|
|
@@ -194,13 +229,13 @@ var getGreeting = () => {
|
|
|
194
229
|
};
|
|
195
230
|
var validateText = (value, errorMessage = null) => {
|
|
196
231
|
try {
|
|
197
|
-
if (!value
|
|
232
|
+
if (!value)
|
|
198
233
|
return { isError: true, errorMessage: errorMessage || "This field must not be empty." };
|
|
199
234
|
const regex = /^[a-zA-Z ]+$/;
|
|
200
235
|
if (!regex.test(value?.trim())) {
|
|
201
236
|
return { isError: true, errorMessage: errorMessage || "Invalid input" };
|
|
202
237
|
}
|
|
203
|
-
return { isError: false, errorMessage:
|
|
238
|
+
return { isError: false, errorMessage: null };
|
|
204
239
|
} catch (error) {
|
|
205
240
|
console.error(error);
|
|
206
241
|
return { isError: true, message: "Something went wrong." };
|
|
@@ -208,13 +243,15 @@ var validateText = (value, errorMessage = null) => {
|
|
|
208
243
|
};
|
|
209
244
|
var validateEmail = (email, errorMessage = null) => {
|
|
210
245
|
try {
|
|
211
|
-
if (
|
|
246
|
+
if (email == null || String(email).trim() === "") {
|
|
212
247
|
return { isError: true, errorMessage: errorMessage || "This field must not be empty." };
|
|
213
|
-
|
|
214
|
-
|
|
248
|
+
}
|
|
249
|
+
const emailStr = String(email).trim();
|
|
250
|
+
const re = /^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/;
|
|
251
|
+
if (!re.test(emailStr)) {
|
|
215
252
|
return { isError: true, errorMessage: errorMessage || "Invalid email" };
|
|
216
253
|
}
|
|
217
|
-
return { isError: false, errorMessage:
|
|
254
|
+
return { isError: false, errorMessage: null };
|
|
218
255
|
} catch (error) {
|
|
219
256
|
console.error(error);
|
|
220
257
|
return { isError: true, message: "Something went wrong." };
|
|
@@ -228,7 +265,7 @@ var validatePassword = (password, errorMessage = null) => {
|
|
|
228
265
|
if (!regex.test(password?.trim())) {
|
|
229
266
|
return { isError: true, errorMessage: errorMessage || "Password must contain at least 8 characters, including at least one uppercase letter, one lowercase letter, one number, and one symbol." };
|
|
230
267
|
}
|
|
231
|
-
return { isError: false, errorMessage:
|
|
268
|
+
return { isError: false, errorMessage: null };
|
|
232
269
|
} catch (error) {
|
|
233
270
|
console.error(error);
|
|
234
271
|
return { isError: true, message: "Something went wrong." };
|
|
@@ -236,19 +273,22 @@ var validatePassword = (password, errorMessage = null) => {
|
|
|
236
273
|
};
|
|
237
274
|
var validateMobileNumber = (value, errorMessage = null) => {
|
|
238
275
|
try {
|
|
239
|
-
|
|
276
|
+
const strValue = typeof value === "number" ? String(value) : value;
|
|
277
|
+
if (strValue == null || strValue.trim() === "") {
|
|
240
278
|
return { isError: true, errorMessage: errorMessage || "This field must not be empty." };
|
|
241
|
-
|
|
242
|
-
|
|
279
|
+
}
|
|
280
|
+
const trimmed = strValue.trim();
|
|
281
|
+
const regex = /^(?:\+?91[-\s]?)?\d{10}$/;
|
|
282
|
+
if (!regex.test(trimmed)) {
|
|
243
283
|
return { isError: true, errorMessage: errorMessage || "Invalid mobile number" };
|
|
244
284
|
}
|
|
245
|
-
return { isError: false, errorMessage:
|
|
285
|
+
return { isError: false, errorMessage: null };
|
|
246
286
|
} catch (error) {
|
|
247
287
|
console.error(error);
|
|
248
288
|
return { isError: true, message: "Something went wrong." };
|
|
249
289
|
}
|
|
250
290
|
};
|
|
251
|
-
var
|
|
291
|
+
var CURRENCYCODE = {
|
|
252
292
|
USD: "USD",
|
|
253
293
|
EUR: "EUR",
|
|
254
294
|
GBP: "GBP",
|
|
@@ -259,13 +299,13 @@ var CurrencyCode = {
|
|
|
259
299
|
CHF: "CHF",
|
|
260
300
|
CNY: "CNY"
|
|
261
301
|
};
|
|
262
|
-
var formatCurrency = (number, currencyCode =
|
|
302
|
+
var formatCurrency = (number, currencyCode = CURRENCYCODE.USD, minimumFractionDigits = 2, maximumFractionDigits = 2) => {
|
|
263
303
|
try {
|
|
264
304
|
if (!number)
|
|
265
305
|
return null;
|
|
266
|
-
if (!Object.values(
|
|
306
|
+
if (!Object.values(CURRENCYCODE).includes(currencyCode)) {
|
|
267
307
|
console.warn(`Invalid currency code "${currencyCode}". Defaulting to USD.`);
|
|
268
|
-
currencyCode =
|
|
308
|
+
currencyCode = CURRENCYCODE.USD;
|
|
269
309
|
}
|
|
270
310
|
return number?.toLocaleString("en-US", {
|
|
271
311
|
style: "currency",
|
|
@@ -286,42 +326,58 @@ var removeDuplicatesFromArray = (array) => {
|
|
|
286
326
|
return null;
|
|
287
327
|
}
|
|
288
328
|
};
|
|
289
|
-
|
|
329
|
+
function getValueByKey(obj, key) {
|
|
330
|
+
if (obj && typeof obj === "object" && key in obj) {
|
|
331
|
+
return obj[key];
|
|
332
|
+
}
|
|
333
|
+
return null;
|
|
334
|
+
}
|
|
335
|
+
var sortArrayByDirection = (array, direction = "asc", key) => {
|
|
290
336
|
try {
|
|
291
337
|
if (!array || array.length === 0)
|
|
292
|
-
return
|
|
293
|
-
if (
|
|
294
|
-
console.error('Invalid sorting
|
|
338
|
+
return [];
|
|
339
|
+
if (direction !== "asc" && direction !== "desc") {
|
|
340
|
+
console.error('Invalid sorting direction. Please use "asc" or "desc".');
|
|
295
341
|
return array;
|
|
296
342
|
}
|
|
297
|
-
|
|
298
|
-
const
|
|
299
|
-
const
|
|
300
|
-
|
|
343
|
+
const sorted = [...array].sort((a, b) => {
|
|
344
|
+
const valueA = key ? a[key] : a;
|
|
345
|
+
const valueB = key ? b[key] : b;
|
|
346
|
+
if (typeof valueA === "string" && typeof valueB === "string") {
|
|
347
|
+
return direction === "asc" ? valueA.localeCompare(valueB) : valueB.localeCompare(valueA);
|
|
348
|
+
} else if (typeof valueA === "number" && typeof valueB === "number") {
|
|
349
|
+
return direction === "asc" ? valueA - valueB : valueB - valueA;
|
|
350
|
+
} else if (valueA instanceof Date && valueB instanceof Date) {
|
|
351
|
+
return direction === "asc" ? valueA.getTime() - valueB.getTime() : valueB.getTime() - valueA.getTime();
|
|
352
|
+
}
|
|
353
|
+
return 0;
|
|
301
354
|
});
|
|
355
|
+
return sorted;
|
|
302
356
|
} catch (error) {
|
|
303
357
|
console.error(error);
|
|
304
|
-
return
|
|
358
|
+
return array;
|
|
305
359
|
}
|
|
306
360
|
};
|
|
307
361
|
var reverseString = (str) => {
|
|
308
362
|
try {
|
|
309
363
|
if (!str)
|
|
310
364
|
return null;
|
|
311
|
-
return str?.split("")
|
|
365
|
+
return str?.split("")?.reverse()?.join("");
|
|
312
366
|
} catch (error) {
|
|
313
367
|
console.error(error);
|
|
314
368
|
return null;
|
|
315
369
|
}
|
|
316
370
|
};
|
|
317
371
|
var isObjectValuesEmpty = (object) => {
|
|
318
|
-
|
|
319
|
-
if (
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
return
|
|
323
|
-
}
|
|
324
|
-
|
|
372
|
+
try {
|
|
373
|
+
if (object == null || typeof object !== "object")
|
|
374
|
+
return { isEmpty: false, emptyKeys: [] };
|
|
375
|
+
const emptyKeys = Object.keys(object).filter((key) => !object[key]);
|
|
376
|
+
return { isEmpty: emptyKeys.length > 0, emptyKeys };
|
|
377
|
+
} catch (error) {
|
|
378
|
+
console.error(error);
|
|
379
|
+
return { isEmpty: false, emptyKeys: [] };
|
|
380
|
+
}
|
|
325
381
|
};
|
|
326
382
|
var checkNotNullAndNotEmpty = (value) => {
|
|
327
383
|
try {
|
|
@@ -351,6 +407,123 @@ var isFalsy = (value) => {
|
|
|
351
407
|
var isTruthy = (value) => {
|
|
352
408
|
return !!value;
|
|
353
409
|
};
|
|
410
|
+
var capitalizeEveryWord = (str) => {
|
|
411
|
+
try {
|
|
412
|
+
if (!str)
|
|
413
|
+
return "";
|
|
414
|
+
return str.trim().split(/\s+/).map((word) => word.charAt(0).toUpperCase() + word.slice(1)).join(" ");
|
|
415
|
+
} catch (error) {
|
|
416
|
+
console.error(error);
|
|
417
|
+
return "";
|
|
418
|
+
}
|
|
419
|
+
};
|
|
420
|
+
var camelCase = (str) => {
|
|
421
|
+
try {
|
|
422
|
+
if (!str)
|
|
423
|
+
return "";
|
|
424
|
+
return str.trim().replace(/[-_\s]/g, " ").split(/\s+/).map((word, index) => index === 0 ? word.toLowerCase() : word.charAt(0).toUpperCase() + word.slice(1).toLowerCase()).join("");
|
|
425
|
+
} catch (error) {
|
|
426
|
+
console.error(error);
|
|
427
|
+
return "";
|
|
428
|
+
}
|
|
429
|
+
};
|
|
430
|
+
var pascalCase = (str) => {
|
|
431
|
+
try {
|
|
432
|
+
if (!str)
|
|
433
|
+
return "";
|
|
434
|
+
return str.trim().replace(/[-_\s]/g, " ").split(/\s+/).map((word) => word.charAt(0).toUpperCase() + word.slice(1).toLowerCase()).join("");
|
|
435
|
+
} catch (error) {
|
|
436
|
+
console.error(error);
|
|
437
|
+
return "";
|
|
438
|
+
}
|
|
439
|
+
};
|
|
440
|
+
var truncate = (str, maxLength = 20) => {
|
|
441
|
+
try {
|
|
442
|
+
if (!str)
|
|
443
|
+
return "";
|
|
444
|
+
if (str.length <= maxLength)
|
|
445
|
+
return str;
|
|
446
|
+
return str.substring(0, maxLength).trim() + "...";
|
|
447
|
+
} catch (error) {
|
|
448
|
+
console.error(error);
|
|
449
|
+
return "";
|
|
450
|
+
}
|
|
451
|
+
};
|
|
452
|
+
var formatDate = (date, format = "YYYY-MM-DD") => {
|
|
453
|
+
try {
|
|
454
|
+
const d = new Date(date);
|
|
455
|
+
if (isNaN(d.getTime()))
|
|
456
|
+
return "";
|
|
457
|
+
const year = d.getFullYear();
|
|
458
|
+
const month = String(d.getMonth() + 1).padStart(2, "0");
|
|
459
|
+
const day = String(d.getDate()).padStart(2, "0");
|
|
460
|
+
const hours = String(d.getHours()).padStart(2, "0");
|
|
461
|
+
const minutes = String(d.getMinutes()).padStart(2, "0");
|
|
462
|
+
const seconds = String(d.getSeconds()).padStart(2, "0");
|
|
463
|
+
return format.replace("YYYY", String(year)).replace("MM", month).replace("DD", day).replace("HH", hours).replace("mm", minutes).replace("ss", seconds);
|
|
464
|
+
} catch (error) {
|
|
465
|
+
console.error(error);
|
|
466
|
+
return "";
|
|
467
|
+
}
|
|
468
|
+
};
|
|
469
|
+
var deepClone = (obj) => {
|
|
470
|
+
try {
|
|
471
|
+
if (obj === null || obj === void 0)
|
|
472
|
+
return obj;
|
|
473
|
+
if (obj instanceof Date)
|
|
474
|
+
return new Date(obj.getTime());
|
|
475
|
+
if (Array.isArray(obj))
|
|
476
|
+
return obj.map((item) => deepClone(item));
|
|
477
|
+
if (typeof obj === "object" && obj !== null) {
|
|
478
|
+
const cloned = {};
|
|
479
|
+
for (const key of Object.keys(obj)) {
|
|
480
|
+
cloned[key] = deepClone(obj[key]);
|
|
481
|
+
}
|
|
482
|
+
return cloned;
|
|
483
|
+
}
|
|
484
|
+
return obj;
|
|
485
|
+
} catch (error) {
|
|
486
|
+
console.error(error);
|
|
487
|
+
return obj;
|
|
488
|
+
}
|
|
489
|
+
};
|
|
490
|
+
var debounce = (func, wait) => {
|
|
491
|
+
let timeout;
|
|
492
|
+
return function executedFunction(...args) {
|
|
493
|
+
const later = () => {
|
|
494
|
+
func(...args);
|
|
495
|
+
};
|
|
496
|
+
clearTimeout(timeout);
|
|
497
|
+
timeout = setTimeout(later, wait);
|
|
498
|
+
};
|
|
499
|
+
};
|
|
500
|
+
var formatDateRelative = (date) => {
|
|
501
|
+
try {
|
|
502
|
+
const d = new Date(date);
|
|
503
|
+
if (isNaN(d.getTime()))
|
|
504
|
+
return "";
|
|
505
|
+
const seconds = Math.floor(((/* @__PURE__ */ new Date()).getTime() - d.getTime()) / 1e3);
|
|
506
|
+
let interval = seconds / 60;
|
|
507
|
+
if (interval < 1)
|
|
508
|
+
return "just now";
|
|
509
|
+
if (interval < 1.5)
|
|
510
|
+
return "a minute ago";
|
|
511
|
+
if (interval < 45)
|
|
512
|
+
return Math.floor(interval) + " minutes ago";
|
|
513
|
+
if (interval < 90)
|
|
514
|
+
return "an hour ago";
|
|
515
|
+
if (interval < 1440)
|
|
516
|
+
return Math.floor(interval / 60) + " hours ago";
|
|
517
|
+
if (interval < 2880)
|
|
518
|
+
return Math.floor(interval / 1440) + " days ago";
|
|
519
|
+
if (interval < 43200)
|
|
520
|
+
return Math.floor(interval / 1440 / 30) + " months ago";
|
|
521
|
+
return Math.floor(interval / 1440 / 365) + " years ago";
|
|
522
|
+
} catch (error) {
|
|
523
|
+
console.error(error);
|
|
524
|
+
return "";
|
|
525
|
+
}
|
|
526
|
+
};
|
|
354
527
|
var groupBy = (arr, key) => {
|
|
355
528
|
try {
|
|
356
529
|
if (!arr)
|
|
@@ -369,29 +542,39 @@ var groupBy = (arr, key) => {
|
|
|
369
542
|
};
|
|
370
543
|
// Annotate the CommonJS export names for ESM import in node:
|
|
371
544
|
0 && (module.exports = {
|
|
545
|
+
CURRENCYCODE,
|
|
546
|
+
camelCase,
|
|
547
|
+
capitalizeEveryWord,
|
|
372
548
|
capitalizeFirstLetter,
|
|
373
549
|
checkNotNullAndNotEmpty,
|
|
374
550
|
clearLocalStorage,
|
|
375
551
|
compareArrays,
|
|
552
|
+
debounce,
|
|
553
|
+
deepClone,
|
|
376
554
|
formatCurrency,
|
|
555
|
+
formatDate,
|
|
556
|
+
formatDateRelative,
|
|
377
557
|
generateOTPWithLength,
|
|
378
558
|
generateRandomPassword,
|
|
379
559
|
getGreeting,
|
|
380
560
|
getInitials,
|
|
381
561
|
getLocalStorage,
|
|
382
562
|
getTruncateDescription,
|
|
563
|
+
getValueByKey,
|
|
383
564
|
groupBy,
|
|
384
565
|
handleNaN,
|
|
385
566
|
isFalsy,
|
|
386
567
|
isObjectValuesEmpty,
|
|
387
568
|
isTruthy,
|
|
569
|
+
pascalCase,
|
|
388
570
|
removeDuplicatesFromArray,
|
|
389
571
|
removeLocalStorage,
|
|
390
572
|
reverseString,
|
|
391
573
|
setLocalStorage,
|
|
392
574
|
slugify,
|
|
393
|
-
|
|
575
|
+
sortArrayByDirection,
|
|
394
576
|
titleCase,
|
|
577
|
+
truncate,
|
|
395
578
|
validateEmail,
|
|
396
579
|
validateMobileNumber,
|
|
397
580
|
validatePassword,
|