dhavaltank-js-utils 0.0.1
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/README.md +411 -0
- package/dist/index.cjs +399 -0
- package/dist/index.d.cts +69 -0
- package/dist/index.d.ts +69 -0
- package/dist/index.js +346 -0
- package/package.json +64 -0
- package/tsconfig.json +17 -0
- package/tsup.config.ts +10 -0
package/dist/index.js
ADDED
|
@@ -0,0 +1,346 @@
|
|
|
1
|
+
// src/functions/index.ts
|
|
2
|
+
var capitalizeFirstLetter = (str) => {
|
|
3
|
+
try {
|
|
4
|
+
if (!str)
|
|
5
|
+
return null;
|
|
6
|
+
return str?.trim()?.length > 0 ? str?.charAt(0).toUpperCase() + str?.slice(1) : "";
|
|
7
|
+
} catch (error) {
|
|
8
|
+
console.error(error);
|
|
9
|
+
return null;
|
|
10
|
+
}
|
|
11
|
+
};
|
|
12
|
+
var titleCase = (str, key) => {
|
|
13
|
+
try {
|
|
14
|
+
if (!str)
|
|
15
|
+
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;
|
|
18
|
+
} catch (error) {
|
|
19
|
+
console.error(error);
|
|
20
|
+
return null;
|
|
21
|
+
}
|
|
22
|
+
};
|
|
23
|
+
var getInitials = (name) => {
|
|
24
|
+
try {
|
|
25
|
+
if (typeof name !== "string" || name.trim() === "")
|
|
26
|
+
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;
|
|
29
|
+
} catch (error) {
|
|
30
|
+
console.error(error);
|
|
31
|
+
}
|
|
32
|
+
};
|
|
33
|
+
var getTruncateDescription = (text, maxLength = 10) => {
|
|
34
|
+
try {
|
|
35
|
+
return !text ? "" : text.length <= maxLength ? text : `${text.substring(0, maxLength)}...`;
|
|
36
|
+
} catch (error) {
|
|
37
|
+
console.error(error);
|
|
38
|
+
return null;
|
|
39
|
+
}
|
|
40
|
+
};
|
|
41
|
+
var compareArrays = (arr1, arr2) => {
|
|
42
|
+
try {
|
|
43
|
+
return arr1.length === arr2.length && arr1.slice().sort().every((value, index) => value === arr2.slice().sort()[index]);
|
|
44
|
+
} catch (error) {
|
|
45
|
+
console.error(error);
|
|
46
|
+
return null;
|
|
47
|
+
}
|
|
48
|
+
};
|
|
49
|
+
var generateOTPWithLength = (length, isAlphaNumeric = false) => {
|
|
50
|
+
try {
|
|
51
|
+
if (isAlphaNumeric) {
|
|
52
|
+
const characters = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
|
53
|
+
return Array.from({ length }, () => characters.charAt(Math.floor(Math.random() * characters.length))).join("");
|
|
54
|
+
} else {
|
|
55
|
+
return Math.floor(Math.random() * (Math.pow(10, length) - Math.pow(10, length - 1) + 1)) + Math.pow(10, length - 1);
|
|
56
|
+
}
|
|
57
|
+
} catch (error) {
|
|
58
|
+
console.error(error);
|
|
59
|
+
}
|
|
60
|
+
};
|
|
61
|
+
var generateRandomPassword = (length = 8) => {
|
|
62
|
+
try {
|
|
63
|
+
const lowercaseChars = "abcdefghijklmnopqrstuvwxyz";
|
|
64
|
+
const uppercaseChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
|
65
|
+
const digitChars = "0123456789";
|
|
66
|
+
const specialChars = "!@#$%^&*()-_=+";
|
|
67
|
+
let password = "";
|
|
68
|
+
password += lowercaseChars.charAt(Math.floor(Math.random() * lowercaseChars.length));
|
|
69
|
+
password += uppercaseChars.charAt(Math.floor(Math.random() * uppercaseChars.length));
|
|
70
|
+
password += digitChars.charAt(Math.floor(Math.random() * digitChars.length));
|
|
71
|
+
for (let i = password.length; i < length; i++) {
|
|
72
|
+
const randomIndex = Math.floor(Math.random() * (lowercaseChars + uppercaseChars + digitChars + specialChars).length);
|
|
73
|
+
password += (lowercaseChars + uppercaseChars + digitChars + specialChars).charAt(randomIndex);
|
|
74
|
+
}
|
|
75
|
+
return password;
|
|
76
|
+
} catch (error) {
|
|
77
|
+
console.error(error);
|
|
78
|
+
return null;
|
|
79
|
+
}
|
|
80
|
+
};
|
|
81
|
+
var slugify = (string, separator = "-") => {
|
|
82
|
+
try {
|
|
83
|
+
return string.trim() ? string.toString().toLowerCase().trim().replace(/\s+/g, separator).replace(/[^\w\-]+/g, "").replace(/\_/g, separator).replace(/\-\-+/g, separator).replace(/\-$/g, "") : "";
|
|
84
|
+
} catch (error) {
|
|
85
|
+
console.error(error);
|
|
86
|
+
return null;
|
|
87
|
+
}
|
|
88
|
+
};
|
|
89
|
+
var setLocalStorage = (key, data) => {
|
|
90
|
+
try {
|
|
91
|
+
localStorage.setItem(key, JSON.stringify(data));
|
|
92
|
+
return true;
|
|
93
|
+
} catch (error) {
|
|
94
|
+
console.error(`Error setting data in localStorage for key "${key}":`, error);
|
|
95
|
+
return false;
|
|
96
|
+
}
|
|
97
|
+
};
|
|
98
|
+
var getLocalStorage = (key) => {
|
|
99
|
+
try {
|
|
100
|
+
const storedData = localStorage.getItem(key);
|
|
101
|
+
return storedData ? JSON.parse(storedData) : null;
|
|
102
|
+
} catch (error) {
|
|
103
|
+
console.error(`Error retrieving data from localStorage for key "${key}":`, error);
|
|
104
|
+
return false;
|
|
105
|
+
}
|
|
106
|
+
};
|
|
107
|
+
var removeLocalStorage = (key) => {
|
|
108
|
+
try {
|
|
109
|
+
localStorage.removeItem(key);
|
|
110
|
+
return true;
|
|
111
|
+
} catch (error) {
|
|
112
|
+
console.error(`Error removing data from localStorage for key "${key}":`, error);
|
|
113
|
+
return false;
|
|
114
|
+
}
|
|
115
|
+
};
|
|
116
|
+
var clearLocalStorage = () => {
|
|
117
|
+
try {
|
|
118
|
+
localStorage.clear();
|
|
119
|
+
return true;
|
|
120
|
+
} catch (error) {
|
|
121
|
+
console.error("Error clearing localStorage:", error);
|
|
122
|
+
return false;
|
|
123
|
+
}
|
|
124
|
+
};
|
|
125
|
+
var getGreeting = () => {
|
|
126
|
+
try {
|
|
127
|
+
const greetings = {
|
|
128
|
+
morning: "Good morning!",
|
|
129
|
+
afternoon: "Good afternoon!",
|
|
130
|
+
evening: "Good evening!"
|
|
131
|
+
};
|
|
132
|
+
const currentHour = (/* @__PURE__ */ new Date()).getHours();
|
|
133
|
+
if (currentHour >= 5 && currentHour < 12)
|
|
134
|
+
return greetings.morning;
|
|
135
|
+
else if (currentHour >= 12 && currentHour < 17)
|
|
136
|
+
return greetings.afternoon;
|
|
137
|
+
else
|
|
138
|
+
return greetings.evening;
|
|
139
|
+
} catch (error) {
|
|
140
|
+
return null;
|
|
141
|
+
}
|
|
142
|
+
};
|
|
143
|
+
var validateText = (value, errorMessage = null) => {
|
|
144
|
+
try {
|
|
145
|
+
if (!value?.trim())
|
|
146
|
+
return { isError: true, errorMessage: errorMessage || "This field must not be empty." };
|
|
147
|
+
const regex = /^[a-zA-Z ]+$/;
|
|
148
|
+
if (!regex.test(value?.trim())) {
|
|
149
|
+
return { isError: true, errorMessage: errorMessage || "Invalid input" };
|
|
150
|
+
}
|
|
151
|
+
return { isError: false, errorMessage: "Valid input" };
|
|
152
|
+
} catch (error) {
|
|
153
|
+
console.error(error);
|
|
154
|
+
return { isError: true, message: "Something went wrong." };
|
|
155
|
+
}
|
|
156
|
+
};
|
|
157
|
+
var validateEmail = (email, errorMessage = null) => {
|
|
158
|
+
try {
|
|
159
|
+
if (!email?.trim())
|
|
160
|
+
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())) {
|
|
163
|
+
return { isError: true, errorMessage: errorMessage || "Invalid email" };
|
|
164
|
+
}
|
|
165
|
+
return { isError: false, errorMessage: "Valid email" };
|
|
166
|
+
} catch (error) {
|
|
167
|
+
console.error(error);
|
|
168
|
+
return { isError: true, message: "Something went wrong." };
|
|
169
|
+
}
|
|
170
|
+
};
|
|
171
|
+
var validatePassword = (password, errorMessage = null) => {
|
|
172
|
+
try {
|
|
173
|
+
if (!password?.trim())
|
|
174
|
+
return { isError: true, errorMessage: errorMessage || "This field must not be empty." };
|
|
175
|
+
const regex = /^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[!@#$%^&*()_+[\]{};':"\\|,.<>/?]).{8,40}$/;
|
|
176
|
+
if (!regex.test(password?.trim())) {
|
|
177
|
+
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
|
+
return { isError: false, errorMessage: "Valid password" };
|
|
180
|
+
} catch (error) {
|
|
181
|
+
console.error(error);
|
|
182
|
+
return { isError: true, message: "Something went wrong." };
|
|
183
|
+
}
|
|
184
|
+
};
|
|
185
|
+
var validateMobileNumber = (value, errorMessage = null) => {
|
|
186
|
+
try {
|
|
187
|
+
if (!value?.trim())
|
|
188
|
+
return { isError: true, errorMessage: errorMessage || "This field must not be empty." };
|
|
189
|
+
const regex = /^\d{10}$/;
|
|
190
|
+
if (!regex.test(value?.trim())) {
|
|
191
|
+
return { isError: true, errorMessage: errorMessage || "Invalid mobile number" };
|
|
192
|
+
}
|
|
193
|
+
return { isError: false, errorMessage: "Valid mobile number" };
|
|
194
|
+
} catch (error) {
|
|
195
|
+
console.error(error);
|
|
196
|
+
return { isError: true, message: "Something went wrong." };
|
|
197
|
+
}
|
|
198
|
+
};
|
|
199
|
+
var CurrencyCode = {
|
|
200
|
+
USD: "USD",
|
|
201
|
+
EUR: "EUR",
|
|
202
|
+
GBP: "GBP",
|
|
203
|
+
INR: "INR",
|
|
204
|
+
JPY: "JPY",
|
|
205
|
+
AUD: "AUD",
|
|
206
|
+
CAD: "CAD",
|
|
207
|
+
CHF: "CHF",
|
|
208
|
+
CNY: "CNY"
|
|
209
|
+
};
|
|
210
|
+
var formatCurrency = (number, currencyCode = CurrencyCode.USD, minimumFractionDigits = 2, maximumFractionDigits = 2) => {
|
|
211
|
+
try {
|
|
212
|
+
if (!number)
|
|
213
|
+
return null;
|
|
214
|
+
if (!Object.values(CurrencyCode).includes(currencyCode)) {
|
|
215
|
+
console.warn(`Invalid currency code "${currencyCode}". Defaulting to USD.`);
|
|
216
|
+
currencyCode = CurrencyCode.USD;
|
|
217
|
+
}
|
|
218
|
+
return number?.toLocaleString("en-US", {
|
|
219
|
+
style: "currency",
|
|
220
|
+
currency: currencyCode,
|
|
221
|
+
minimumFractionDigits,
|
|
222
|
+
maximumFractionDigits
|
|
223
|
+
});
|
|
224
|
+
} catch (error) {
|
|
225
|
+
console.error(error);
|
|
226
|
+
return null;
|
|
227
|
+
}
|
|
228
|
+
};
|
|
229
|
+
var removeDuplicatesFromArray = (array) => {
|
|
230
|
+
try {
|
|
231
|
+
return Array.from(new Set(array));
|
|
232
|
+
} catch (error) {
|
|
233
|
+
console.error(error);
|
|
234
|
+
return null;
|
|
235
|
+
}
|
|
236
|
+
};
|
|
237
|
+
var sortArrayByMode = (array, mode = "asc", key = null) => {
|
|
238
|
+
try {
|
|
239
|
+
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".');
|
|
243
|
+
return array;
|
|
244
|
+
}
|
|
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;
|
|
249
|
+
});
|
|
250
|
+
} catch (error) {
|
|
251
|
+
console.error(error);
|
|
252
|
+
return null;
|
|
253
|
+
}
|
|
254
|
+
};
|
|
255
|
+
var reverseString = (str) => {
|
|
256
|
+
try {
|
|
257
|
+
if (!str)
|
|
258
|
+
return null;
|
|
259
|
+
return str?.split("").reverse().join("");
|
|
260
|
+
} catch (error) {
|
|
261
|
+
console.error(error);
|
|
262
|
+
return null;
|
|
263
|
+
}
|
|
264
|
+
};
|
|
265
|
+
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;
|
|
273
|
+
};
|
|
274
|
+
var checkNotNullAndNotEmpty = (value) => {
|
|
275
|
+
try {
|
|
276
|
+
if (value !== void 0 && value !== null && value !== "" && value?.toString()?.length !== 0)
|
|
277
|
+
return true;
|
|
278
|
+
else
|
|
279
|
+
return false;
|
|
280
|
+
} catch (error) {
|
|
281
|
+
console.error(error);
|
|
282
|
+
return false;
|
|
283
|
+
}
|
|
284
|
+
};
|
|
285
|
+
var handleNaN = (value, returnValue = 0) => {
|
|
286
|
+
try {
|
|
287
|
+
if (isNaN(value))
|
|
288
|
+
return returnValue;
|
|
289
|
+
else
|
|
290
|
+
return value;
|
|
291
|
+
} catch (error) {
|
|
292
|
+
console.error(error);
|
|
293
|
+
return returnValue;
|
|
294
|
+
}
|
|
295
|
+
};
|
|
296
|
+
var isFalsy = (value) => {
|
|
297
|
+
return !value;
|
|
298
|
+
};
|
|
299
|
+
var isTruthy = (value) => {
|
|
300
|
+
return !!value;
|
|
301
|
+
};
|
|
302
|
+
var groupBy = (arr, key) => {
|
|
303
|
+
try {
|
|
304
|
+
if (!arr)
|
|
305
|
+
return null;
|
|
306
|
+
if (!key)
|
|
307
|
+
return arr;
|
|
308
|
+
return arr.reduce((acc, obj) => {
|
|
309
|
+
const keyValue = obj[key];
|
|
310
|
+
(acc[keyValue] ?? (acc[keyValue] = [])).push(obj);
|
|
311
|
+
return acc;
|
|
312
|
+
}, {});
|
|
313
|
+
} catch (error) {
|
|
314
|
+
console.error(error);
|
|
315
|
+
return null;
|
|
316
|
+
}
|
|
317
|
+
};
|
|
318
|
+
export {
|
|
319
|
+
capitalizeFirstLetter,
|
|
320
|
+
checkNotNullAndNotEmpty,
|
|
321
|
+
clearLocalStorage,
|
|
322
|
+
compareArrays,
|
|
323
|
+
formatCurrency,
|
|
324
|
+
generateOTPWithLength,
|
|
325
|
+
generateRandomPassword,
|
|
326
|
+
getGreeting,
|
|
327
|
+
getInitials,
|
|
328
|
+
getLocalStorage,
|
|
329
|
+
getTruncateDescription,
|
|
330
|
+
groupBy,
|
|
331
|
+
handleNaN,
|
|
332
|
+
isFalsy,
|
|
333
|
+
isObjectValuesEmpty,
|
|
334
|
+
isTruthy,
|
|
335
|
+
removeDuplicatesFromArray,
|
|
336
|
+
removeLocalStorage,
|
|
337
|
+
reverseString,
|
|
338
|
+
setLocalStorage,
|
|
339
|
+
slugify,
|
|
340
|
+
sortArrayByMode,
|
|
341
|
+
titleCase,
|
|
342
|
+
validateEmail,
|
|
343
|
+
validateMobileNumber,
|
|
344
|
+
validatePassword,
|
|
345
|
+
validateText
|
|
346
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "dhavaltank-js-utils",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"description": "JavaScript Utility Package which is a collection of functions i use regularly and don't want to write again",
|
|
5
|
+
"main": "./dist/index.ts",
|
|
6
|
+
"module": "./dist/index.mjs",
|
|
7
|
+
"type": "module",
|
|
8
|
+
"types": "./dist/index.d.ts",
|
|
9
|
+
"scripts": {
|
|
10
|
+
"build": "tsup"
|
|
11
|
+
},
|
|
12
|
+
"keywords": [
|
|
13
|
+
"utils",
|
|
14
|
+
"utils-file",
|
|
15
|
+
"js-utils",
|
|
16
|
+
"javascript",
|
|
17
|
+
"dhaval",
|
|
18
|
+
"dhavaltank",
|
|
19
|
+
"dhavaltank-js-utils",
|
|
20
|
+
"capitalizeFirstLetter",
|
|
21
|
+
"titleCase",
|
|
22
|
+
"getInitials",
|
|
23
|
+
"getTruncateDescription",
|
|
24
|
+
"compareArrays",
|
|
25
|
+
"generateOTPWithLength",
|
|
26
|
+
"generateRandomPassword",
|
|
27
|
+
"slugify",
|
|
28
|
+
"setLocalStorage",
|
|
29
|
+
"getLocalStorage",
|
|
30
|
+
"removeLocalStorage",
|
|
31
|
+
"clearLocalStorage",
|
|
32
|
+
"getGreeting",
|
|
33
|
+
"validateText",
|
|
34
|
+
"validateEmail",
|
|
35
|
+
"validatePassword",
|
|
36
|
+
"validateMobileNumber",
|
|
37
|
+
"formatCurrency",
|
|
38
|
+
"CurrencyCode",
|
|
39
|
+
"removeDuplicatesFromArray",
|
|
40
|
+
"sortArrayByMode",
|
|
41
|
+
"reverseString",
|
|
42
|
+
"isObjectValuesEmpty",
|
|
43
|
+
"checkNotNullAndNotEmpty",
|
|
44
|
+
"handleNaN",
|
|
45
|
+
"isFalsy",
|
|
46
|
+
"isTruthy",
|
|
47
|
+
"groupBy"
|
|
48
|
+
],
|
|
49
|
+
"license": "MIT",
|
|
50
|
+
"repository": {
|
|
51
|
+
"type": "git",
|
|
52
|
+
"url": "git+https://github.com/DhavalTank01/dhavaltank-js-utils.git"
|
|
53
|
+
},
|
|
54
|
+
"author": "Dhaval Tank",
|
|
55
|
+
"bugs": {
|
|
56
|
+
"url": "https://github.com/DhavalTank01/dhavaltank-js-utils/issues"
|
|
57
|
+
},
|
|
58
|
+
"homepage": "https://github.com/DhavalTank01/dhavaltank-js-utils#readme",
|
|
59
|
+
"devDependencies": {
|
|
60
|
+
"prop-types": "^15.8.1",
|
|
61
|
+
"tsup": "^8.0.2",
|
|
62
|
+
"typescript": "^5.4.5"
|
|
63
|
+
}
|
|
64
|
+
}
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"strict": true,
|
|
4
|
+
"noImplicitAny": true,
|
|
5
|
+
"esModuleInterop": true,
|
|
6
|
+
"strictNullChecks": true,
|
|
7
|
+
"target": "ES2020",
|
|
8
|
+
"moduleResolution": "Node10",
|
|
9
|
+
"module": "CommonJS",
|
|
10
|
+
"declaration": true,
|
|
11
|
+
"isolatedModules": true,
|
|
12
|
+
"noEmit": true,
|
|
13
|
+
"outDir": "dist"
|
|
14
|
+
},
|
|
15
|
+
"include": ["src"],
|
|
16
|
+
"exclude": ["node_modules"]
|
|
17
|
+
}
|