-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutility-methods.js
More file actions
828 lines (674 loc) · 26.4 KB
/
utility-methods.js
File metadata and controls
828 lines (674 loc) · 26.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
/**
* Utility Methods Collection
*
* Description:
* This file contains various utility functions that are commonly used
* in JavaScript development. These include type checking, data manipulation,
* validation, formatting, and general helper functions.
*/
// ========================================
// TYPE CHECKING UTILITIES
// ========================================
console.log("=== Type Checking Utilities ===\n");
// Example 1: Robust type checking functions
console.log("Example 1: Type checking functions");
const TypeChecker = {
isString: (value) => typeof value === 'string',
isNumber: (value) => typeof value === 'number' && !isNaN(value),
isBoolean: (value) => typeof value === 'boolean',
isFunction: (value) => typeof value === 'function',
isObject: (value) => value !== null && typeof value === 'object' && !Array.isArray(value),
isArray: (value) => Array.isArray(value),
isNull: (value) => value === null,
isUndefined: (value) => value === undefined,
isNullOrUndefined: (value) => value == null,
isEmpty: (value) => {
if (value == null) return true;
if (typeof value === 'string' || Array.isArray(value)) return value.length === 0;
if (typeof value === 'object') return Object.keys(value).length === 0;
return false;
},
isDate: (value) => value instanceof Date && !isNaN(value.getTime()),
isRegExp: (value) => value instanceof RegExp,
isPromise: (value) => value && typeof value.then === 'function',
// Get detailed type information
getType: (value) => {
if (value === null) return 'null';
if (Array.isArray(value)) return 'array';
if (value instanceof Date) return 'date';
if (value instanceof RegExp) return 'regexp';
return typeof value;
}
};
// Test type checking
const testValues = [
42, "hello", true, [], {}, null, undefined,
new Date(), /regex/, Promise.resolve(), function() {}
];
console.log("Type checking results:");
testValues.forEach(value => {
console.log(`${JSON.stringify(value)}: ${TypeChecker.getType(value)}`);
});
console.log("\nEmpty value checks:");
const emptyValues = ["", [], {}, null, undefined, 0];
emptyValues.forEach(value => {
console.log(`${JSON.stringify(value)} is empty: ${TypeChecker.isEmpty(value)}`);
});
console.log();
// ========================================
// DEEP CLONING AND COMPARISON
// ========================================
console.log("=== Deep Cloning and Comparison ===\n");
// Example 2: Deep clone implementation
console.log("Example 2: Deep cloning");
function deepClone(obj) {
// Handle null and non-objects
if (obj === null || typeof obj !== 'object') {
return obj;
}
// Handle Date
if (obj instanceof Date) {
return new Date(obj.getTime());
}
// Handle Array
if (Array.isArray(obj)) {
return obj.map(item => deepClone(item));
}
// Handle RegExp
if (obj instanceof RegExp) {
return new RegExp(obj.source, obj.flags);
}
// Handle Objects
const cloned = {};
for (let key in obj) {
if (obj.hasOwnProperty(key)) {
cloned[key] = deepClone(obj[key]);
}
}
return cloned;
}
const originalObject = {
name: "John",
age: 30,
hobbies: ["reading", "coding"],
address: {
street: "123 Main St",
city: "Anytown"
},
birthDate: new Date("1993-01-01"),
pattern: /[a-z]+/gi
};
const clonedObject = deepClone(originalObject);
console.log("Original:", originalObject);
console.log("Cloned:", clonedObject);
// Modify original to test deep cloning
originalObject.hobbies.push("swimming");
originalObject.address.city = "New City";
console.log("After modifying original:");
console.log("Original hobbies:", originalObject.hobbies);
console.log("Cloned hobbies:", clonedObject.hobbies);
console.log("Original city:", originalObject.address.city);
console.log("Cloned city:", clonedObject.address.city);
console.log();
// Example 3: Deep comparison
console.log("Example 3: Deep comparison");
function deepEqual(a, b) {
if (a === b) return true;
if (a === null || b === null) return false;
if (typeof a !== 'object' || typeof b !== 'object') return false;
// Handle arrays
if (Array.isArray(a) !== Array.isArray(b)) return false;
// Handle dates
if (a instanceof Date && b instanceof Date) {
return a.getTime() === b.getTime();
}
const keysA = Object.keys(a);
const keysB = Object.keys(b);
if (keysA.length !== keysB.length) return false;
for (let key of keysA) {
if (!keysB.includes(key)) return false;
if (!deepEqual(a[key], b[key])) return false;
}
return true;
}
const obj1 = { a: 1, b: { c: 2, d: [3, 4] } };
const obj2 = { a: 1, b: { c: 2, d: [3, 4] } };
const obj3 = { a: 1, b: { c: 2, d: [3, 5] } };
console.log("obj1 == obj2:", deepEqual(obj1, obj2)); // true
console.log("obj1 == obj3:", deepEqual(obj1, obj3)); // false
console.log();
// ========================================
// ARRAY UTILITIES
// ========================================
console.log("=== Array Utilities ===\n");
// Example 4: Array utility functions
console.log("Example 4: Array utilities");
const ArrayUtils = {
// Remove duplicates
unique: (arr) => [...new Set(arr)],
// Flatten nested arrays
flatten: (arr) => arr.reduce((flat, item) =>
Array.isArray(item) ? flat.concat(ArrayUtils.flatten(item)) : flat.concat(item), []),
// Chunk array into smaller arrays
chunk: (arr, size) => {
const chunks = [];
for (let i = 0; i < arr.length; i += size) {
chunks.push(arr.slice(i, i + size));
}
return chunks;
},
// Get random element
sample: (arr) => arr[Math.floor(Math.random() * arr.length)],
// Shuffle array
shuffle: (arr) => {
const shuffled = [...arr];
for (let i = shuffled.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[shuffled[i], shuffled[j]] = [shuffled[j], shuffled[i]];
}
return shuffled;
},
// Group by property
groupBy: (arr, key) => {
return arr.reduce((groups, item) => {
const groupKey = typeof key === 'function' ? key(item) : item[key];
groups[groupKey] = groups[groupKey] || [];
groups[groupKey].push(item);
return groups;
}, {});
},
// Find differences between arrays
difference: (arr1, arr2) => arr1.filter(item => !arr2.includes(item)),
// Find intersection
intersection: (arr1, arr2) => arr1.filter(item => arr2.includes(item)),
// Partition array based on predicate
partition: (arr, predicate) => {
return arr.reduce((acc, item) => {
acc[predicate(item) ? 0 : 1].push(item);
return acc;
}, [[], []]);
}
};
// Test array utilities
const numbers = [1, 2, 2, 3, 4, 4, 5];
const nestedArray = [1, [2, 3], [4, [5, 6]], 7];
const people = [
{ name: "John", age: 25, department: "IT" },
{ name: "Jane", age: 30, department: "HR" },
{ name: "Bob", age: 35, department: "IT" },
{ name: "Alice", age: 28, department: "HR" }
];
console.log("Original numbers:", numbers);
console.log("Unique:", ArrayUtils.unique(numbers));
console.log("Chunked (size 3):", ArrayUtils.chunk(numbers, 3));
console.log("Sample:", ArrayUtils.sample(numbers));
console.log("Shuffled:", ArrayUtils.shuffle(numbers));
console.log("\nNested array:", nestedArray);
console.log("Flattened:", ArrayUtils.flatten(nestedArray));
console.log("\nPeople grouped by department:");
console.log(ArrayUtils.groupBy(people, 'department'));
console.log("\nArray operations:");
const arr1 = [1, 2, 3, 4];
const arr2 = [3, 4, 5, 6];
console.log("Array 1:", arr1);
console.log("Array 2:", arr2);
console.log("Difference (arr1 - arr2):", ArrayUtils.difference(arr1, arr2));
console.log("Intersection:", ArrayUtils.intersection(arr1, arr2));
const [evens, odds] = ArrayUtils.partition(numbers, n => n % 2 === 0);
console.log("Evens:", evens);
console.log("Odds:", odds);
console.log();
// ========================================
// STRING UTILITIES
// ========================================
console.log("=== String Utilities ===\n");
// Example 5: String utility functions
console.log("Example 5: String utilities");
const StringUtils = {
// Capitalize first letter
capitalize: (str) => str.charAt(0).toUpperCase() + str.slice(1).toLowerCase(),
// Convert to title case
titleCase: (str) => str.replace(/\w\S*/g, txt =>
txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase()),
// Convert to camelCase
camelCase: (str) => str.replace(/[-_\s]+(.)?/g, (_, char) =>
char ? char.toUpperCase() : ''),
// Convert to kebab-case
kebabCase: (str) => str.replace(/([a-z])([A-Z])/g, '$1-$2')
.replace(/\s+/g, '-').toLowerCase(),
// Convert to snake_case
snakeCase: (str) => str.replace(/([a-z])([A-Z])/g, '$1_$2')
.replace(/\s+/g, '_').toLowerCase(),
// Truncate with ellipsis
truncate: (str, length, suffix = '...') =>
str.length > length ? str.slice(0, length) + suffix : str,
// Count words
wordCount: (str) => str.trim().split(/\s+/).filter(word => word.length > 0).length,
// Reverse string
reverse: (str) => str.split('').reverse().join(''),
// Check if palindrome
isPalindrome: (str) => {
const cleaned = str.toLowerCase().replace(/[^a-z0-9]/g, '');
return cleaned === cleaned.split('').reverse().join('');
},
// Generate random string
random: (length = 8, chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789') => {
let result = '';
for (let i = 0; i < length; i++) {
result += chars.charAt(Math.floor(Math.random() * chars.length));
}
return result;
},
// Escape HTML
escapeHtml: (str) => str.replace(/[&<>"']/g, (match) => {
const htmlEscapes = {
'&': '&',
'<': '<',
'>': '>',
'"': '"',
"'": '''
};
return htmlEscapes[match];
}),
// Strip HTML tags
stripHtml: (str) => str.replace(/<[^>]*>/g, ''),
// Format template
template: (str, vars) => str.replace(/\{\{(\w+)\}\}/g, (match, key) => vars[key] || match)
};
// Test string utilities
const testString = "hello world javascript";
console.log("Original:", testString);
console.log("Capitalize:", StringUtils.capitalize(testString));
console.log("Title case:", StringUtils.titleCase(testString));
console.log("Camel case:", StringUtils.camelCase(testString));
console.log("Kebab case:", StringUtils.kebabCase("Hello World JavaScript"));
console.log("Snake case:", StringUtils.snakeCase("Hello World JavaScript"));
const longString = "This is a very long string that needs to be truncated";
console.log("Truncated:", StringUtils.truncate(longString, 20));
console.log("Word count:", StringUtils.wordCount(longString));
console.log("Reversed:", StringUtils.reverse("hello"));
console.log("Is 'racecar' a palindrome?", StringUtils.isPalindrome("racecar"));
console.log("Is 'A man a plan a canal Panama' a palindrome?",
StringUtils.isPalindrome("A man a plan a canal Panama"));
console.log("Random string:", StringUtils.random(12));
const htmlString = '<p>Hello "World" & <script>alert("test")</script></p>';
console.log("HTML escaped:", StringUtils.escapeHtml(htmlString));
console.log("HTML stripped:", StringUtils.stripHtml(htmlString));
const template = "Hello {{name}}, welcome to {{place}}!";
const templateVars = { name: "John", place: "our website" };
console.log("Template result:", StringUtils.template(template, templateVars));
console.log();
// ========================================
// NUMBER AND MATH UTILITIES
// ========================================
console.log("=== Number and Math Utilities ===\n");
// Example 6: Number utility functions
console.log("Example 6: Number utilities");
const NumberUtils = {
// Round to specified decimal places
round: (num, decimals = 0) => {
const factor = Math.pow(10, decimals);
return Math.round(num * factor) / factor;
},
// Clamp number between min and max
clamp: (num, min, max) => Math.min(Math.max(num, min), max),
// Check if number is in range
inRange: (num, min, max) => num >= min && num <= max,
// Generate random number in range
randomBetween: (min, max) => Math.random() * (max - min) + min,
// Generate random integer in range
randomInt: (min, max) => Math.floor(Math.random() * (max - min + 1)) + min,
// Calculate percentage
percentage: (part, total) => total === 0 ? 0 : (part / total) * 100,
// Format currency
formatCurrency: (amount, currency = 'USD', locale = 'en-US') => {
return new Intl.NumberFormat(locale, {
style: 'currency',
currency: currency
}).format(amount);
},
// Format with commas
formatNumber: (num, locale = 'en-US') => {
return new Intl.NumberFormat(locale).format(num);
},
// Convert bytes to human readable format
formatBytes: (bytes, decimals = 2) => {
if (bytes === 0) return '0 Bytes';
const k = 1024;
const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB'];
const i = Math.floor(Math.log(bytes) / Math.log(k));
return parseFloat((bytes / Math.pow(k, i)).toFixed(decimals)) + ' ' + sizes[i];
},
// Calculate average
average: (numbers) => numbers.reduce((sum, num) => sum + num, 0) / numbers.length,
// Calculate median
median: (numbers) => {
const sorted = [...numbers].sort((a, b) => a - b);
const mid = Math.floor(sorted.length / 2);
return sorted.length % 2 === 0
? (sorted[mid - 1] + sorted[mid]) / 2
: sorted[mid];
},
// Calculate standard deviation
standardDeviation: (numbers) => {
const avg = NumberUtils.average(numbers);
const squaredDiffs = numbers.map(num => Math.pow(num - avg, 2));
const avgSquaredDiff = NumberUtils.average(squaredDiffs);
return Math.sqrt(avgSquaredDiff);
}
};
// Test number utilities
const testNumbers = [10, 15, 20, 25, 30, 35, 40];
console.log("Numbers:", testNumbers);
console.log("Average:", NumberUtils.average(testNumbers));
console.log("Median:", NumberUtils.median(testNumbers));
console.log("Standard deviation:", NumberUtils.round(NumberUtils.standardDeviation(testNumbers), 2));
console.log("\nNumber formatting:");
console.log("Round 3.14159 to 2 decimals:", NumberUtils.round(3.14159, 2));
console.log("Clamp 150 between 0-100:", NumberUtils.clamp(150, 0, 100));
console.log("Is 50 in range 25-75?", NumberUtils.inRange(50, 25, 75));
console.log("Random between 1-10:", NumberUtils.randomInt(1, 10));
console.log("25 is what % of 200?", NumberUtils.percentage(25, 200));
console.log("\nFormatted numbers:");
console.log("Currency:", NumberUtils.formatCurrency(1234.56));
console.log("Large number:", NumberUtils.formatNumber(1234567));
console.log("File size:", NumberUtils.formatBytes(1024 * 1024 * 2.5));
console.log();
// ========================================
// DATE UTILITIES
// ========================================
console.log("=== Date Utilities ===\n");
// Example 7: Date utility functions
console.log("Example 7: Date utilities");
const DateUtils = {
// Format date
format: (date, format = 'YYYY-MM-DD') => {
const d = new Date(date);
const year = d.getFullYear();
const month = String(d.getMonth() + 1).padStart(2, '0');
const day = String(d.getDate()).padStart(2, '0');
const hours = String(d.getHours()).padStart(2, '0');
const minutes = String(d.getMinutes()).padStart(2, '0');
const seconds = String(d.getSeconds()).padStart(2, '0');
return format
.replace('YYYY', year)
.replace('MM', month)
.replace('DD', day)
.replace('HH', hours)
.replace('mm', minutes)
.replace('ss', seconds);
},
// Add days to date
addDays: (date, days) => {
const result = new Date(date);
result.setDate(result.getDate() + days);
return result;
},
// Calculate difference in days
daysBetween: (date1, date2) => {
const oneDay = 24 * 60 * 60 * 1000;
const firstDate = new Date(date1);
const secondDate = new Date(date2);
return Math.round(Math.abs((firstDate - secondDate) / oneDay));
},
// Check if date is today
isToday: (date) => {
const today = new Date();
const checkDate = new Date(date);
return today.toDateString() === checkDate.toDateString();
},
// Check if date is weekend
isWeekend: (date) => {
const day = new Date(date).getDay();
return day === 0 || day === 6; // Sunday or Saturday
},
// Get start of day
startOfDay: (date) => {
const result = new Date(date);
result.setHours(0, 0, 0, 0);
return result;
},
// Get end of day
endOfDay: (date) => {
const result = new Date(date);
result.setHours(23, 59, 59, 999);
return result;
},
// Get relative time string
getRelativeTime: (date) => {
const now = new Date();
const diffMs = now - new Date(date);
const diffSeconds = Math.floor(diffMs / 1000);
const diffMinutes = Math.floor(diffSeconds / 60);
const diffHours = Math.floor(diffMinutes / 60);
const diffDays = Math.floor(diffHours / 24);
if (diffDays > 0) return `${diffDays} day${diffDays > 1 ? 's' : ''} ago`;
if (diffHours > 0) return `${diffHours} hour${diffHours > 1 ? 's' : ''} ago`;
if (diffMinutes > 0) return `${diffMinutes} minute${diffMinutes > 1 ? 's' : ''} ago`;
return 'Just now';
}
};
// Test date utilities
const now = new Date();
const pastDate = new Date('2023-01-01');
const futureDate = DateUtils.addDays(now, 30);
console.log("Current date:", DateUtils.format(now, 'YYYY-MM-DD HH:mm:ss'));
console.log("Past date:", DateUtils.format(pastDate, 'DD/MM/YYYY'));
console.log("Future date:", DateUtils.format(futureDate, 'YYYY-MM-DD'));
console.log("Days between now and past:", DateUtils.daysBetween(now, pastDate));
console.log("Is today today?", DateUtils.isToday(now));
console.log("Is past date weekend?", DateUtils.isWeekend(pastDate));
console.log("Start of today:", DateUtils.startOfDay(now));
console.log("End of today:", DateUtils.endOfDay(now));
// Test relative time with different dates
const oneHourAgo = new Date(Date.now() - 60 * 60 * 1000);
const oneDayAgo = new Date(Date.now() - 24 * 60 * 60 * 1000);
console.log("One hour ago:", DateUtils.getRelativeTime(oneHourAgo));
console.log("One day ago:", DateUtils.getRelativeTime(oneDayAgo));
console.log();
// ========================================
// VALIDATION UTILITIES
// ========================================
console.log("=== Validation Utilities ===\n");
// Example 8: Validation functions
console.log("Example 8: Validation utilities");
const ValidationUtils = {
// Email validation
isEmail: (email) => /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(email),
// Phone validation (US format)
isPhone: (phone) => /^\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$/.test(phone),
// URL validation
isUrl: (url) => {
try {
new URL(url);
return true;
} catch {
return false;
}
},
// Credit card validation (Luhn algorithm)
isCreditCard: (number) => {
const num = number.replace(/\D/g, '');
if (num.length < 13 || num.length > 19) return false;
let sum = 0;
let alternate = false;
for (let i = num.length - 1; i >= 0; i--) {
let digit = parseInt(num.charAt(i));
if (alternate) {
digit *= 2;
if (digit > 9) digit -= 9;
}
sum += digit;
alternate = !alternate;
}
return sum % 10 === 0;
},
// Password strength
getPasswordStrength: (password) => {
let score = 0;
const checks = {
length: password.length >= 8,
lowercase: /[a-z]/.test(password),
uppercase: /[A-Z]/.test(password),
numbers: /\d/.test(password),
symbols: /[!@#$%^&*(),.?":{}|<>]/.test(password)
};
score = Object.values(checks).filter(Boolean).length;
let strength = 'Very Weak';
if (score >= 2) strength = 'Weak';
if (score >= 3) strength = 'Medium';
if (score >= 4) strength = 'Strong';
if (score === 5) strength = 'Very Strong';
return { score, strength, checks };
},
// Custom validation builder
createValidator: (rules) => {
return (value) => {
const errors = [];
for (const [key, rule] of Object.entries(rules)) {
if (!rule.test(value)) {
errors.push(rule.message || `${key} validation failed`);
}
}
return {
isValid: errors.length === 0,
errors
};
};
}
};
// Test validation utilities
const testEmails = ['test@example.com', 'invalid.email', 'user@domain.co.uk'];
const testPhones = ['(555) 123-4567', '555-123-4567', '5551234567', 'invalid'];
const testUrls = ['https://example.com', 'ftp://files.example.com', 'not-a-url'];
console.log("Email validation:");
testEmails.forEach(email => {
console.log(` ${email}: ${ValidationUtils.isEmail(email)}`);
});
console.log("\nPhone validation:");
testPhones.forEach(phone => {
console.log(` ${phone}: ${ValidationUtils.isPhone(phone)}`);
});
console.log("\nURL validation:");
testUrls.forEach(url => {
console.log(` ${url}: ${ValidationUtils.isUrl(url)}`);
});
console.log("\nCredit card validation:");
const testCards = ['4532015112830366', '1234567890123456'];
testCards.forEach(card => {
console.log(` ${card}: ${ValidationUtils.isCreditCard(card)}`);
});
console.log("\nPassword strength:");
const testPasswords = ['weak', 'Password1', 'StrongP@ssw0rd!'];
testPasswords.forEach(password => {
const result = ValidationUtils.getPasswordStrength(password);
console.log(` "${password}": ${result.strength} (${result.score}/5)`);
});
// Custom validator example
const usernameValidator = ValidationUtils.createValidator({
minLength: {
test: (value) => value.length >= 3,
message: 'Username must be at least 3 characters'
},
maxLength: {
test: (value) => value.length <= 20,
message: 'Username must be no more than 20 characters'
},
alphanumeric: {
test: (value) => /^[a-zA-Z0-9_]+$/.test(value),
message: 'Username can only contain letters, numbers, and underscores'
}
});
console.log("\nCustom username validation:");
const testUsernames = ['ab', 'validUsername', 'invalid-username!', 'a'.repeat(25)];
testUsernames.forEach(username => {
const result = usernameValidator(username);
console.log(` "${username}": ${result.isValid ? 'Valid' : result.errors.join(', ')}`);
});
console.log();
// ========================================
// PERFORMANCE UTILITIES
// ========================================
console.log("=== Performance Utilities ===\n");
// Example 9: Performance measurement utilities
console.log("Example 9: Performance utilities");
const PerformanceUtils = {
// Measure function execution time
measureTime: async (fn, ...args) => {
const start = performance.now();
const result = await fn(...args);
const end = performance.now();
return {
result,
time: end - start
};
},
// Benchmark function multiple times
benchmark: async (fn, iterations = 1000, ...args) => {
const times = [];
for (let i = 0; i < iterations; i++) {
const start = performance.now();
await fn(...args);
const end = performance.now();
times.push(end - start);
}
const total = times.reduce((sum, time) => sum + time, 0);
const average = total / iterations;
const min = Math.min(...times);
const max = Math.max(...times);
return {
iterations,
total: NumberUtils.round(total, 3),
average: NumberUtils.round(average, 3),
min: NumberUtils.round(min, 3),
max: NumberUtils.round(max, 3)
};
},
// Throttle function calls
throttle: (func, limit) => {
let inThrottle;
return function(...args) {
if (!inThrottle) {
func.apply(this, args);
inThrottle = true;
setTimeout(() => inThrottle = false, limit);
}
};
},
// Debounce function calls
debounce: (func, delay) => {
let timeoutId;
return function(...args) {
clearTimeout(timeoutId);
timeoutId = setTimeout(() => func.apply(this, args), delay);
};
}
};
// Test performance utilities
function expensiveCalculation(n) {
let result = 0;
for (let i = 0; i < n; i++) {
result += Math.sqrt(i);
}
return result;
}
PerformanceUtils.measureTime(expensiveCalculation, 100000)
.then(({ result, time }) => {
console.log(`Calculation result: ${NumberUtils.round(result, 2)}`);
console.log(`Execution time: ${NumberUtils.round(time, 3)}ms`);
});
PerformanceUtils.benchmark(expensiveCalculation, 100, 10000)
.then(stats => {
console.log("Benchmark results:", stats);
});
console.log("\n=== Best Practices ===");
console.log("1. Use appropriate type checking for reliable code");
console.log("2. Implement deep cloning for complex object manipulation");
console.log("3. Leverage utility functions to avoid code duplication");
console.log("4. Validate user input thoroughly");
console.log("5. Use performance utilities to identify bottlenecks");
console.log("6. Choose the right data structure for your use case");
console.log("7. Handle edge cases in utility functions");
console.log("8. Consider using libraries like Lodash for production code");
console.log("9. Test utility functions thoroughly");
console.log("10. Document utility functions with clear examples");