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.
@@ -0,0 +1,391 @@
1
+ import { describe, it, expect } from "vitest";
2
+ import {
3
+ capitalizeFirstLetter,
4
+ titleCase,
5
+ getInitials,
6
+ getTruncateDescription,
7
+ compareArrays,
8
+ generateOTPWithLength,
9
+ generateRandomPassword,
10
+ slugify,
11
+ setLocalStorage,
12
+ getLocalStorage,
13
+ removeLocalStorage,
14
+ clearLocalStorage,
15
+ getGreeting,
16
+ validateText,
17
+ validateEmail,
18
+ validatePassword,
19
+ validateMobileNumber,
20
+ formatCurrency,
21
+ removeDuplicatesFromArray,
22
+ sortArrayByDirection,
23
+ reverseString,
24
+ isObjectValuesEmpty,
25
+ checkNotNullAndNotEmpty,
26
+ handleNaN,
27
+ isFalsy,
28
+ isTruthy,
29
+ capitalizeEveryWord,
30
+ camelCase,
31
+ pascalCase,
32
+ truncate,
33
+ formatDate,
34
+ deepClone,
35
+ debounce,
36
+ formatDateRelative,
37
+ groupBy,
38
+ } from "../src";
39
+
40
+ describe("New Functions", () => {
41
+ describe("capitalizeEveryWord", () => {
42
+ it("capitalizes first letter of each word", () => {
43
+ expect(capitalizeEveryWord("hello world")).toBe("Hello World");
44
+ });
45
+ it("handles single word", () => {
46
+ expect(capitalizeEveryWord("test")).toBe("Test");
47
+ });
48
+ it("handles empty string", () => {
49
+ expect(capitalizeEveryWord("")).toBe("");
50
+ });
51
+ });
52
+
53
+ describe("camelCase", () => {
54
+ it("converts to camelCase", () => {
55
+ expect(camelCase("hello world")).toBe("helloWorld");
56
+ });
57
+ it("handles underscores", () => {
58
+ expect(camelCase("hello_world")).toBe("helloWorld");
59
+ });
60
+ });
61
+
62
+ describe("pascalCase", () => {
63
+ it("converts to PascalCase", () => {
64
+ expect(pascalCase("hello world")).toBe("HelloWorld");
65
+ });
66
+ });
67
+
68
+ describe("truncate", () => {
69
+ it("truncates with default maxLength", () => {
70
+ expect(truncate("Hello World User")).toBe("Hello World User");
71
+ });
72
+ it("truncates with custom maxLength", () => {
73
+ expect(truncate("Hello World User", 5)).toBe("Hello...");
74
+ });
75
+ it("handles empty string", () => {
76
+ expect(truncate("")).toBe("");
77
+ });
78
+ });
79
+
80
+ describe("formatDate", () => {
81
+ it("formats current date", () => {
82
+ const result = formatDate(new Date());
83
+ expect(result).toMatch(/^\d{4}-\d{2}-\d{2}$/);
84
+ });
85
+ it("accepts string date", () => {
86
+ expect(formatDate("2024-01-15")).toBe("2024-01-15");
87
+ });
88
+ });
89
+
90
+ describe("deepClone", () => {
91
+ it("creates deep copy of object", () => {
92
+ const original = { a: 1, b: { c: 2 } };
93
+ const cloned = deepClone(original);
94
+ cloned.b.c = 99;
95
+ expect(original.b.c).toBe(2);
96
+ });
97
+ it("handles arrays", () => {
98
+ const original = [1, 2, 3];
99
+ const cloned = deepClone(original);
100
+ cloned.push(4);
101
+ expect(original.length).toBe(3);
102
+ });
103
+ });
104
+
105
+ describe("debounce", () => {
106
+ it("returns a function", () => {
107
+ const fn = debounce(() => {}, 100);
108
+ expect(typeof fn).toBe("function");
109
+ });
110
+ });
111
+
112
+ describe("formatDateRelative", () => {
113
+ it("returns 'just now' for current date", () => {
114
+ expect(formatDateRelative(new Date())).toBe("just now");
115
+ });
116
+ it("formats past date", () => {
117
+ expect(formatDateRelative(new Date(Date.now() - 2 * 60 * 60 * 1000))).toContain("hours ago");
118
+ });
119
+ });
120
+ });
121
+
122
+ describe("Improved Functions", () => {
123
+ describe("capitalizeFirstLetter", () => {
124
+ it("capitalizes first letter", () => {
125
+ expect(capitalizeFirstLetter("test")).toBe("Test");
126
+ });
127
+ it("returns null for null input", () => {
128
+ expect(capitalizeFirstLetter(null)).toBeNull();
129
+ });
130
+ // Note: empty string returns null per implementation
131
+ });
132
+
133
+ describe("titleCase", () => {
134
+ it("converts to title case", () => {
135
+ expect(titleCase("hello world")).toBe("Hello World");
136
+ });
137
+ it("handles underscore separator", () => {
138
+ expect(titleCase("hello_world", "_")).toBe("Hello_World");
139
+ });
140
+ });
141
+
142
+ describe("getInitials", () => {
143
+ it("gets initials from full name", () => {
144
+ expect(getInitials("John Doe")).toBe("JD");
145
+ });
146
+ it("gets first and last letter for single word", () => {
147
+ // Implementation returns uppercase first+last
148
+ expect(getInitials("hello")).toBe("HO");
149
+ });
150
+ it("handles names with hyphens", () => {
151
+ expect(getInitials("user-name")).toBe("UN");
152
+ });
153
+ });
154
+
155
+ describe("getTruncateDescription", () => {
156
+ it("truncates with default length", () => {
157
+ expect(getTruncateDescription("Hello World User")).toBe("Hello World User");
158
+ });
159
+ it("truncates with custom length", () => {
160
+ expect(getTruncateDescription("Hello World User", 5)).toBe("Hello...");
161
+ });
162
+ });
163
+
164
+ describe("compareArrays", () => {
165
+ it("returns true for equal arrays", () => {
166
+ expect(compareArrays([1, 2, 3], [3, 1, 2])).toBe(true);
167
+ });
168
+ it("returns false for different arrays", () => {
169
+ expect(compareArrays([1, 2], [1, 2, 3])).toBe(false);
170
+ });
171
+ });
172
+
173
+ describe("validateMobileNumber", () => {
174
+ it("validates string mobile number", () => {
175
+ const result = validateMobileNumber("1234567890");
176
+ expect(result.isError).toBe(false);
177
+ });
178
+ it("validates number mobile number", () => {
179
+ const result = validateMobileNumber(1234567890);
180
+ expect(result.isError).toBe(false);
181
+ });
182
+ it("rejects empty input", () => {
183
+ const result = validateMobileNumber("");
184
+ expect(result.isError).toBe(true);
185
+ });
186
+ });
187
+
188
+ describe("validateEmail", () => {
189
+ it("validates valid email", () => {
190
+ const result = validateEmail("test@example.com");
191
+ expect(result.isError).toBe(false);
192
+ });
193
+ it("rejects invalid email", () => {
194
+ const result = validateEmail("invalid");
195
+ expect(result.isError).toBe(true);
196
+ });
197
+ });
198
+
199
+ describe("sortArrayByDirection", () => {
200
+ it("sorts array in descending order", () => {
201
+ expect(sortArrayByDirection([3, 1, 2], "desc")).toEqual([3, 2, 1]);
202
+ });
203
+ it("sorts array in ascending order", () => {
204
+ expect(sortArrayByDirection([3, 1, 2], "asc")).toEqual([1, 2, 3]);
205
+ });
206
+ it("sorts objects by key", () => {
207
+ expect(sortArrayByDirection([{ a: 1 }, { a: 3 }], "desc", "a")).toEqual([
208
+ { a: 3 },
209
+ { a: 1 },
210
+ ]);
211
+ });
212
+ });
213
+
214
+ describe("isObjectValuesEmpty", () => {
215
+ it("detects empty values", () => {
216
+ const result = isObjectValuesEmpty({ a: "", b: 1 });
217
+ expect(result.isEmpty).toBe(true);
218
+ expect(result.emptyKeys).toContain("a");
219
+ });
220
+ it("returns false for non-empty object", () => {
221
+ const result = isObjectValuesEmpty({ a: 1, b: 2 });
222
+ expect(result.isEmpty).toBe(false);
223
+ expect(result.emptyKeys).toEqual([]);
224
+ });
225
+ });
226
+ });
227
+
228
+ describe("Original Functions", () => {
229
+ describe("capitalizeFirstLetter", () => {
230
+ it("capitalizes first letter", () => {
231
+ expect(capitalizeFirstLetter("hello")).toBe("Hello");
232
+ });
233
+ });
234
+
235
+ describe("titleCase", () => {
236
+ it("converts to title case", () => {
237
+ expect(titleCase("hello world")).toBe("Hello World");
238
+ });
239
+ });
240
+
241
+ describe("getInitials", () => {
242
+ it("gets initials", () => {
243
+ expect(getInitials("John Doe")).toBe("JD");
244
+ });
245
+ });
246
+
247
+ describe("getTruncateDescription", () => {
248
+ it("truncates description", () => {
249
+ expect(getTruncateDescription("Hello World User")).toBe("Hello World User");
250
+ });
251
+ });
252
+
253
+ describe("compareArrays", () => {
254
+ it("compares arrays", () => {
255
+ expect(compareArrays([1, 2, 3], [3, 1, 2])).toBe(true);
256
+ });
257
+ });
258
+
259
+ describe("generateOTPWithLength", () => {
260
+ it("generates OTP with length", () => {
261
+ const otp = generateOTPWithLength(4);
262
+ // OTP is a number when isAlphaNumeric is false (default)
263
+ expect(typeof otp).toBe("number");
264
+ expect(otp).toBeGreaterThan(0);
265
+ });
266
+ });
267
+
268
+ describe("generateRandomPassword", () => {
269
+ it("generates random password", () => {
270
+ const password = generateRandomPassword(8);
271
+ expect(typeof password).toBe("string");
272
+ expect(password).toHaveLength(8);
273
+ });
274
+ });
275
+
276
+ describe("slugify", () => {
277
+ it("creates slug", () => {
278
+ expect(slugify("some string")).toBe("some-string");
279
+ });
280
+ it("custom separator", () => {
281
+ expect(slugify("some string", "_")).toBe("some_string");
282
+ });
283
+ });
284
+
285
+ describe("validateText", () => {
286
+ it("validates text", () => {
287
+ const result = validateText("John Doe");
288
+ expect(result.isError).toBe(false);
289
+ });
290
+ });
291
+
292
+ describe("validateEmail", () => {
293
+ it("validates email", () => {
294
+ const result = validateEmail("test@example.com");
295
+ expect(result.isError).toBe(false);
296
+ });
297
+ });
298
+
299
+ describe("validatePassword", () => {
300
+ it("validates password", () => {
301
+ const result = validatePassword("Abc@1234");
302
+ expect(result.isError).toBe(false);
303
+ });
304
+ });
305
+
306
+ describe("validateMobileNumber", () => {
307
+ it("validates mobile number", () => {
308
+ const result = validateMobileNumber(1234567890);
309
+ expect(result.isError).toBe(false);
310
+ });
311
+ });
312
+
313
+ describe("formatCurrency", () => {
314
+ it("formats currency", () => {
315
+ const result = formatCurrency(12345.67);
316
+ expect(typeof result).toBe("string");
317
+ expect(result).toContain("$");
318
+ });
319
+ });
320
+
321
+ describe("removeDuplicatesFromArray", () => {
322
+ it("removes duplicates", () => {
323
+ expect(removeDuplicatesFromArray([1, 2, 3, 2, 4])).toEqual([1, 2, 3, 4]);
324
+ });
325
+ });
326
+
327
+ describe("sortArrayByDirection (original)", () => {
328
+ it("sorts descending", () => {
329
+ expect(sortArrayByDirection([3, 1, 4, 1, 5], "desc")).toEqual([5, 4, 3, 1, 1]);
330
+ });
331
+ });
332
+
333
+ describe("reverseString", () => {
334
+ it("reverses string", () => {
335
+ expect(reverseString("hello")).toBe("olleh");
336
+ });
337
+ });
338
+
339
+ describe("isObjectValuesEmpty", () => {
340
+ it("detects empty values", () => {
341
+ const result = isObjectValuesEmpty({ a: "", b: 1 });
342
+ expect(result.isEmpty).toBe(true);
343
+ });
344
+ });
345
+
346
+ describe("checkNotNullAndNotEmpty", () => {
347
+ it("returns true for valid value", () => {
348
+ expect(checkNotNullAndNotEmpty("Hello")).toBe(true);
349
+ });
350
+ it("returns false for empty string", () => {
351
+ expect(checkNotNullAndNotEmpty("")).toBe(false);
352
+ });
353
+ });
354
+
355
+ describe("handleNaN", () => {
356
+ it("returns fallback for NaN", () => {
357
+ expect(handleNaN(NaN)).toBe(0);
358
+ });
359
+ it("returns value for non-NaN", () => {
360
+ expect(handleNaN(42)).toBe(42);
361
+ });
362
+ });
363
+
364
+ describe("isFalsy", () => {
365
+ it("detects falsy values", () => {
366
+ expect(isFalsy(null)).toBe(true);
367
+ expect(isFalsy("")).toBe(true);
368
+ expect(isFalsy(0)).toBe(true);
369
+ });
370
+ });
371
+
372
+ describe("isTruthy", () => {
373
+ it("detects truthy values", () => {
374
+ expect(isTruthy(1)).toBe(true);
375
+ expect(isTruthy("hello")).toBe(true);
376
+ });
377
+ });
378
+
379
+ describe("groupBy", () => {
380
+ it("groups objects by key", () => {
381
+ const data = [
382
+ { id: 1, category: "A" },
383
+ { id: 2, category: "B" },
384
+ { id: 3, category: "A" },
385
+ ];
386
+ const result = groupBy(data, "category");
387
+ expect(result).toHaveProperty("A");
388
+ expect(result).toHaveProperty("B");
389
+ });
390
+ });
391
+ });