|
85 | 85 | "description": "Converts RGB color values to hexadecimal color code.",
|
86 | 86 | "author": "jjcantu",
|
87 | 87 | "tags": [
|
88 |
| - "javascript", |
89 | 88 | "color",
|
90 |
| - "conversion", |
91 |
| - "utility" |
| 89 | + "conversion" |
92 | 90 | ],
|
93 | 91 | "contributors": [],
|
94 | 92 | "code": "function rgbToHex(r, g, b) {\n const toHex = (n) => {\n const hex = n.toString(16);\n return hex.length === 1 ? '0' + hex : hex;\n };\n \n return '#' + toHex(r) + toHex(g) + toHex(b);\n}\n\n// Usage:\nconsole.log(rgbToHex(255, 128, 0)); // Output: \"#ff8000\"\nconsole.log(rgbToHex(0, 255, 0)); // Output: \"#00ff00\"\n"
|
|
366 | 364 | }
|
367 | 365 | ]
|
368 | 366 | },
|
| 367 | + { |
| 368 | + "categoryName": "Mathematical Functions", |
| 369 | + "snippets": [ |
| 370 | + { |
| 371 | + "title": "Greatest Common Divisor", |
| 372 | + "description": "Calculates the largest positive integer that divides each of the integers without leaving a remainder. Useful for calculating aspect ratios.", |
| 373 | + "author": "JanluOfficial", |
| 374 | + "tags": [ |
| 375 | + "math", |
| 376 | + "division" |
| 377 | + ], |
| 378 | + "contributors": [], |
| 379 | + "code": "function gcd(a, b) {\n while (b !== 0) {\n let temp = b;\n b = a % b;\n a = temp;\n }\n return a;\n}\n\n// Usage:\ngcd(1920, 1080); // Returns: 120\ngcd(1920, 1200); // Returns: 240\ngcd(5,12); // Returns: 1\n" |
| 380 | + } |
| 381 | + ] |
| 382 | + }, |
369 | 383 | {
|
370 | 384 | "categoryName": "Number Formatting",
|
371 | 385 | "snippets": [
|
|
407 | 421 | "description": "Converts bytes into human-readable file size format.",
|
408 | 422 | "author": "jjcantu",
|
409 | 423 | "tags": [
|
410 |
| - "javascript", |
411 | 424 | "format",
|
412 |
| - "size", |
413 |
| - "utility" |
| 425 | + "size" |
414 | 426 | ],
|
415 | 427 | "contributors": [],
|
416 | 428 | "code": "function formatFileSize(bytes) {\n if (bytes === 0) return '0 Bytes';\n \n const k = 1024;\n const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB'];\n const i = Math.floor(Math.log(bytes) / Math.log(k));\n \n return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + ' ' + sizes[i];\n}\n\n// Usage:\nconsole.log(formatFileSize(1234)); // Output: \"1.21 KB\"\nconsole.log(formatFileSize(1234567)); // Output: \"1.18 MB\"\n"
|
|
506 | 518 | "description": "Creates a deep copy of an object or array without reference.",
|
507 | 519 | "author": "jjcantu",
|
508 | 520 | "tags": [
|
509 |
| - "javascript", |
510 | 521 | "object",
|
511 |
| - "clone", |
512 |
| - "utility" |
| 522 | + "clone" |
513 | 523 | ],
|
514 | 524 | "contributors": [],
|
515 |
| - "code": "function deepClone(obj) {\n if (obj === null || typeof obj !== 'object') return obj;\n \n const clone = Array.isArray(obj) ? [] : {};\n \n for (let key in obj) {\n if (Object.prototype.hasOwnProperty.call(obj, key)) {\n clone[key] = deepClone(obj[key]);\n }\n }\n \n return clone;\n}\n\n// Usage:\nconst original = { a: 1, b: { c: 2 }, d: [1, 2, 3] };\nconst cloned = deepClone(original);\nconsole.log(cloned); // Output: { a: 1, b: { c: 2 }, d: [1, 2, 3] }\n" |
| 525 | + "code": "function deepClone(obj) {\n if (obj === null || typeof obj !== 'object') return obj;\n \n const clone = Array.isArray(obj) ? [] : {};\n \n for (let key in obj) {\n if (Object.prototype.hasOwnProperty.call(obj, key)) {\n clone[key] = deepClone(obj[key]);\n }\n }\n \n return clone;\n}\n\n// Usage:\nconst original = { a: 1, b: { c: 2 }, d: [1, 2, 3] };\nconst cloned = deepClone(original);\nconsole.log(cloned); // Output: 'original' but cloned\n" |
516 | 526 | },
|
517 | 527 | {
|
518 | 528 | "title": "Filter Object",
|
|
758 | 768 | "description": "Generates a UUID (v4) string.",
|
759 | 769 | "author": "jjcantu",
|
760 | 770 | "tags": [
|
761 |
| - "javascript", |
762 | 771 | "uuid",
|
763 |
| - "utility" |
| 772 | + "generate", |
| 773 | + "string" |
764 | 774 | ],
|
765 | 775 | "contributors": [],
|
766 | 776 | "code": "function generateUUID() {\n return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {\n const r = Math.random() * 16 | 0;\n const v = c === 'x' ? r : (r & 0x3 | 0x8);\n return v.toString(16);\n });\n}\n\n// Usage:\nconsole.log(generateUUID()); // Output: \"a1b2c3d4-e5f6-4g7h-8i9j-k0l1m2n3o4p5\"\n"
|
|
0 commit comments