|
109 | 109 | "description": "Converts RGB color values to hexadecimal color code.",
|
110 | 110 | "author": "jjcantu",
|
111 | 111 | "tags": [
|
112 |
| - "javascript", |
113 | 112 | "color",
|
114 |
| - "conversion", |
115 |
| - "utility" |
| 113 | + "conversion" |
116 | 114 | ],
|
117 |
| - "contributors": [], |
118 |
| - "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" |
| 115 | + "contributors": [ |
| 116 | + "jjcantu", |
| 117 | + "jjcantu", |
| 118 | + "jjcantu", |
| 119 | + "jjcantu", |
| 120 | + "jjcantu", |
| 121 | + "jjcantu" |
| 122 | + ], |
| 123 | + "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" |
119 | 124 | }
|
120 | 125 | ]
|
121 | 126 | },
|
|
570 | 575 | "description": "Converts bytes into human-readable file size format.",
|
571 | 576 | "author": "jjcantu",
|
572 | 577 | "tags": [
|
573 |
| - "javascript", |
574 | 578 | "format",
|
575 |
| - "size", |
576 |
| - "utility" |
| 579 | + "size" |
577 | 580 | ],
|
578 | 581 | "contributors": [],
|
579 | 582 | "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"
|
|
692 | 695 | "description": "Creates a deep copy of an object or array without reference.",
|
693 | 696 | "author": "jjcantu",
|
694 | 697 | "tags": [
|
695 |
| - "javascript", |
696 | 698 | "object",
|
697 |
| - "clone", |
698 |
| - "utility" |
| 699 | + "clone" |
699 | 700 | ],
|
700 | 701 | "contributors": [],
|
701 |
| - "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" |
| 702 | + "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" |
702 | 703 | },
|
703 | 704 | {
|
704 | 705 | "title": "Filter Object",
|
|
979 | 980 | "description": "Generates a UUID (v4) string.",
|
980 | 981 | "author": "jjcantu",
|
981 | 982 | "tags": [
|
982 |
| - "javascript", |
983 | 983 | "uuid",
|
984 |
| - "utility" |
| 984 | + "generate", |
| 985 | + "string" |
985 | 986 | ],
|
986 | 987 | "contributors": [],
|
987 | 988 | "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