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/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, key: NullableString) => NullableString;
11
- declare const getInitials: (name: NullableString) => string | undefined;
12
- declare const getTruncateDescription: (text: NullableString, maxLength?: number) => string | null;
13
- declare const compareArrays: <T>(arr1: ArrayType<T>, arr2: ArrayType<T>) => boolean | null;
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;
@@ -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,47 @@ 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 const sortArrayByMode: (array: AnyType, mode?: string, key?: null) => any;
87
+ declare function getValueByKey(obj: AnyType, key: StringType): any;
88
+ declare const sortArrayByDirection: <T>(array: T[], direction?: 'asc' | 'desc', key?: keyof T) => T[];
61
89
  declare const reverseString: (str: NullableString) => string | null;
62
- declare const isObjectValuesEmpty: (object: AnyType) => boolean;
90
+ declare const isObjectValuesEmpty: (object: AnyType) => {
91
+ isEmpty: boolean;
92
+ emptyKeys: string[];
93
+ };
63
94
  declare const checkNotNullAndNotEmpty: (value: AnyType) => boolean;
64
95
  declare const handleNaN: (value: AnyType, returnValue?: number) => any;
65
96
  declare const isFalsy: (value: BooleanType) => boolean;
66
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;
67
106
  declare const groupBy: (arr: AnyType, key: StringType) => any;
68
107
 
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, sortArrayByMode, 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, key: NullableString) => NullableString;
11
- declare const getInitials: (name: NullableString) => string | undefined;
12
- declare const getTruncateDescription: (text: NullableString, maxLength?: number) => string | null;
13
- declare const compareArrays: <T>(arr1: ArrayType<T>, arr2: ArrayType<T>) => boolean | null;
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;
@@ -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,47 @@ 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 const sortArrayByMode: (array: AnyType, mode?: string, key?: null) => any;
87
+ declare function getValueByKey(obj: AnyType, key: StringType): any;
88
+ declare const sortArrayByDirection: <T>(array: T[], direction?: 'asc' | 'desc', key?: keyof T) => T[];
61
89
  declare const reverseString: (str: NullableString) => string | null;
62
- declare const isObjectValuesEmpty: (object: AnyType) => boolean;
90
+ declare const isObjectValuesEmpty: (object: AnyType) => {
91
+ isEmpty: boolean;
92
+ emptyKeys: string[];
93
+ };
63
94
  declare const checkNotNullAndNotEmpty: (value: AnyType) => boolean;
64
95
  declare const handleNaN: (value: AnyType, returnValue?: number) => any;
65
96
  declare const isFalsy: (value: BooleanType) => boolean;
66
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;
67
106
  declare const groupBy: (arr: AnyType, key: StringType) => any;
68
107
 
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, sortArrayByMode, 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.js CHANGED
@@ -1,20 +1,23 @@
1
1
  // src/functions/index.ts
2
2
  var capitalizeFirstLetter = (str) => {
3
3
  try {
4
- if (!str)
4
+ if (str == null || str === "")
5
5
  return null;
6
- return str?.trim()?.length > 0 ? str?.charAt(0).toUpperCase() + str?.slice(1) : "";
6
+ const trimmed = str.trim();
7
+ if (trimmed.length === 0)
8
+ return "";
9
+ return trimmed.charAt(0).toUpperCase() + trimmed.slice(1);
7
10
  } catch (error) {
8
11
  console.error(error);
9
12
  return null;
10
13
  }
11
14
  };
