|
77 | 77 | }
|
78 | 78 | ]
|
79 | 79 | },
|
| 80 | + { |
| 81 | + "categoryName": "Color Manipulation", |
| 82 | + "snippets": [ |
| 83 | + { |
| 84 | + "title": "RGB to Hex Color", |
| 85 | + "description": "Converts RGB color values to hexadecimal color code.", |
| 86 | + "author": "jjcantu", |
| 87 | + "tags": [ |
| 88 | + "javascript", |
| 89 | + "color", |
| 90 | + "conversion", |
| 91 | + "utility" |
| 92 | + ], |
| 93 | + "contributors": [], |
| 94 | + "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" |
| 95 | + } |
| 96 | + ] |
| 97 | + }, |
80 | 98 | {
|
81 | 99 | "categoryName": "Date And Time",
|
82 | 100 | "snippets": [
|
|
384 | 402 | "contributors": [],
|
385 | 403 | "code": "const toScientificNotation = (num) => {\n if (isNaN(num)) {\n throw new Error('Input must be a number');\n }\n if (num === 0) {\n return '0e+0';\n }\n const exponent = Math.floor(Math.log10(Math.abs(num)));\n const mantissa = num / Math.pow(10, exponent);\n return `${mantissa.toFixed(2)}e${exponent >= 0 ? '+' : ''}${exponent}`;\n};\n\n// Usage:\ntoScientificNotation(12345); // Returns: '1.23e+4'\ntoScientificNotation(0.0005678); // Returns: '5.68e-4'\ntoScientificNotation(1000); // Returns: '1.00e+3'\ntoScientificNotation(0); // Returns: '0e+0'\ntoScientificNotation(-54321); // Returns: '-5.43e+4'\n"
|
386 | 404 | },
|
| 405 | + { |
| 406 | + "title": "Format File Size", |
| 407 | + "description": "Converts bytes into human-readable file size format.", |
| 408 | + "author": "jjcantu", |
| 409 | + "tags": [ |
| 410 | + "javascript", |
| 411 | + "format", |
| 412 | + "size", |
| 413 | + "utility" |
| 414 | + ], |
| 415 | + "contributors": [], |
| 416 | + "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" |
| 417 | + }, |
387 | 418 | {
|
388 | 419 | "title": "Format Number with Commas",
|
389 | 420 | "description": "Formats a number with commas for better readability (e.g., 1000 -> 1,000).",
|
|
470 | 501 | "contributors": [],
|
471 | 502 | "code": "function countProperties(obj) {\n return Object.keys(obj).length;\n}\n\n// Usage:\nconst obj = { a: 1, b: 2, c: 3 };\ncountProperties(obj); // Returns: 3\n"
|
472 | 503 | },
|
| 504 | + { |
| 505 | + "title": "Deep Clone Object", |
| 506 | + "description": "Creates a deep copy of an object or array without reference.", |
| 507 | + "author": "jjcantu", |
| 508 | + "tags": [ |
| 509 | + "javascript", |
| 510 | + "object", |
| 511 | + "clone", |
| 512 | + "utility" |
| 513 | + ], |
| 514 | + "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" |
| 516 | + }, |
473 | 517 | {
|
474 | 518 | "title": "Filter Object",
|
475 | 519 | "description": "Filter out entries in an object where the value is falsy, including empty strings, empty objects, null, and undefined.",
|
|
709 | 753 | "contributors": [],
|
710 | 754 | "code": "function getInitials(name) {\n return name.split(' ').map(part => part.charAt(0).toUpperCase()).join('');\n}\n\n// Usage:\ngetInitials('John Doe'); // Returns: 'JD'\n"
|
711 | 755 | },
|
| 756 | + { |
| 757 | + "title": "Generate UUID", |
| 758 | + "description": "Generates a UUID (v4) string.", |
| 759 | + "author": "jjcantu", |
| 760 | + "tags": [ |
| 761 | + "javascript", |
| 762 | + "uuid", |
| 763 | + "utility" |
| 764 | + ], |
| 765 | + "contributors": [], |
| 766 | + "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" |
| 767 | + }, |
712 | 768 | {
|
713 | 769 | "title": "Mask Sensitive Information",
|
714 | 770 | "description": "Masks parts of a sensitive string, like a credit card or email address.",
|
|
0 commit comments