dhavaltank-js-utils 0.0.4 → 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 +199 -49
- package/dist/index.d.cts +18 -7
- package/dist/index.d.ts +18 -7
- package/dist/index.js +191 -49
- package/package.json +6 -4
- 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
|
@@ -21,11 +21,17 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
21
21
|
var src_exports = {};
|
|
22
22
|
__export(src_exports, {
|
|
23
23
|
CURRENCYCODE: () => CURRENCYCODE,
|
|
24
|
+
camelCase: () => camelCase,
|
|
25
|
+
capitalizeEveryWord: () => capitalizeEveryWord,
|
|
24
26
|
capitalizeFirstLetter: () => capitalizeFirstLetter,
|
|
25
27
|
checkNotNullAndNotEmpty: () => checkNotNullAndNotEmpty,
|
|
26
28
|
clearLocalStorage: () => clearLocalStorage,
|
|
27
29
|
compareArrays: () => compareArrays,
|
|
30
|
+
debounce: () => debounce,
|
|
31
|
+
deepClone: () => deepClone,
|
|
28
32
|
formatCurrency: () => formatCurrency,
|
|
33
|
+
formatDate: () => formatDate,
|
|
34
|
+
formatDateRelative: () => formatDateRelative,
|
|
29
35
|
generateOTPWithLength: () => generateOTPWithLength,
|
|
30
36
|
generateRandomPassword: () => generateRandomPassword,
|
|
31
37
|
getGreeting: () => getGreeting,
|
|
@@ -38,6 +44,7 @@ __export(src_exports, {
|
|
|
38
44
|
isFalsy: () => isFalsy,
|
|
39
45
|
isObjectValuesEmpty: () => isObjectValuesEmpty,
|
|
40
46
|
isTruthy: () => isTruthy,
|
|
47
|
+
pascalCase: () => pascalCase,
|
|
41
48
|
removeDuplicatesFromArray: () => removeDuplicatesFromArray,
|
|
42
49
|
removeLocalStorage: () => removeLocalStorage,
|
|
43
50
|
reverseString: () => reverseString,
|
|
@@ -45,6 +52,7 @@ __export(src_exports, {
|
|
|
45
52
|
slugify: () => slugify,
|
|
46
53
|
sortArrayByDirection: () => sortArrayByDirection,
|
|
47
54
|
titleCase: () => titleCase,
|
|
55
|
+
truncate: () => truncate,
|
|
48
56
|
validateEmail: () => validateEmail,
|
|
49
57
|
validateMobileNumber: () => validateMobileNumber,
|
|
50
58
|
validatePassword: () => validatePassword,
|
|
@@ -55,19 +63,23 @@ module.exports = __toCommonJS(src_exports);
|
|
|
55
63
|
// src/functions/index.ts
|
|
56
64
|
var capitalizeFirstLetter = (str) => {
|
|
57
65
|
try {
|
|
58
|
-
if (
|
|
66
|
+
if (str == null || str === "")
|
|
59
67
|
return null;
|
|
60
|
-
|
|
68
|
+
const trimmed = str.trim();
|
|
69
|
+
if (trimmed.length === 0)
|
|
70
|
+
return "";
|
|
71
|
+
return trimmed.charAt(0).toUpperCase() + trimmed.slice(1);
|
|
61
72
|
} catch (error) {
|
|
62
73
|
console.error(error);
|
|
63
74
|
return null;
|
|
64
75
|
}
|
|
65
76
|
};
|
|
66
|
-
var titleCase = (str) => {
|
|
77
|
+
var titleCase = (str, separator = " ") => {
|
|
67
78
|
try {
|
|
68
|
-
if (
|
|
79
|
+
if (str == null || str === "")
|
|
69
80
|
return null;
|
|
70
|
-
|
|
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);
|
|
71
83
|
} catch (error) {
|
|
72
84
|
console.error(error);
|
|
73
85
|
return null;
|
|
@@ -77,28 +89,48 @@ var getInitials = (name) => {
|
|
|
77
89
|
try {
|
|
78
90
|
if (typeof name !== "string" || name.trim() === "")
|
|
79
91
|
return "?";
|
|
80
|
-
const words = name.trim().split(
|
|
81
|
-
|
|
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;
|
|
82
103
|
} catch (error) {
|
|
83
104
|
console.error(error);
|
|
105
|
+
return "?";
|
|
84
106
|
}
|
|
85
107
|
};
|
|
86
|
-
var getTruncateDescription = (text, maxLength =
|
|
108
|
+
var getTruncateDescription = (text, maxLength = 50) => {
|
|
87
109
|
try {
|
|
88
|
-
|
|
110
|
+
if (!text)
|
|
111
|
+
return "";
|
|
112
|
+
if (text.length <= maxLength)
|
|
113
|
+
return text;
|
|
114
|
+
return `${text.substring(0, maxLength)}...`;
|
|
89
115
|
} catch (error) {
|
|
90
116
|
console.error(error);
|
|
91
|
-
return
|
|
117
|
+
return "";
|
|
92
118
|
}
|
|
93
119
|
};
|
|
94
120
|
var compareArrays = (arr1, arr2) => {
|
|
95
121
|
try {
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
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]);
|
|
99
131
|
} catch (error) {
|
|
100
132
|
console.error(error);
|
|
101
|
-
return
|
|
133
|
+
return false;
|
|
102
134
|
}
|
|
103
135
|
};
|
|
104
136
|
var generateOTPWithLength = (length, isAlphaNumeric = false) => {
|
|
@@ -211,10 +243,12 @@ var validateText = (value, errorMessage = null) => {
|
|
|
211
243
|
};
|
|
212
244
|
var validateEmail = (email, errorMessage = null) => {
|
|
213
245
|
try {
|
|
214
|
-
if (
|
|
246
|
+
if (email == null || String(email).trim() === "") {
|
|
215
247
|
return { isError: true, errorMessage: errorMessage || "This field must not be empty." };
|
|
216
|
-
|
|
217
|
-
|
|
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)) {
|
|
218
252
|
return { isError: true, errorMessage: errorMessage || "Invalid email" };
|
|
219
253
|
}
|
|
220
254
|
return { isError: false, errorMessage: null };
|
|
@@ -239,10 +273,13 @@ var validatePassword = (password, errorMessage = null) => {
|
|
|
239
273
|
};
|
|
240
274
|
var validateMobileNumber = (value, errorMessage = null) => {
|
|
241
275
|
try {
|
|
242
|
-
|
|
276
|
+
const strValue = typeof value === "number" ? String(value) : value;
|
|
277
|
+
if (strValue == null || strValue.trim() === "") {
|
|
243
278
|
return { isError: true, errorMessage: errorMessage || "This field must not be empty." };
|
|
244
|
-
|
|
245
|
-
|
|
279
|
+
}
|
|
280
|
+
const trimmed = strValue.trim();
|
|
281
|
+
const regex = /^(?:\+?91[-\s]?)?\d{10}$/;
|
|
282
|
+
if (!regex.test(trimmed)) {
|
|
246
283
|
return { isError: true, errorMessage: errorMessage || "Invalid mobile number" };
|
|
247
284
|
}
|
|
248
285
|
return { isError: false, errorMessage: null };
|
|
@@ -295,44 +332,30 @@ function getValueByKey(obj, key) {
|
|
|
295
332
|
}
|
|
296
333
|
return null;
|
|
297
334
|
}
|
|
298
|
-
var sortArrayByDirection = (array, direction = "asc", key
|
|
335
|
+
var sortArrayByDirection = (array, direction = "asc", key) => {
|
|
299
336
|
try {
|
|
300
337
|
if (!array || array.length === 0)
|
|
301
|
-
return
|
|
338
|
+
return [];
|
|
302
339
|
if (direction !== "asc" && direction !== "desc") {
|
|
303
340
|
console.error('Invalid sorting direction. Please use "asc" or "desc".');
|
|
304
341
|
return array;
|
|
305
342
|
}
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
return direction === "asc" ? valueA.localeCompare(valueB) : valueB.localeCompare(valueA);
|
|
310
|
-
} else if (typeof valueA === "number" && typeof valueB === "number") {
|
|
311
|
-
return direction === "asc" ? valueA - valueB : valueB - valueA;
|
|
312
|
-
} else if (valueA instanceof Date && valueB instanceof Date) {
|
|
313
|
-
return direction === "asc" ? valueA.getTime() - valueB.getTime() : valueB.getTime() - valueA.getTime();
|
|
314
|
-
} else {
|
|
315
|
-
return 0;
|
|
316
|
-
}
|
|
317
|
-
});
|
|
318
|
-
}
|
|
319
|
-
let Array2 = [...array];
|
|
320
|
-
return Array2.sort((a, b) => {
|
|
321
|
-
const valueA = getValueByKey(a, key);
|
|
322
|
-
const valueB = getValueByKey(b, key);
|
|
343
|
+
const sorted = [...array].sort((a, b) => {
|
|
344
|
+
const valueA = key ? a[key] : a;
|
|
345
|
+
const valueB = key ? b[key] : b;
|
|
323
346
|
if (typeof valueA === "string" && typeof valueB === "string") {
|
|
324
347
|
return direction === "asc" ? valueA.localeCompare(valueB) : valueB.localeCompare(valueA);
|
|
325
348
|
} else if (typeof valueA === "number" && typeof valueB === "number") {
|
|
326
349
|
return direction === "asc" ? valueA - valueB : valueB - valueA;
|
|
327
350
|
} else if (valueA instanceof Date && valueB instanceof Date) {
|
|
328
351
|
return direction === "asc" ? valueA.getTime() - valueB.getTime() : valueB.getTime() - valueA.getTime();
|
|
329
|
-
} else {
|
|
330
|
-
return 0;
|
|
331
352
|
}
|
|
353
|
+
return 0;
|
|
332
354
|
});
|
|
355
|
+
return sorted;
|
|
333
356
|
} catch (error) {
|
|
334
357
|
console.error(error);
|
|
335
|
-
return
|
|
358
|
+
return array;
|
|
336
359
|
}
|
|
337
360
|
};
|
|
338
361
|
var reverseString = (str) => {
|
|
@@ -346,13 +369,15 @@ var reverseString = (str) => {
|
|
|
346
369
|
}
|
|
347
370
|
};
|
|
348
371
|
var isObjectValuesEmpty = (object) => {
|
|
349
|
-
|
|
350
|
-
if (
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
return
|
|
354
|
-
}
|
|
355
|
-
|
|
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
|
+
}
|
|
356
381
|
};
|
|
357
382
|
var checkNotNullAndNotEmpty = (value) => {
|
|
358
383
|
try {
|
|
@@ -382,6 +407,123 @@ var isFalsy = (value) => {
|
|
|
382
407
|
var isTruthy = (value) => {
|
|
383
408
|
return !!value;
|
|
384
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
|
+
};
|
|
385
527
|
var groupBy = (arr, key) => {
|
|
386
528
|
try {
|
|
387
529
|
if (!arr)
|
|
@@ -401,11 +543,17 @@ var groupBy = (arr, key) => {
|
|
|
401
543
|
// Annotate the CommonJS export names for ESM import in node:
|
|
402
544
|
0 && (module.exports = {
|
|
403
545
|
CURRENCYCODE,
|
|
546
|
+
camelCase,
|
|
547
|
+
capitalizeEveryWord,
|
|
404
548
|
capitalizeFirstLetter,
|
|
405
549
|
checkNotNullAndNotEmpty,
|
|
406
550
|
clearLocalStorage,
|
|
407
551
|
compareArrays,
|
|
552
|
+
debounce,
|
|
553
|
+
deepClone,
|
|
408
554
|
formatCurrency,
|
|
555
|
+
formatDate,
|
|
556
|
+
formatDateRelative,
|
|
409
557
|
generateOTPWithLength,
|
|
410
558
|
generateRandomPassword,
|
|
411
559
|
getGreeting,
|
|
@@ -418,6 +566,7 @@ var groupBy = (arr, key) => {
|
|
|
418
566
|
isFalsy,
|
|
419
567
|
isObjectValuesEmpty,
|
|
420
568
|
isTruthy,
|
|
569
|
+
pascalCase,
|
|
421
570
|
removeDuplicatesFromArray,
|
|
422
571
|
removeLocalStorage,
|
|
423
572
|
reverseString,
|
|
@@ -425,6 +574,7 @@ var groupBy = (arr, key) => {
|
|
|
425
574
|
slugify,
|
|
426
575
|
sortArrayByDirection,
|
|
427
576
|
titleCase,
|
|
577
|
+
truncate,
|
|
428
578
|
validateEmail,
|
|
429
579
|
validateMobileNumber,
|
|
430
580
|
validatePassword,
|
package/dist/index.d.cts
CHANGED
|
@@ -7,10 +7,10 @@ type ObjectType = object;
|
|
|
7
7
|
type BooleanType = boolean;
|
|
8
8
|
|
|
9
9
|
declare const capitalizeFirstLetter: (str: NullableString) => NullableString;
|
|
10
|
-
declare const titleCase: (str: NullableString) => NullableString;
|
|
11
|
-
declare const getInitials: (name: NullableString) => string
|
|
12
|
-
declare const getTruncateDescription: (text: NullableString, maxLength?: number) => string
|
|
13
|
-
declare const compareArrays: <T>(arr1: ArrayType<T>, arr2: ArrayType<T>) => boolean
|
|
10
|
+
declare const titleCase: (str: NullableString, separator?: string) => NullableString;
|
|
11
|
+
declare const getInitials: (name: NullableString) => string;
|
|
12
|
+
declare const getTruncateDescription: (text: NullableString, maxLength?: number) => string;
|
|
13
|
+
declare const compareArrays: <T>(arr1: ArrayType<T>, arr2: ArrayType<T>) => boolean;
|
|
14
14
|
declare const generateOTPWithLength: (length: NumberType, isAlphaNumeric?: boolean) => string | number | undefined;
|
|
15
15
|
declare const generateRandomPassword: (length?: number) => string | null;
|
|
16
16
|
declare const slugify: (string: StringType, separator?: string) => string | null;
|
|
@@ -85,13 +85,24 @@ declare const CURRENCYCODE: {
|
|
|
85
85
|
declare const formatCurrency: (number: NumberType, currencyCode?: string, minimumFractionDigits?: number, maximumFractionDigits?: number) => string | null;
|
|
86
86
|
declare const removeDuplicatesFromArray: (array: AnyType) => unknown[] | null;
|
|
87
87
|
declare function getValueByKey(obj: AnyType, key: StringType): any;
|
|
88
|
-
declare const sortArrayByDirection: (array:
|
|
88
|
+
declare const sortArrayByDirection: <T>(array: T[], direction?: 'asc' | 'desc', key?: keyof T) => T[];
|
|
89
89
|
declare const reverseString: (str: NullableString) => string | null;
|
|
90
|
-
declare const isObjectValuesEmpty: (object: AnyType) =>
|
|
90
|
+
declare const isObjectValuesEmpty: (object: AnyType) => {
|
|
91
|
+
isEmpty: boolean;
|
|
92
|
+
emptyKeys: string[];
|
|
93
|
+
};
|
|
91
94
|
declare const checkNotNullAndNotEmpty: (value: AnyType) => boolean;
|
|
92
95
|
declare const handleNaN: (value: AnyType, returnValue?: number) => any;
|
|
93
96
|
declare const isFalsy: (value: BooleanType) => boolean;
|
|
94
97
|
declare const isTruthy: (value: BooleanType) => boolean;
|
|
98
|
+
declare const capitalizeEveryWord: (str: StringType) => StringType;
|
|
99
|
+
declare const camelCase: (str: StringType) => StringType;
|
|
100
|
+
declare const pascalCase: (str: StringType) => StringType;
|
|
101
|
+
declare const truncate: (str: StringType, maxLength?: number) => StringType;
|
|
102
|
+
declare const formatDate: (date: Date | string | number, format?: string) => StringType;
|
|
103
|
+
declare const deepClone: <T>(obj: T) => T;
|
|
104
|
+
declare const debounce: <F extends (...args: any[]) => any>(func: F, wait: number) => ((...args: Parameters<F>) => void);
|
|
105
|
+
declare const formatDateRelative: (date: Date | string | number) => StringType;
|
|
95
106
|
declare const groupBy: (arr: AnyType, key: StringType) => any;
|
|
96
107
|
|
|
97
|
-
export { type AnyType, type ArrayType, type BooleanType, CURRENCYCODE, type NullableString, type NumberType, type ObjectType, type StringType, capitalizeFirstLetter, checkNotNullAndNotEmpty, clearLocalStorage, compareArrays, formatCurrency, generateOTPWithLength, generateRandomPassword, getGreeting, getInitials, getLocalStorage, getTruncateDescription, getValueByKey, groupBy, handleNaN, isFalsy, isObjectValuesEmpty, isTruthy, removeDuplicatesFromArray, removeLocalStorage, reverseString, setLocalStorage, slugify, sortArrayByDirection, titleCase, validateEmail, validateMobileNumber, validatePassword, validateText };
|
|
108
|
+
export { type AnyType, type ArrayType, type BooleanType, CURRENCYCODE, type NullableString, type NumberType, type ObjectType, type StringType, camelCase, capitalizeEveryWord, capitalizeFirstLetter, checkNotNullAndNotEmpty, clearLocalStorage, compareArrays, debounce, deepClone, formatCurrency, formatDate, formatDateRelative, generateOTPWithLength, generateRandomPassword, getGreeting, getInitials, getLocalStorage, getTruncateDescription, getValueByKey, groupBy, handleNaN, isFalsy, isObjectValuesEmpty, isTruthy, pascalCase, removeDuplicatesFromArray, removeLocalStorage, reverseString, setLocalStorage, slugify, sortArrayByDirection, titleCase, truncate, validateEmail, validateMobileNumber, validatePassword, validateText };
|
package/dist/index.d.ts
CHANGED
|
@@ -7,10 +7,10 @@ type ObjectType = object;
|
|
|
7
7
|
type BooleanType = boolean;
|
|
8
8
|
|
|
9
9
|
declare const capitalizeFirstLetter: (str: NullableString) => NullableString;
|
|
10
|
-
declare const titleCase: (str: NullableString) => NullableString;
|
|
11
|
-
declare const getInitials: (name: NullableString) => string
|
|
12
|
-
declare const getTruncateDescription: (text: NullableString, maxLength?: number) => string
|
|
13
|
-
declare const compareArrays: <T>(arr1: ArrayType<T>, arr2: ArrayType<T>) => boolean
|
|
10
|
+
declare const titleCase: (str: NullableString, separator?: string) => NullableString;
|
|
11
|
+
declare const getInitials: (name: NullableString) => string;
|
|
12
|
+
declare const getTruncateDescription: (text: NullableString, maxLength?: number) => string;
|
|
13
|
+
declare const compareArrays: <T>(arr1: ArrayType<T>, arr2: ArrayType<T>) => boolean;
|
|
14
14
|
declare const generateOTPWithLength: (length: NumberType, isAlphaNumeric?: boolean) => string | number | undefined;
|
|
15
15
|
declare const generateRandomPassword: (length?: number) => string | null;
|
|
16
16
|
declare const slugify: (string: StringType, separator?: string) => string | null;
|
|
@@ -85,13 +85,24 @@ declare const CURRENCYCODE: {
|
|
|
85
85
|
declare const formatCurrency: (number: NumberType, currencyCode?: string, minimumFractionDigits?: number, maximumFractionDigits?: number) => string | null;
|
|
86
86
|
declare const removeDuplicatesFromArray: (array: AnyType) => unknown[] | null;
|
|
87
87
|
declare function getValueByKey(obj: AnyType, key: StringType): any;
|
|
88
|
-
declare const sortArrayByDirection: (array:
|
|
88
|
+
declare const sortArrayByDirection: <T>(array: T[], direction?: 'asc' | 'desc', key?: keyof T) => T[];
|
|
89
89
|
declare const reverseString: (str: NullableString) => string | null;
|
|
90
|
-
declare const isObjectValuesEmpty: (object: AnyType) =>
|
|
90
|
+
declare const isObjectValuesEmpty: (object: AnyType) => {
|
|
91
|
+
isEmpty: boolean;
|
|
92
|
+
emptyKeys: string[];
|
|
93
|
+
};
|
|
91
94
|
declare const checkNotNullAndNotEmpty: (value: AnyType) => boolean;
|
|
92
95
|
declare const handleNaN: (value: AnyType, returnValue?: number) => any;
|
|
93
96
|
declare const isFalsy: (value: BooleanType) => boolean;
|
|
94
97
|
declare const isTruthy: (value: BooleanType) => boolean;
|
|
98
|
+
declare const capitalizeEveryWord: (str: StringType) => StringType;
|
|
99
|
+
declare const camelCase: (str: StringType) => StringType;
|
|
100
|
+
declare const pascalCase: (str: StringType) => StringType;
|
|
101
|
+
declare const truncate: (str: StringType, maxLength?: number) => StringType;
|
|
102
|
+
declare const formatDate: (date: Date | string | number, format?: string) => StringType;
|
|
103
|
+
declare const deepClone: <T>(obj: T) => T;
|
|
104
|
+
declare const debounce: <F extends (...args: any[]) => any>(func: F, wait: number) => ((...args: Parameters<F>) => void);
|
|
105
|
+
declare const formatDateRelative: (date: Date | string | number) => StringType;
|
|
95
106
|
declare const groupBy: (arr: AnyType, key: StringType) => any;
|
|
96
107
|
|
|
97
|
-
export { type AnyType, type ArrayType, type BooleanType, CURRENCYCODE, type NullableString, type NumberType, type ObjectType, type StringType, capitalizeFirstLetter, checkNotNullAndNotEmpty, clearLocalStorage, compareArrays, formatCurrency, generateOTPWithLength, generateRandomPassword, getGreeting, getInitials, getLocalStorage, getTruncateDescription, getValueByKey, groupBy, handleNaN, isFalsy, isObjectValuesEmpty, isTruthy, removeDuplicatesFromArray, removeLocalStorage, reverseString, setLocalStorage, slugify, sortArrayByDirection, titleCase, validateEmail, validateMobileNumber, validatePassword, validateText };
|
|
108
|
+
export { type AnyType, type ArrayType, type BooleanType, CURRENCYCODE, type NullableString, type NumberType, type ObjectType, type StringType, camelCase, capitalizeEveryWord, capitalizeFirstLetter, checkNotNullAndNotEmpty, clearLocalStorage, compareArrays, debounce, deepClone, formatCurrency, formatDate, formatDateRelative, generateOTPWithLength, generateRandomPassword, getGreeting, getInitials, getLocalStorage, getTruncateDescription, getValueByKey, groupBy, handleNaN, isFalsy, isObjectValuesEmpty, isTruthy, pascalCase, removeDuplicatesFromArray, removeLocalStorage, reverseString, setLocalStorage, slugify, sortArrayByDirection, titleCase, truncate, validateEmail, validateMobileNumber, validatePassword, validateText };
|