|
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"
|
|
407 | 405 | "description": "Converts bytes into human-readable file size format.",
|
408 | 406 | "author": "jjcantu",
|
409 | 407 | "tags": [
|
410 | | - "javascript", |
411 | 408 | "format",
|
412 | | - "size", |
413 | | - "utility" |
| 409 | + "size" |
414 | 410 | ],
|
415 | 411 | "contributors": [],
|
416 | 412 | "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 | 502 | "description": "Creates a deep copy of an object or array without reference.",
|
507 | 503 | "author": "jjcantu",
|
508 | 504 | "tags": [
|
509 | | - "javascript", |
510 | 505 | "object",
|
511 | | - "clone", |
512 | | - "utility" |
| 506 | + "clone" |
513 | 507 | ],
|
514 | 508 | "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" |
| 509 | + "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 | 510 | },
|
517 | 511 | {
|
518 | 512 | "title": "Filter Object",
|
|
758 | 752 | "description": "Generates a UUID (v4) string.",
|
759 | 753 | "author": "jjcantu",
|
760 | 754 | "tags": [
|
761 | | - "javascript", |
762 | 755 | "uuid",
|
763 | | - "utility" |
| 756 | + "generate", |
| 757 | + "string" |
764 | 758 | ],
|
765 | 759 | "contributors": [],
|
766 | 760 | "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