12
- var titleCase = (str, key) => {
15
+ var titleCase = (str, separator = " ") => {
13
16
  try {
14
- if (!str)
17
+ if (str == null || str === "")
15
18
  return null;
16
- const text = str.replace(/_/g, " ").split(" ").map((word) => word.charAt(0).toUpperCase() + word.slice(1)).join(" ");
17
- return key === "And" ? text.replace("And", "&") : text;
19
+ const normalized = str.replace(/[-_]/g, separator).split(separator).filter((word) => word.length > 0);
20
+ return normalized.map((word) => word.charAt(0).toUpperCase() + word.slice(1)).join(separator);
18
21
  } catch (error) {
19
22
  console.error(error);
20
23
  return null;
@@ -24,26 +27,48 @@ var getInitials = (name) => {
24
27
  try {
25
28
  if (typeof name !== "string" || name.trim() === "")
26
29
  return "?";
27
- const words = name.trim().split(" ").map((latter) => latter[0].toUpperCase()).join("");
28
- return words.length > 1 ? words[0] + words[words.length - 1] : words;
30
+ const words = name.trim().split(/[\s_-]+/).filter((value) => value.length > 0);
31
+ if (words.length === 0)
32
+ return "?";
33
+ const initials = words.map((word) => word.charAt(0)?.toUpperCase() ?? "").join("");
34
+ if (words.length === 1) {
35
+ const word = words[0];
36
+ if (word.length <= 1)
37
+ return word.toUpperCase();
38
+ return word.charAt(0).toUpperCase() + word.slice(-1).toUpperCase();
39
+ }
40
+ return initials;
29
41
  } catch (error) {
30
42
  console.error(error);
43
+ return "?";
31
44
  }
32
45
  };
33
- var getTruncateDescription = (text, maxLength = 10) => {
46
+ var getTruncateDescription = (text, maxLength = 50) => {
34
47
  try {
35
- return !text ? "" : text.length <= maxLength ? text : `${text.substring(0, maxLength)}...`;
48
+ if (!text)
49
+ return "";
50
+ if (text.length <= maxLength)
51
+ return text;
52
+ return `${text.substring(0, maxLength)}...`;
36
53
  } catch (error) {
37
54
  console.error(error);
38
- return null;
55
+ return "";
39
56
  }
40
57
  };
41
58
  var compareArrays = (arr1, arr2) => {
42
59
  try {
43
- return arr1.length === arr2.length && arr1.slice().sort().every((value, index) => value === arr2.slice().sort()[index]);
60
+ if (arr1 == null || arr2 == null)
61
+ return false;
62
+ const array1 = Array.isArray(arr1) ? arr1.flat(Infinity).slice() : [arr1];
63
+ const array2 = Array.isArray(arr2) ? arr2.flat(Infinity).slice() : [arr2];
64
+ const sorted1 = array1.sort();
65
+ const sorted2 = array2.sort();
66
+ if (sorted1.length !== sorted2.length)
67
+ return false;
68
+ return sorted1.every((value, index) => value === sorted2[index]);
44
69
  } catch (error) {
45
70
  console.error(error);
46
- return null;
71
+ return false;
47
72
  }
48
73
  };
49
74
  var generateOTPWithLength = (length, isAlphaNumeric = false) => {
@@ -80,7 +105,7 @@ var generateRandomPassword = (length = 8) => {
80
105
  };
81
106
  var slugify = (string, separator = "-") => {
82
107
  try {
83
- return string.trim() ? string.toString().toLowerCase().trim().replace(/\s+/g, separator).replace(/[^\w\-]+/g, "").replace(/\_/g, separator).replace(/\-\-+/g, separator).replace(/\-$/g, "") : "";
108
+ 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
109
  } catch (error) {
85
110
  console.error(error);
86
111
  return null;
@@ -142,13 +167,13 @@ var getGreeting = () => {
142
167
  };
143
168
  var validateText = (value, errorMessage = null) => {
144
169
  try {
145
- if (!value?.trim())
170
+ if (!value)
146
171
  return { isError: true, errorMessage: errorMessage || "This field must not be empty." };
147
172
  const regex = /^[a-zA-Z ]+$/;
148
173
  if (!regex.test(value?.trim())) {
149
174
  return { isError: true, errorMessage: errorMessage || "Invalid input" };
150
175
  }
151
- return { isError: false, errorMessage: "Valid input" };
176
+ return { isError: false, errorMessage: null };
152
177
  } catch (error) {
153
178
  console.error(error);
154
179
  return { isError: true, message: "Something went wrong." };
@@ -156,13 +181,15 @@ var validateText = (value, errorMessage = null) => {
156
181
  };
157
182
  var validateEmail = (email, errorMessage = null) => {
158
183
  try {
159
- if (!email?.trim())
184
+ if (email == null || String(email).trim() === "") {
160
185
  return { isError: true, errorMessage: errorMessage || "This field must not be empty." };
161
- var re = /^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
162
- if (!re.test(email?.trim())) {
186
+ }
187
+ const emailStr = String(email).trim();
188
+ 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])?)*$/;
189
+ if (!re.test(emailStr)) {
163
190
  return { isError: true, errorMessage: errorMessage || "Invalid email" };
164
191
  }
165
- return { isError: false, errorMessage: "Valid email" };
192
+ return { isError: false, errorMessage: null };
166
193
  } catch (error) {
167
194
  console.error(error);
168
195
  return { isError: true, message: "Something went wrong." };
@@ -176,7 +203,7 @@ var validatePassword = (password, errorMessage = null) => {
176
203
  if (!regex.test(password?.trim())) {
177
204
  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
205
  }
179
- return { isError: false, errorMessage: "Valid password" };
206
+ return { isError: false, errorMessage: null };
180
207
  } catch (error) {
181
208
  console.error(error);
182
209
  return { isError: true, message: "Something went wrong." };
@@ -184,19 +211,22 @@ var validatePassword = (password, errorMessage = null) => {
184
211
  };
185
212
  var validateMobileNumber = (value, errorMessage = null) => {
186
213
  try {
187
- if (!value?.trim())
214
+ const strValue = typeof value === "number" ? String(value) : value;
215
+ if (strValue == null || strValue.trim() === "") {
188
216
  return { isError: true, errorMessage: errorMessage || "This field must not be empty." };
189
- const regex = /^\d{10}$/;
190
- if (!regex.test(value?.trim())) {
217
+ }
218
+ const trimmed = strValue.trim();
219
+ const regex = /^(?:\+?91[-\s]?)?\d{10}$/;
220
+ if (!regex.test(trimmed)) {
191
221
  return { isError: true, errorMessage: errorMessage || "Invalid mobile number" };
192
222
  }
193
- return { isError: false, errorMessage: "Valid mobile number" };
223
+ return { isError: false, errorMessage: null };
194
224
  } catch (error) {
195
225
  console.error(error);
196
226
  return { isError: true, message: "Something went wrong." };
197
227
  }
198
228
  };
199
- var CurrencyCode = {
229
+ var CURRENCYCODE = {
200
230
  USD: "USD",
201
231
  EUR: "EUR",
202
232
  GBP: "GBP",
@@ -207,13 +237,13 @@ var CurrencyCode = {
207
237
  CHF: "CHF",
208
238
  CNY: "CNY"
209
239
  };
210
- var formatCurrency = (number, currencyCode = CurrencyCode.USD, minimumFractionDigits = 2, maximumFractionDigits = 2) => {
240
+ var formatCurrency = (number, currencyCode = CURRENCYCODE.USD, minimumFractionDigits = 2, maximumFractionDigits = 2) => {
211
241
  try {
212
242
  if (!number)
213
243
  return null;
214
- if (!Object.values(CurrencyCode).includes(currencyCode)) {
244
+ if (!Object.values(CURRENCYCODE).includes(currencyCode)) {
215
245
  console.warn(`Invalid currency code "${currencyCode}". Defaulting to USD.`);
216
- currencyCode = CurrencyCode.USD;
246
+ currencyCode = CURRENCYCODE.USD;
217
247
  }
218
248
  return number?.toLocaleString("en-US", {
219
249
  style: "currency",
@@ -234,42 +264,58 @@ var removeDuplicatesFromArray = (array) => {
234
264
  return null;
235
265
  }
236
266
  };
237
- var sortArrayByMode = (array, mode = "asc", key = null) => {
267
+ function getValueByKey(obj, key) {
268
+ if (obj && typeof obj === "object" && key in obj) {
269
+ return obj[key];
270
+ }
271
+ return null;
272
+ }
273
+ var sortArrayByDirection = (array, direction = "asc", key) => {
238
274
  try {
239
275
  if (!array || array.length === 0)
240
- return null;
241
- if (mode !== "asc" && mode !== "desc") {
242
- console.error('Invalid sorting mode. Please use "asc" or "desc".');
276
+ return [];
277
+ if (direction !== "asc" && direction !== "desc") {
278
+ console.error('Invalid sorting direction. Please use "asc" or "desc".');
243
279
  return array;
244
280
  }
245
- return [...array].sort((a, b) => {
246
- const aValue = key !== null ? a[key] : a;
247
- const bValue = key !== null ? b[key] : b;
248
- return mode === "asc" ? aValue - bValue : bValue - aValue;
281
+ const sorted = [...array].sort((a, b) => {
282
+ const valueA = key ? a[key] : a;
283
+ const valueB = key ? b[key] : b;
284
+ if (typeof valueA === "string" && typeof valueB === "string") {
285
+ return direction === "asc" ? valueA.localeCompare(valueB) : valueB.localeCompare(valueA);
286
+ } else if (typeof valueA === "number" && typeof valueB === "number") {
287
+ return direction === "asc" ? valueA - valueB : valueB - valueA;
288
+ } else if (valueA instanceof Date && valueB instanceof Date) {
289
+ return direction === "asc" ? valueA.getTime() - valueB.getTime() : valueB.getTime() - valueA.getTime();
290
+ }
291
+ return 0;
249
292
  });
293
+ return sorted;
250
294
  } catch (error) {
251
295
  console.error(error);
252
- return null;
296
+ return array;
253
297
  }
254
298
  };
255
299
  var reverseString = (str) => {
256
300
  try {
257
301
  if (!str)
258
302
  return null;
259
- return str?.split("").reverse().join("");
303
+ return str?.split("")?.reverse()?.join("");
260
304
  } catch (error) {
261
305
  console.error(error);
262
306
  return null;
263
307
  }
264
308
  };
265
309
  var isObjectValuesEmpty = (object) => {
266
- const errors = Object.keys(object).reduce((acc, key) => {
267
- if (!object[key]) {
268
- acc[key] = key;
269
- }
270
- return acc;
271
- }, {});
272
- return Object.keys(errors).length > 0;
310
+ try {
311
+ if (object == null || typeof object !== "object")
312
+ return { isEmpty: false, emptyKeys: [] };
313
+ const emptyKeys = Object.keys(object).filter((key) => !object[key]);
314
+ return { isEmpty: emptyKeys.length > 0, emptyKeys };
315
+ } catch (error) {
316
+ console.error(error);
317
+ return { isEmpty: false, emptyKeys: [] };
318
+ }
273
319
  };
274
320
  var checkNotNullAndNotEmpty = (value) => {
275
321
  try {
@@ -299,6 +345,123 @@ var isFalsy = (value) => {
299
345
  var isTruthy = (value) => {
300
346
  return !!value;
301
347
  };
348
+ var capitalizeEveryWord = (str) => {
349
+ try {
350
+ if (!str)
351
+ return "";
352
+ return str.trim().split(/\s+/).map((word) => word.charAt(0).toUpperCase() + word.slice(1)).join(" ");
353
+ } catch (error) {
354
+ console.error(error);
355
+ return "";
356
+ }
357
+ };
358
+ var camelCase = (str) => {
359
+ try {
360
+ if (!str)
361
+ return "";
362
+ return str.trim().replace(/[-_\s]/g, " ").split(/\s+/).map((word, index) => index === 0 ? word.toLowerCase() : word.charAt(0).toUpperCase() + word.slice(1).toLowerCase()).join("");
363
+ } catch (error) {
364
+ console.error(error);
365
+ return "";
366
+ }
367
+ };
368
+ var pascalCase = (str) => {
369
+ try {
370
+ if (!str)
371
+ return "";
372
+ return str.trim().replace(/[-_\s]/g, " ").split(/\s+/).map((word) => word.charAt(0).toUpperCase() + word.slice(1).toLowerCase()).join("");
373
+ } catch (error) {
374
+ console.error(error);
375
+ return "";
376
+ }
377
+ };
378
+ var truncate = (str, maxLength = 20) => {
379
+ try {
380
+ if (!str)
381
+ return "";
382
+ if (str.length <= maxLength)
383
+ return str;
384
+ return str.substring(0, maxLength).trim() + "...";
385
+ } catch (error) {
386
+ console.error(error);
387
+ return "";
388
+ }
389
+ };
390
+ var formatDate = (date, format = "YYYY-MM-DD") => {
391
+ try {
392
+ const d = new Date(date);
393
+ if (isNaN(d.getTime()))
394
+ return "";
395
+ const year = d.getFullYear();
396
+ const month = String(d.getMonth() + 1).padStart(2, "0");
397
+ const day = String(d.getDate()).padStart(2, "0");
398
+ const hours = String(d.getHours()).padStart(2, "0");
399
+ const minutes = String(d.getMinutes()).padStart(2, "0");
400
+ const seconds = String(d.getSeconds()).padStart(2, "0");
401
+ return format.replace("YYYY", String(year)).replace("MM", month).replace("DD", day).replace("HH", hours).replace("mm", minutes).replace("ss", seconds);
402
+ } catch (error) {
403
+ console.error(error);
404
+ return "";
405
+ }
406
+ };
407
+ var deepClone = (obj) => {
408
+ try {
409
+ if (obj === null || obj === void 0)
410
+ return obj;
411
+ if (obj instanceof Date)
412
+ return new Date(obj.getTime());
413
+ if (Array.isArray(obj))
414
+ return obj.map((item) => deepClone(item));
415
+ if (typeof obj === "object" && obj !== null) {
416
+ const cloned = {};
417
+ for (const key of Object.keys(obj)) {
418
+ cloned[key] = deepClone(obj[key]);
419
+ }
420
+ return cloned;
421
+ }
422
+ return obj;
423
+ } catch (error) {
424
+ console.error(error);
425
+ return obj;
426
+ }
427
+ };
428
+ var debounce = (func, wait) => {
429
+ let timeout;
430
+ return function executedFunction(...args) {
431
+ const later = () => {
432
+ func(...args);
433
+ };
434
+ clearTimeout(timeout);
435
+ timeout = setTimeout(later, wait);
436
+ };
437
+ };
438
+ var formatDateRelative = (date) => {
439
+ try {
440
+ const d = new Date(date);
441
+ if (isNaN(d.getTime()))
442
+ return "";
443
+ const seconds = Math.floor(((/* @__PURE__ */ new Date()).getTime() - d.getTime()) / 1e3);
444
+ let interval = seconds / 60;
445
+ if (interval < 1)
446
+ return "just now";
447
+ if (interval < 1.5)
448
+ return "a minute ago";
449
+ if (interval < 45)
450
+ return Math.floor(interval) + " minutes ago";
451
+ if (interval < 90)
452
+ return "an hour ago";
453
+ if (interval < 1440)
454
+ return Math.floor(interval / 60) + " hours ago";
455
+ if (interval < 2880)
456
+ return Math.floor(interval / 1440) + " days ago";
457
+ if (interval < 43200)
458
+ return Math.floor(interval / 1440 / 30) + " months ago";
459
+ return Math.floor(interval / 1440 / 365) + " years ago";
460
+ } catch (error) {
461
+ console.error(error);
462
+ return "";
463
+ }
464
+ };
302
465
  var groupBy = (arr, key) => {
303
466
  try {
304
467
  if (!arr)
@@ -316,29 +479,39 @@ var groupBy = (arr, key) => {
316
479
  }
317
480
  };
318
481
  export {
482
+ CURRENCYCODE,
483
+ camelCase,
484
+ capitalizeEveryWord,
319
485
  capitalizeFirstLetter,
320
486
  checkNotNullAndNotEmpty,
321
487
  clearLocalStorage,
322
488
  compareArrays,
489
+ debounce,
490
+ deepClone,
323
491
  formatCurrency,
492
+ formatDate,
493
+ formatDateRelative,
324
494
  generateOTPWithLength,
325
495
  generateRandomPassword,
326
496
  getGreeting,
327
497
  getInitials,
328
498
  getLocalStorage,
329
499
  getTruncateDescription,
500
+ getValueByKey,
330
501
  groupBy,
331
502
  handleNaN,
332
503
  isFalsy,
333
504
  isObjectValuesEmpty,
334
505
  isTruthy,
506
+ pascalCase,
335
507
  removeDuplicatesFromArray,
336
508
  removeLocalStorage,
337
509
  reverseString,
338
510
  setLocalStorage,
339
511
  slugify,
340
- sortArrayByMode,
512
+ sortArrayByDirection,
341
513
  titleCase,
514
+ truncate,
342
515
  validateEmail,
343
516
  validateMobileNumber,
344
517
  validatePassword,
package/package.json CHANGED
@@ -1,13 +1,14 @@
1
1
  {
2
2
  "name": "dhavaltank-js-utils",
3
- "version": "0.0.3",
3
+ "version": "0.0.5",
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",
7
7
  "type": "module",
8
8
  "types": "./dist/index.d.ts",
9
9
  "scripts": {
10
- "build": "tsup"
10
+ "build": "tsup",
11
+ "test": "vitest run"
11
12
  },
12
13
  "keywords": [
13
14
  "utils",
@@ -49,7 +50,7 @@
49
50
  "license": "MIT",
50
51
  "repository": {
51
52
  "type": "git",
52
- "url": "git+https://github.com/DhavalTank01/dhavaltank-js-utils.git"
53
+ "url": "git+https.com/DhavalTank01/dhavaltank-js-utils.git"
53
54
  },
54
55
  "author": "Dhaval Tank",
55
56
  "bugs": {
@@ -57,8 +58,8 @@
57
58
  },
58
59
  "homepage": "https://github.com/DhavalTank01/dhavaltank-js-utils#readme",
59
60
  "devDependencies": {
60
- "prop-types": "^15.8.1",
61
61
  "tsup": "^8.0.2",
62
- "typescript": "^5.4.5"
62
+ "typescript": "^5.4.5",
63
+ "vitest": "^5.1.1"
63
64
  }
64
65
  }