dhavaltank-js-utils 0.0.3 → 0.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs +59 -26
- package/dist/index.d.cts +32 -4
- package/dist/index.d.ts +32 -4
- package/dist/index.js +56 -25
- package/package.json +1 -2
package/dist/index.cjs
CHANGED
|
@@ -20,6 +20,7 @@ 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,
|
|
23
24
|
capitalizeFirstLetter: () => capitalizeFirstLetter,
|
|
24
25
|
checkNotNullAndNotEmpty: () => checkNotNullAndNotEmpty,
|
|
25
26
|
clearLocalStorage: () => clearLocalStorage,
|
|
@@ -31,6 +32,7 @@ __export(src_exports, {
|
|
|
31
32
|
getInitials: () => getInitials,
|
|
32
33
|
getLocalStorage: () => getLocalStorage,
|
|
33
34
|
getTruncateDescription: () => getTruncateDescription,
|
|
35
|
+
getValueByKey: () => getValueByKey,
|
|
34
36
|
groupBy: () => groupBy,
|
|
35
37
|
handleNaN: () => handleNaN,
|
|
36
38
|
isFalsy: () => isFalsy,
|
|
@@ -41,7 +43,7 @@ __export(src_exports, {
|
|
|
41
43
|
reverseString: () => reverseString,
|
|
42
44
|
setLocalStorage: () => setLocalStorage,
|
|
43
45
|
slugify: () => slugify,
|
|
44
|
-
|
|
46
|
+
sortArrayByDirection: () => sortArrayByDirection,
|
|
45
47
|
titleCase: () => titleCase,
|
|
46
48
|
validateEmail: () => validateEmail,
|
|
47
49
|
validateMobileNumber: () => validateMobileNumber,
|
|
@@ -61,12 +63,11 @@ var capitalizeFirstLetter = (str) => {
|
|
|
61
63
|
return null;
|
|
62
64
|
}
|
|
63
65
|
};
|
|
64
|
-
var titleCase = (str
|
|
66
|
+
var titleCase = (str) => {
|
|
65
67
|
try {
|
|
66
68
|
if (!str)
|
|
67
69
|
return null;
|
|
68
|
-
|
|
69
|
-
return key === "And" ? text.replace("And", "&") : text;
|
|
70
|
+
return str.replace(/_/g, " ").split(" ").map((word) => word.charAt(0).toUpperCase() + word.slice(1)).join(" ");
|
|
70
71
|
} catch (error) {
|
|
71
72
|
console.error(error);
|
|
72
73
|
return null;
|
|
@@ -76,7 +77,7 @@ var getInitials = (name) => {
|
|
|
76
77
|
try {
|
|
77
78
|
if (typeof name !== "string" || name.trim() === "")
|
|
78
79
|
return "?";
|
|
79
|
-
const words = name.trim().split(" ").map((latter) => latter[0]
|
|
80
|
+
const words = name.trim().split(" ").filter((value) => !!value)?.map((latter) => latter[0]?.toUpperCase()).join("");
|
|
80
81
|
return words.length > 1 ? words[0] + words[words.length - 1] : words;
|
|
81
82
|
} catch (error) {
|
|
82
83
|
console.error(error);
|
|
@@ -92,7 +93,9 @@ var getTruncateDescription = (text, maxLength = 10) => {
|
|
|
92
93
|
};
|
|
93
94
|
var compareArrays = (arr1, arr2) => {
|
|
94
95
|
try {
|
|
95
|
-
|
|
96
|
+
let array1 = arr1?.flat(10)?.slice()?.sort();
|
|
97
|
+
let array2 = arr2?.flat(10)?.slice()?.sort();
|
|
98
|
+
return array1?.length === array2?.length && array1?.every((value, index) => value === array2[index]);
|
|
96
99
|
} catch (error) {
|
|
97
100
|
console.error(error);
|
|
98
101
|
return null;
|
|
@@ -132,7 +135,7 @@ var generateRandomPassword = (length = 8) => {
|
|
|
132
135
|
};
|
|
133
136
|
var slugify = (string, separator = "-") => {
|
|
134
137
|
try {
|
|
135
|
-
return string
|
|
138
|
+
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
139
|
} catch (error) {
|
|
137
140
|
console.error(error);
|
|
138
141
|
return null;
|
|
@@ -194,13 +197,13 @@ var getGreeting = () => {
|
|
|
194
197
|
};
|
|
195
198
|
var validateText = (value, errorMessage = null) => {
|
|
196
199
|
try {
|
|
197
|
-
if (!value
|
|
200
|
+
if (!value)
|
|
198
201
|
return { isError: true, errorMessage: errorMessage || "This field must not be empty." };
|
|
199
202
|
const regex = /^[a-zA-Z ]+$/;
|
|
200
203
|
if (!regex.test(value?.trim())) {
|
|
201
204
|
return { isError: true, errorMessage: errorMessage || "Invalid input" };
|
|
202
205
|
}
|
|
203
|
-
return { isError: false, errorMessage:
|
|
206
|
+
return { isError: false, errorMessage: null };
|
|
204
207
|
} catch (error) {
|
|
205
208
|
console.error(error);
|
|
206
209
|
return { isError: true, message: "Something went wrong." };
|
|
@@ -214,7 +217,7 @@ var validateEmail = (email, errorMessage = null) => {
|
|
|
214
217
|
if (!re.test(email?.trim())) {
|
|
215
218
|
return { isError: true, errorMessage: errorMessage || "Invalid email" };
|
|
216
219
|
}
|
|
217
|
-
return { isError: false, errorMessage:
|
|
220
|
+
return { isError: false, errorMessage: null };
|
|
218
221
|
} catch (error) {
|
|
219
222
|
console.error(error);
|
|
220
223
|
return { isError: true, message: "Something went wrong." };
|
|
@@ -228,7 +231,7 @@ var validatePassword = (password, errorMessage = null) => {
|
|
|
228
231
|
if (!regex.test(password?.trim())) {
|
|
229
232
|
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
233
|
}
|
|
231
|
-
return { isError: false, errorMessage:
|
|
234
|
+
return { isError: false, errorMessage: null };
|
|
232
235
|
} catch (error) {
|
|
233
236
|
console.error(error);
|
|
234
237
|
return { isError: true, message: "Something went wrong." };
|
|
@@ -242,13 +245,13 @@ var validateMobileNumber = (value, errorMessage = null) => {
|
|
|
242
245
|
if (!regex.test(value?.trim())) {
|
|
243
246
|
return { isError: true, errorMessage: errorMessage || "Invalid mobile number" };
|
|
244
247
|
}
|
|
245
|
-
return { isError: false, errorMessage:
|
|
248
|
+
return { isError: false, errorMessage: null };
|
|
246
249
|
} catch (error) {
|
|
247
250
|
console.error(error);
|
|
248
251
|
return { isError: true, message: "Something went wrong." };
|
|
249
252
|
}
|
|
250
253
|
};
|
|
251
|
-
var
|
|
254
|
+
var CURRENCYCODE = {
|
|
252
255
|
USD: "USD",
|
|
253
256
|
EUR: "EUR",
|
|
254
257
|
GBP: "GBP",
|
|
@@ -259,13 +262,13 @@ var CurrencyCode = {
|
|
|
259
262
|
CHF: "CHF",
|
|
260
263
|
CNY: "CNY"
|
|
261
264
|
};
|
|
262
|
-
var formatCurrency = (number, currencyCode =
|
|
265
|
+
var formatCurrency = (number, currencyCode = CURRENCYCODE.USD, minimumFractionDigits = 2, maximumFractionDigits = 2) => {
|
|
263
266
|
try {
|
|
264
267
|
if (!number)
|
|
265
268
|
return null;
|
|
266
|
-
if (!Object.values(
|
|
269
|
+
if (!Object.values(CURRENCYCODE).includes(currencyCode)) {
|
|
267
270
|
console.warn(`Invalid currency code "${currencyCode}". Defaulting to USD.`);
|
|
268
|
-
currencyCode =
|
|
271
|
+
currencyCode = CURRENCYCODE.USD;
|
|
269
272
|
}
|
|
270
273
|
return number?.toLocaleString("en-US", {
|
|
271
274
|
style: "currency",
|
|
@@ -286,18 +289,46 @@ var removeDuplicatesFromArray = (array) => {
|
|
|
286
289
|
return null;
|
|
287
290
|
}
|
|
288
291
|
};
|
|
289
|
-
|
|
292
|
+
function getValueByKey(obj, key) {
|
|
293
|
+
if (obj && typeof obj === "object" && key in obj) {
|
|
294
|
+
return obj[key];
|
|
295
|
+
}
|
|
296
|
+
return null;
|
|
297
|
+
}
|
|
298
|
+
var sortArrayByDirection = (array, direction = "asc", key = "") => {
|
|
290
299
|
try {
|
|
291
300
|
if (!array || array.length === 0)
|
|
292
301
|
return null;
|
|
293
|
-
if (
|
|
294
|
-
console.error('Invalid sorting
|
|
302
|
+
if (direction !== "asc" && direction !== "desc") {
|
|
303
|
+
console.error('Invalid sorting direction. Please use "asc" or "desc".');
|
|
295
304
|
return array;
|
|
296
305
|
}
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
306
|
+
if (!key) {
|
|
307
|
+
return [...array].sort((valueA, valueB) => {
|
|
308
|
+
if (typeof valueA === "string" && typeof valueB === "string") {
|
|
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);
|
|
323
|
+
if (typeof valueA === "string" && typeof valueB === "string") {
|
|
324
|
+
return direction === "asc" ? valueA.localeCompare(valueB) : valueB.localeCompare(valueA);
|
|
325
|
+
} else if (typeof valueA === "number" && typeof valueB === "number") {
|
|
326
|
+
return direction === "asc" ? valueA - valueB : valueB - valueA;
|
|
327
|
+
} else if (valueA instanceof Date && valueB instanceof Date) {
|
|
328
|
+
return direction === "asc" ? valueA.getTime() - valueB.getTime() : valueB.getTime() - valueA.getTime();
|
|
329
|
+
} else {
|
|
330
|
+
return 0;
|
|
331
|
+
}
|
|
301
332
|
});
|
|
302
333
|
} catch (error) {
|
|
303
334
|
console.error(error);
|
|
@@ -308,7 +339,7 @@ var reverseString = (str) => {
|
|
|
308
339
|
try {
|
|
309
340
|
if (!str)
|
|
310
341
|
return null;
|
|
311
|
-
return str?.split("")
|
|
342
|
+
return str?.split("")?.reverse()?.join("");
|
|
312
343
|
} catch (error) {
|
|
313
344
|
console.error(error);
|
|
314
345
|
return null;
|
|
@@ -321,7 +352,7 @@ var isObjectValuesEmpty = (object) => {
|
|
|
321
352
|
}
|
|
322
353
|
return acc;
|
|
323
354
|
}, {});
|
|
324
|
-
return Object.keys(errors).length > 0;
|
|
355
|
+
return errors && Object.keys(errors).length > 0;
|
|
325
356
|
};
|
|
326
357
|
var checkNotNullAndNotEmpty = (value) => {
|
|
327
358
|
try {
|
|
@@ -369,6 +400,7 @@ var groupBy = (arr, key) => {
|
|
|
369
400
|
};
|
|
370
401
|
// Annotate the CommonJS export names for ESM import in node:
|
|
371
402
|
0 && (module.exports = {
|
|
403
|
+
CURRENCYCODE,
|
|
372
404
|
capitalizeFirstLetter,
|
|
373
405
|
checkNotNullAndNotEmpty,
|
|
374
406
|
clearLocalStorage,
|
|
@@ -380,6 +412,7 @@ var groupBy = (arr, key) => {
|
|
|
380
412
|
getInitials,
|
|
381
413
|
getLocalStorage,
|
|
382
414
|
getTruncateDescription,
|
|
415
|
+
getValueByKey,
|
|
383
416
|
groupBy,
|
|
384
417
|
handleNaN,
|
|
385
418
|
isFalsy,
|
|
@@ -390,7 +423,7 @@ var groupBy = (arr, key) => {
|
|
|
390
423
|
reverseString,
|
|
391
424
|
setLocalStorage,
|
|
392
425
|
slugify,
|
|
393
|
-
|
|
426
|
+
sortArrayByDirection,
|
|
394
427
|
titleCase,
|
|
395
428
|
validateEmail,
|
|
396
429
|
validateMobileNumber,
|
package/dist/index.d.cts
CHANGED
|
@@ -7,7 +7,7 @@ type ObjectType = object;
|
|
|
7
7
|
type BooleanType = boolean;
|
|
8
8
|
|
|
9
9
|
declare const capitalizeFirstLetter: (str: NullableString) => NullableString;
|
|
10
|
-
declare const titleCase: (str: NullableString
|
|
10
|
+
declare const titleCase: (str: NullableString) => NullableString;
|
|
11
11
|
declare const getInitials: (name: NullableString) => string | undefined;
|
|
12
12
|
declare const getTruncateDescription: (text: NullableString, maxLength?: number) => string | null;
|
|
13
13
|
declare const compareArrays: <T>(arr1: ArrayType<T>, arr2: ArrayType<T>) => boolean | null;
|
|
@@ -23,6 +23,10 @@ declare const validateText: (value: NullableString, errorMessage?: null) => {
|
|
|
23
23
|
isError: boolean;
|
|
24
24
|
errorMessage: string;
|
|
25
25
|
message?: undefined;
|
|
26
|
+
} | {
|
|
27
|
+
isError: boolean;
|
|
28
|
+
errorMessage: null;
|
|
29
|
+
message?: undefined;
|
|
26
30
|
} | {
|
|
27
31
|
isError: boolean;
|
|
28
32
|
message: string;
|
|
@@ -32,6 +36,10 @@ declare const validateEmail: (email: NullableString, errorMessage?: null) => {
|
|
|
32
36
|
isError: boolean;
|
|
33
37
|
errorMessage: string;
|
|
34
38
|
message?: undefined;
|
|
39
|
+
} | {
|
|
40
|
+
isError: boolean;
|
|
41
|
+
errorMessage: null;
|
|
42
|
+
message?: undefined;
|
|
35
43
|
} | {
|
|
36
44
|
isError: boolean;
|
|
37
45
|
message: string;
|
|
@@ -41,6 +49,10 @@ declare const validatePassword: (password: NullableString, errorMessage?: null)
|
|
|
41
49
|
isError: boolean;
|
|
42
50
|
errorMessage: string;
|
|
43
51
|
message?: undefined;
|
|
52
|
+
} | {
|
|
53
|
+
isError: boolean;
|
|
54
|
+
errorMessage: null;
|
|
55
|
+
message?: undefined;
|
|
44
56
|
} | {
|
|
45
57
|
isError: boolean;
|
|
46
58
|
message: string;
|
|
@@ -50,20 +62,36 @@ declare const validateMobileNumber: (value: NullableString, errorMessage?: null)
|
|
|
50
62
|
isError: boolean;
|
|
51
63
|
errorMessage: string;
|
|
52
64
|
message?: undefined;
|
|
65
|
+
} | {
|
|
66
|
+
isError: boolean;
|
|
67
|
+
errorMessage: null;
|
|
68
|
+
message?: undefined;
|
|
53
69
|
} | {
|
|
54
70
|
isError: boolean;
|
|
55
71
|
message: string;
|
|
56
72
|
errorMessage?: undefined;
|
|
57
73
|
};
|
|
74
|
+
declare const CURRENCYCODE: {
|
|
75
|
+
USD: string;
|
|
76
|
+
EUR: string;
|
|
77
|
+
GBP: string;
|
|
78
|
+
INR: string;
|
|
79
|
+
JPY: string;
|
|
80
|
+
AUD: string;
|
|
81
|
+
CAD: string;
|
|
82
|
+
CHF: string;
|
|
83
|
+
CNY: string;
|
|
84
|
+
};
|
|
58
85
|
declare const formatCurrency: (number: NumberType, currencyCode?: string, minimumFractionDigits?: number, maximumFractionDigits?: number) => string | null;
|
|
59
86
|
declare const removeDuplicatesFromArray: (array: AnyType) => unknown[] | null;
|
|
60
|
-
declare
|
|
87
|
+
declare function getValueByKey(obj: AnyType, key: StringType): any;
|
|
88
|
+
declare const sortArrayByDirection: (array: AnyType, direction?: string, key?: string) => any;
|
|
61
89
|
declare const reverseString: (str: NullableString) => string | null;
|
|
62
|
-
declare const isObjectValuesEmpty: (object: AnyType) =>
|
|
90
|
+
declare const isObjectValuesEmpty: (object: AnyType) => any;
|
|
63
91
|
declare const checkNotNullAndNotEmpty: (value: AnyType) => boolean;
|
|
64
92
|
declare const handleNaN: (value: AnyType, returnValue?: number) => any;
|
|
65
93
|
declare const isFalsy: (value: BooleanType) => boolean;
|
|
66
94
|
declare const isTruthy: (value: BooleanType) => boolean;
|
|
67
95
|
declare const groupBy: (arr: AnyType, key: StringType) => any;
|
|
68
96
|
|
|
69
|
-
export { type AnyType, type ArrayType, type BooleanType, type NullableString, type NumberType, type ObjectType, type StringType, capitalizeFirstLetter, checkNotNullAndNotEmpty, clearLocalStorage, compareArrays, formatCurrency, generateOTPWithLength, generateRandomPassword, getGreeting, getInitials, getLocalStorage, getTruncateDescription, groupBy, handleNaN, isFalsy, isObjectValuesEmpty, isTruthy, removeDuplicatesFromArray, removeLocalStorage, reverseString, setLocalStorage, slugify,
|
|
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 };
|
package/dist/index.d.ts
CHANGED
|
@@ -7,7 +7,7 @@ type ObjectType = object;
|
|
|
7
7
|
type BooleanType = boolean;
|
|
8
8
|
|
|
9
9
|
declare const capitalizeFirstLetter: (str: NullableString) => NullableString;
|
|
10
|
-
declare const titleCase: (str: NullableString
|
|
10
|
+
declare const titleCase: (str: NullableString) => NullableString;
|
|
11
11
|
declare const getInitials: (name: NullableString) => string | undefined;
|
|
12
12
|
declare const getTruncateDescription: (text: NullableString, maxLength?: number) => string | null;
|
|
13
13
|
declare const compareArrays: <T>(arr1: ArrayType<T>, arr2: ArrayType<T>) => boolean | null;
|
|
@@ -23,6 +23,10 @@ declare const validateText: (value: NullableString, errorMessage?: null) => {
|
|
|
23
23
|
isError: boolean;
|
|
24
24
|
errorMessage: string;
|
|
25
25
|
message?: undefined;
|
|
26
|
+
} | {
|
|
27
|
+
isError: boolean;
|
|
28
|
+
errorMessage: null;
|
|
29
|
+
message?: undefined;
|
|
26
30
|
} | {
|
|
27
31
|
isError: boolean;
|
|
28
32
|
message: string;
|
|
@@ -32,6 +36,10 @@ declare const validateEmail: (email: NullableString, errorMessage?: null) => {
|
|
|
32
36
|
isError: boolean;
|
|
33
37
|
errorMessage: string;
|
|
34
38
|
message?: undefined;
|
|
39
|
+
} | {
|
|
40
|
+
isError: boolean;
|
|
41
|
+
errorMessage: null;
|
|
42
|
+
message?: undefined;
|
|
35
43
|
} | {
|
|
36
44
|
isError: boolean;
|
|
37
45
|
message: string;
|
|
@@ -41,6 +49,10 @@ declare const validatePassword: (password: NullableString, errorMessage?: null)
|
|
|
41
49
|
isError: boolean;
|
|
42
50
|
errorMessage: string;
|
|
43
51
|
message?: undefined;
|
|
52
|
+
} | {
|
|
53
|
+
isError: boolean;
|
|
54
|
+
errorMessage: null;
|
|
55
|
+
message?: undefined;
|
|
44
56
|
} | {
|
|
45
57
|
isError: boolean;
|
|
46
58
|
message: string;
|
|
@@ -50,20 +62,36 @@ declare const validateMobileNumber: (value: NullableString, errorMessage?: null)
|
|
|
50
62
|
isError: boolean;
|
|
51
63
|
errorMessage: string;
|
|
52
64
|
message?: undefined;
|
|
65
|
+
} | {
|
|
66
|
+
isError: boolean;
|
|
67
|
+
errorMessage: null;
|
|
68
|
+
message?: undefined;
|
|
53
69
|
} | {
|
|
54
70
|
isError: boolean;
|
|
55
71
|
message: string;
|
|
56
72
|
errorMessage?: undefined;
|
|
57
73
|
};
|
|
74
|
+
declare const CURRENCYCODE: {
|
|
75
|
+
USD: string;
|
|
76
|
+
EUR: string;
|
|
77
|
+
GBP: string;
|
|
78
|
+
INR: string;
|
|
79
|
+
JPY: string;
|
|
80
|
+
AUD: string;
|
|
81
|
+
CAD: string;
|
|
82
|
+
CHF: string;
|
|
83
|
+
CNY: string;
|
|
84
|
+
};
|
|
58
85
|
declare const formatCurrency: (number: NumberType, currencyCode?: string, minimumFractionDigits?: number, maximumFractionDigits?: number) => string | null;
|
|
59
86
|
declare const removeDuplicatesFromArray: (array: AnyType) => unknown[] | null;
|
|
60
|
-
declare
|
|
87
|
+
declare function getValueByKey(obj: AnyType, key: StringType): any;
|
|
88
|
+
declare const sortArrayByDirection: (array: AnyType, direction?: string, key?: string) => any;
|
|
61
89
|
declare const reverseString: (str: NullableString) => string | null;
|
|
62
|
-
declare const isObjectValuesEmpty: (object: AnyType) =>
|
|
90
|
+
declare const isObjectValuesEmpty: (object: AnyType) => any;
|
|
63
91
|
declare const checkNotNullAndNotEmpty: (value: AnyType) => boolean;
|
|
64
92
|
declare const handleNaN: (value: AnyType, returnValue?: number) => any;
|
|
65
93
|
declare const isFalsy: (value: BooleanType) => boolean;
|
|
66
94
|
declare const isTruthy: (value: BooleanType) => boolean;
|
|
67
95
|
declare const groupBy: (arr: AnyType, key: StringType) => any;
|
|
68
96
|
|
|
69
|
-
export { type AnyType, type ArrayType, type BooleanType, type NullableString, type NumberType, type ObjectType, type StringType, capitalizeFirstLetter, checkNotNullAndNotEmpty, clearLocalStorage, compareArrays, formatCurrency, generateOTPWithLength, generateRandomPassword, getGreeting, getInitials, getLocalStorage, getTruncateDescription, groupBy, handleNaN, isFalsy, isObjectValuesEmpty, isTruthy, removeDuplicatesFromArray, removeLocalStorage, reverseString, setLocalStorage, slugify,
|
|
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 };
|
package/dist/index.js
CHANGED
|
@@ -9,12 +9,11 @@ var capitalizeFirstLetter = (str) => {
|
|
|
9
9
|
return null;
|
|
10
10
|
}
|
|
11
11
|
};
|
|
12
|
-
var titleCase = (str
|
|
12
|
+
var titleCase = (str) => {
|
|
13
13
|
try {
|
|
14
14
|
if (!str)
|
|
15
15
|
return null;
|
|
16
|
-
|
|
17
|
-
return key === "And" ? text.replace("And", "&") : text;
|
|
16
|
+
return str.replace(/_/g, " ").split(" ").map((word) => word.charAt(0).toUpperCase() + word.slice(1)).join(" ");
|
|
18
17
|
} catch (error) {
|
|
19
18
|
console.error(error);
|
|
20
19
|
return null;
|
|
@@ -24,7 +23,7 @@ var getInitials = (name) => {
|
|
|
24
23
|
try {
|
|
25
24
|
if (typeof name !== "string" || name.trim() === "")
|
|
26
25
|
return "?";
|
|
27
|
-
const words = name.trim().split(" ").map((latter) => latter[0]
|
|
26
|
+
const words = name.trim().split(" ").filter((value) => !!value)?.map((latter) => latter[0]?.toUpperCase()).join("");
|
|
28
27
|
return words.length > 1 ? words[0] + words[words.length - 1] : words;
|
|
29
28
|
} catch (error) {
|
|
30
29
|
console.error(error);
|
|
@@ -40,7 +39,9 @@ var getTruncateDescription = (text, maxLength = 10) => {
|
|
|
40
39
|
};
|
|
41
40
|
var compareArrays = (arr1, arr2) => {
|
|
42
41
|
try {
|
|
43
|
-
|
|
42
|
+
let array1 = arr1?.flat(10)?.slice()?.sort();
|
|
43
|
+
let array2 = arr2?.flat(10)?.slice()?.sort();
|
|
44
|
+
return array1?.length === array2?.length && array1?.every((value, index) => value === array2[index]);
|
|
44
45
|
} catch (error) {
|
|
45
46
|
console.error(error);
|
|
46
47
|
return null;
|
|
@@ -80,7 +81,7 @@ var generateRandomPassword = (length = 8) => {
|
|
|
80
81
|
};
|
|
81
82
|
var slugify = (string, separator = "-") => {
|
|
82
83
|
try {
|
|
83
|
-
return string
|
|
84
|
+
return string?.trim()?.length > 0 ? string?.toString()?.toLowerCase()?.trim().replace(/\s+/g, separator)?.replace(/[^\w\-]+/g, "")?.replace(/\_/g, separator)?.replace(/\-\-+/g, separator)?.replace(/\-$/g, "") : "";
|
|
84
85
|
} catch (error) {
|
|
85
86
|
console.error(error);
|
|
86
87
|
return null;
|
|
@@ -142,13 +143,13 @@ var getGreeting = () => {
|
|
|
142
143
|
};
|
|
143
144
|
var validateText = (value, errorMessage = null) => {
|
|
144
145
|
try {
|
|
145
|
-
if (!value
|
|
146
|
+
if (!value)
|
|
146
147
|
return { isError: true, errorMessage: errorMessage || "This field must not be empty." };
|
|
147
148
|
const regex = /^[a-zA-Z ]+$/;
|
|
148
149
|
if (!regex.test(value?.trim())) {
|
|
149
150
|
return { isError: true, errorMessage: errorMessage || "Invalid input" };
|
|
150
151
|
}
|
|
151
|
-
return { isError: false, errorMessage:
|
|
152
|
+
return { isError: false, errorMessage: null };
|
|
152
153
|
} catch (error) {
|
|
153
154
|
console.error(error);
|
|
154
155
|
return { isError: true, message: "Something went wrong." };
|
|
@@ -162,7 +163,7 @@ var validateEmail = (email, errorMessage = null) => {
|
|
|
162
163
|
if (!re.test(email?.trim())) {
|
|
163
164
|
return { isError: true, errorMessage: errorMessage || "Invalid email" };
|
|
164
165
|
}
|
|
165
|
-
return { isError: false, errorMessage:
|
|
166
|
+
return { isError: false, errorMessage: null };
|
|
166
167
|
} catch (error) {
|
|
167
168
|
console.error(error);
|
|
168
169
|
return { isError: true, message: "Something went wrong." };
|
|
@@ -176,7 +177,7 @@ var validatePassword = (password, errorMessage = null) => {
|
|
|
176
177
|
if (!regex.test(password?.trim())) {
|
|
177
178
|
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." };
|
|
178
179
|
}
|
|
179
|
-
return { isError: false, errorMessage:
|
|
180
|
+
return { isError: false, errorMessage: null };
|
|
180
181
|
} catch (error) {
|
|
181
182
|
console.error(error);
|
|
182
183
|
return { isError: true, message: "Something went wrong." };
|
|
@@ -190,13 +191,13 @@ var validateMobileNumber = (value, errorMessage = null) => {
|
|
|
190
191
|
if (!regex.test(value?.trim())) {
|
|
191
192
|
return { isError: true, errorMessage: errorMessage || "Invalid mobile number" };
|
|
192
193
|
}
|
|
193
|
-
return { isError: false, errorMessage:
|
|
194
|
+
return { isError: false, errorMessage: null };
|
|
194
195
|
} catch (error) {
|
|
195
196
|
console.error(error);
|
|
196
197
|
return { isError: true, message: "Something went wrong." };
|
|
197
198
|
}
|
|
198
199
|
};
|
|
199
|
-
var
|
|
200
|
+
var CURRENCYCODE = {
|
|
200
201
|
USD: "USD",
|
|
201
202
|
EUR: "EUR",
|
|
202
203
|
GBP: "GBP",
|
|
@@ -207,13 +208,13 @@ var CurrencyCode = {
|
|
|
207
208
|
CHF: "CHF",
|
|
208
209
|
CNY: "CNY"
|
|
209
210
|
};
|
|
210
|
-
var formatCurrency = (number, currencyCode =
|
|
211
|
+
var formatCurrency = (number, currencyCode = CURRENCYCODE.USD, minimumFractionDigits = 2, maximumFractionDigits = 2) => {
|
|
211
212
|
try {
|
|
212
213
|
if (!number)
|
|
213
214
|
return null;
|
|
214
|
-
if (!Object.values(
|
|
215
|
+
if (!Object.values(CURRENCYCODE).includes(currencyCode)) {
|
|
215
216
|
console.warn(`Invalid currency code "${currencyCode}". Defaulting to USD.`);
|
|
216
|
-
currencyCode =
|
|
217
|
+
currencyCode = CURRENCYCODE.USD;
|
|
217
218
|
}
|
|
218
219
|
return number?.toLocaleString("en-US", {
|
|
219
220
|
style: "currency",
|
|
@@ -234,18 +235,46 @@ var removeDuplicatesFromArray = (array) => {
|
|
|
234
235
|
return null;
|
|
235
236
|
}
|
|
236
237
|
};
|
|
237
|
-
|
|
238
|
+
function getValueByKey(obj, key) {
|
|
239
|
+
if (obj && typeof obj === "object" && key in obj) {
|
|
240
|
+
return obj[key];
|
|
241
|
+
}
|
|
242
|
+
return null;
|
|
243
|
+
}
|
|
244
|
+
var sortArrayByDirection = (array, direction = "asc", key = "") => {
|
|
238
245
|
try {
|
|
239
246
|
if (!array || array.length === 0)
|
|
240
247
|
return null;
|
|
241
|
-
if (
|
|
242
|
-
console.error('Invalid sorting
|
|
248
|
+
if (direction !== "asc" && direction !== "desc") {
|
|
249
|
+
console.error('Invalid sorting direction. Please use "asc" or "desc".');
|
|
243
250
|
return array;
|
|
244
251
|
}
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
252
|
+
if (!key) {
|
|
253
|
+
return [...array].sort((valueA, valueB) => {
|
|
254
|
+
if (typeof valueA === "string" && typeof valueB === "string") {
|
|
255
|
+
return direction === "asc" ? valueA.localeCompare(valueB) : valueB.localeCompare(valueA);
|
|
256
|
+
} else if (typeof valueA === "number" && typeof valueB === "number") {
|
|
257
|
+
return direction === "asc" ? valueA - valueB : valueB - valueA;
|
|
258
|
+
} else if (valueA instanceof Date && valueB instanceof Date) {
|
|
259
|
+
return direction === "asc" ? valueA.getTime() - valueB.getTime() : valueB.getTime() - valueA.getTime();
|
|
260
|
+
} else {
|
|
261
|
+
return 0;
|
|
262
|
+
}
|
|
263
|
+
});
|
|
264
|
+
}
|
|
265
|
+
let Array2 = [...array];
|
|
266
|
+
return Array2.sort((a, b) => {
|
|
267
|
+
const valueA = getValueByKey(a, key);
|
|
268
|
+
const valueB = getValueByKey(b, key);
|
|
269
|
+
if (typeof valueA === "string" && typeof valueB === "string") {
|
|
270
|
+
return direction === "asc" ? valueA.localeCompare(valueB) : valueB.localeCompare(valueA);
|
|
271
|
+
} else if (typeof valueA === "number" && typeof valueB === "number") {
|
|
272
|
+
return direction === "asc" ? valueA - valueB : valueB - valueA;
|
|
273
|
+
} else if (valueA instanceof Date && valueB instanceof Date) {
|
|
274
|
+
return direction === "asc" ? valueA.getTime() - valueB.getTime() : valueB.getTime() - valueA.getTime();
|
|
275
|
+
} else {
|
|
276
|
+
return 0;
|
|
277
|
+
}
|
|
249
278
|
});
|
|
250
279
|
} catch (error) {
|
|
251
280
|
console.error(error);
|
|
@@ -256,7 +285,7 @@ var reverseString = (str) => {
|
|
|
256
285
|
try {
|
|
257
286
|
if (!str)
|
|
258
287
|
return null;
|
|
259
|
-
return str?.split("")
|
|
288
|
+
return str?.split("")?.reverse()?.join("");
|
|
260
289
|
} catch (error) {
|
|
261
290
|
console.error(error);
|
|
262
291
|
return null;
|
|
@@ -269,7 +298,7 @@ var isObjectValuesEmpty = (object) => {
|
|
|
269
298
|
}
|
|
270
299
|
return acc;
|
|
271
300
|
}, {});
|
|
272
|
-
return Object.keys(errors).length > 0;
|
|
301
|
+
return errors && Object.keys(errors).length > 0;
|
|
273
302
|
};
|
|
274
303
|
var checkNotNullAndNotEmpty = (value) => {
|
|
275
304
|
try {
|
|
@@ -316,6 +345,7 @@ var groupBy = (arr, key) => {
|
|
|
316
345
|
}
|
|
317
346
|
};
|
|
318
347
|
export {
|
|
348
|
+
CURRENCYCODE,
|
|
319
349
|
capitalizeFirstLetter,
|
|
320
350
|
checkNotNullAndNotEmpty,
|
|
321
351
|
clearLocalStorage,
|
|
@@ -327,6 +357,7 @@ export {
|
|
|
327
357
|
getInitials,
|
|
328
358
|
getLocalStorage,
|
|
329
359
|
getTruncateDescription,
|
|
360
|
+
getValueByKey,
|
|
330
361
|
groupBy,
|
|
331
362
|
handleNaN,
|
|
332
363
|
isFalsy,
|
|
@@ -337,7 +368,7 @@ export {
|
|
|
337
368
|
reverseString,
|
|
338
369
|
setLocalStorage,
|
|
339
370
|
slugify,
|
|
340
|
-
|
|
371
|
+
sortArrayByDirection,
|
|
341
372
|
titleCase,
|
|
342
373
|
validateEmail,
|
|
343
374
|
validateMobileNumber,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dhavaltank-js-utils",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.4",
|
|
4
4
|
"description": "JavaScript Utility Package which is a collection of functions i use regularly and don't want to write again",
|
|
5
5
|
"main": "./dist/index.js",
|
|
6
6
|
"module": "./dist/index.mjs",
|
|
@@ -57,7 +57,6 @@
|
|
|
57
57
|
},
|
|
58
58
|
"homepage": "https://github.com/DhavalTank01/dhavaltank-js-utils#readme",
|
|
59
59
|
"devDependencies": {
|
|
60
|
-
"prop-types": "^15.8.1",
|
|
61
60
|
"tsup": "^8.0.2",
|
|
62
61
|
"typescript": "^5.4.5"
|
|
63
62
|
}
|