From a50a47e0aad7ba225292db02c20407d7ba5f0e6d Mon Sep 17 00:00:00 2001 From: Jose Cantu Date: Fri, 3 Jan 2025 00:09:51 -0600 Subject: [PATCH 1/3] snippets - add a few useful JS snippets --- public/consolidated/javascript.json | 56 +++++++++++++++++++ .../color-manipulation/rgb-to-hex.md | 21 +++++++ .../number-formatting/format-file-size.md | 22 ++++++++ .../object-manipulation/deep-clone-object.md | 27 +++++++++ .../string-manipulation/generate-uuid.md | 19 +++++++ 5 files changed, 145 insertions(+) create mode 100644 snippets/javascript/color-manipulation/rgb-to-hex.md create mode 100644 snippets/javascript/number-formatting/format-file-size.md create mode 100644 snippets/javascript/object-manipulation/deep-clone-object.md create mode 100644 snippets/javascript/string-manipulation/generate-uuid.md diff --git a/public/consolidated/javascript.json b/public/consolidated/javascript.json index 7b1ceee5..e800f749 100644 --- a/public/consolidated/javascript.json +++ b/public/consolidated/javascript.json @@ -101,6 +101,24 @@ } ] }, + { + "categoryName": "Color Manipulation", + "snippets": [ + { + "title": "RGB to Hex Color", + "description": "Converts RGB color values to hexadecimal color code.", + "author": "jjcantu", + "tags": [ + "javascript", + "color", + "conversion", + "utility" + ], + "contributors": [], + "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" + } + ] + }, { "categoryName": "Date And Time", "snippets": [ @@ -547,6 +565,19 @@ "contributors": [], "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:\nconsole.log(toScientificNotation(12345)); // Output: '1.23e+4'\nconsole.log(toScientificNotation(0.0005678)); // Output: '5.68e-4'\nconsole.log(toScientificNotation(1000)); // Output: '1.00e+3'\nconsole.log(toScientificNotation(0)); // Output: '0e+0'\nconsole.log(toScientificNotation(-54321)); // Output: '-5.43e+4'\n" }, + { + "title": "Format File Size", + "description": "Converts bytes into human-readable file size format.", + "author": "jjcantu", + "tags": [ + "javascript", + "format", + "size", + "utility" + ], + "contributors": [], + "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" + }, { "title": "Format Number with Commas", "description": "Formats a number with commas for better readability (e.g., 1000 -> 1,000).", @@ -656,6 +687,19 @@ "contributors": [], "code": "function countProperties(obj) {\n return Object.keys(obj).length;\n}\n\n// Usage:\nconst obj = { a: 1, b: 2, c: 3 };\nconsole.log(countProperties(obj)); // Output: 3\n" }, + { + "title": "Deep Clone Object", + "description": "Creates a deep copy of an object or array without reference.", + "author": "jjcantu", + "tags": [ + "javascript", + "object", + "clone", + "utility" + ], + "contributors": [], + "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" + }, { "title": "Filter Object", "description": "Filter out entries in an object where the value is falsy, including empty strings, empty objects, null, and undefined.", @@ -930,6 +974,18 @@ "contributors": [], "code": "function getInitials(name) {\n return name.split(' ').map(part => part.charAt(0).toUpperCase()).join('');\n}\n\n// Example usage:\nconsole.log(getInitials('John Doe')); // Output: 'JD'\n" }, + { + "title": "Generate UUID", + "description": "Generates a UUID (v4) string.", + "author": "jjcantu", + "tags": [ + "javascript", + "uuid", + "utility" + ], + "contributors": [], + "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" + }, { "title": "Mask Sensitive Information", "description": "Masks parts of a sensitive string, like a credit card or email address.", diff --git a/snippets/javascript/color-manipulation/rgb-to-hex.md b/snippets/javascript/color-manipulation/rgb-to-hex.md new file mode 100644 index 00000000..c232727e --- /dev/null +++ b/snippets/javascript/color-manipulation/rgb-to-hex.md @@ -0,0 +1,21 @@ +--- +title: RGB to Hex Color +description: Converts RGB color values to hexadecimal color code. +author: jjcantu +tags: javascript,color,conversion,utility +--- + +```js +function rgbToHex(r, g, b) { + const toHex = (n) => { + const hex = n.toString(16); + return hex.length === 1 ? '0' + hex : hex; + }; + + return '#' + toHex(r) + toHex(g) + toHex(b); +} + +// Usage: +console.log(rgbToHex(255, 128, 0)); // Output: "#ff8000" +console.log(rgbToHex(0, 255, 0)); // Output: "#00ff00" +``` \ No newline at end of file diff --git a/snippets/javascript/number-formatting/format-file-size.md b/snippets/javascript/number-formatting/format-file-size.md new file mode 100644 index 00000000..f8036846 --- /dev/null +++ b/snippets/javascript/number-formatting/format-file-size.md @@ -0,0 +1,22 @@ +--- +title: Format File Size +description: Converts bytes into human-readable file size format. +author: jjcantu +tags: javascript,format,size,utility +--- + +```js +function formatFileSize(bytes) { + 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(2)) + ' ' + sizes[i]; +} + +// Usage: +console.log(formatFileSize(1234)); // Output: "1.21 KB" +console.log(formatFileSize(1234567)); // Output: "1.18 MB" +``` \ No newline at end of file diff --git a/snippets/javascript/object-manipulation/deep-clone-object.md b/snippets/javascript/object-manipulation/deep-clone-object.md new file mode 100644 index 00000000..0be9e232 --- /dev/null +++ b/snippets/javascript/object-manipulation/deep-clone-object.md @@ -0,0 +1,27 @@ +--- +title: Deep Clone Object +description: Creates a deep copy of an object or array without reference. +author: jjcantu +tags: javascript,object,clone,utility +--- + +```js +function deepClone(obj) { + if (obj === null || typeof obj !== 'object') return obj; + + const clone = Array.isArray(obj) ? [] : {}; + + for (let key in obj) { + if (Object.prototype.hasOwnProperty.call(obj, key)) { + clone[key] = deepClone(obj[key]); + } + } + + return clone; +} + +// Usage: +const original = { a: 1, b: { c: 2 }, d: [1, 2, 3] }; +const cloned = deepClone(original); +console.log(cloned); // Output: { a: 1, b: { c: 2 }, d: [1, 2, 3] } +``` \ No newline at end of file diff --git a/snippets/javascript/string-manipulation/generate-uuid.md b/snippets/javascript/string-manipulation/generate-uuid.md new file mode 100644 index 00000000..d84fbb95 --- /dev/null +++ b/snippets/javascript/string-manipulation/generate-uuid.md @@ -0,0 +1,19 @@ +--- +title: Generate UUID +description: Generates a UUID (v4) string. +author: jjcantu +tags: javascript,uuid,utility +--- + +```js +function generateUUID() { + return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) { + const r = Math.random() * 16 | 0; + const v = c === 'x' ? r : (r & 0x3 | 0x8); + return v.toString(16); + }); +} + +// Usage: +console.log(generateUUID()); // Output: "a1b2c3d4-e5f6-4g7h-8i9j-k0l1m2n3o4p5" +``` \ No newline at end of file From ec596716fcf7d11f706a4920dbc04d3d81dcdd21 Mon Sep 17 00:00:00 2001 From: Jose Cantu Date: Fri, 3 Jan 2025 01:40:45 -0600 Subject: [PATCH 2/3] resolve feedbacl --- .../color-manipulation/{rgb-to-hex.md => rgb-to-hex-color.md} | 2 +- snippets/javascript/object-manipulation/deep-clone-object.md | 4 ++-- snippets/javascript/string-manipulation/generate-uuid.md | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) rename snippets/javascript/color-manipulation/{rgb-to-hex.md => rgb-to-hex-color.md} (91%) diff --git a/snippets/javascript/color-manipulation/rgb-to-hex.md b/snippets/javascript/color-manipulation/rgb-to-hex-color.md similarity index 91% rename from snippets/javascript/color-manipulation/rgb-to-hex.md rename to snippets/javascript/color-manipulation/rgb-to-hex-color.md index c232727e..2fdcda9d 100644 --- a/snippets/javascript/color-manipulation/rgb-to-hex.md +++ b/snippets/javascript/color-manipulation/rgb-to-hex-color.md @@ -2,7 +2,7 @@ title: RGB to Hex Color description: Converts RGB color values to hexadecimal color code. author: jjcantu -tags: javascript,color,conversion,utility +tags: color,conversion --- ```js diff --git a/snippets/javascript/object-manipulation/deep-clone-object.md b/snippets/javascript/object-manipulation/deep-clone-object.md index 0be9e232..a925d9b3 100644 --- a/snippets/javascript/object-manipulation/deep-clone-object.md +++ b/snippets/javascript/object-manipulation/deep-clone-object.md @@ -2,7 +2,7 @@ title: Deep Clone Object description: Creates a deep copy of an object or array without reference. author: jjcantu -tags: javascript,object,clone,utility +tags: object,clone --- ```js @@ -23,5 +23,5 @@ function deepClone(obj) { // Usage: const original = { a: 1, b: { c: 2 }, d: [1, 2, 3] }; const cloned = deepClone(original); -console.log(cloned); // Output: { a: 1, b: { c: 2 }, d: [1, 2, 3] } +console.log(cloned); // Output: 'original' but cloned ``` \ No newline at end of file diff --git a/snippets/javascript/string-manipulation/generate-uuid.md b/snippets/javascript/string-manipulation/generate-uuid.md index d84fbb95..b8f84bb7 100644 --- a/snippets/javascript/string-manipulation/generate-uuid.md +++ b/snippets/javascript/string-manipulation/generate-uuid.md @@ -2,7 +2,7 @@ title: Generate UUID description: Generates a UUID (v4) string. author: jjcantu -tags: javascript,uuid,utility +tags: uuid, generate, string --- ```js From bcfc368c33f6e9050d920b102c84bcc4aa5f0d97 Mon Sep 17 00:00:00 2001 From: Jose Cantu Date: Fri, 3 Jan 2025 10:06:45 -0600 Subject: [PATCH 3/3] remove unnecessary tags --- snippets/javascript/number-formatting/format-file-size.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/snippets/javascript/number-formatting/format-file-size.md b/snippets/javascript/number-formatting/format-file-size.md index f8036846..fa7fb3a1 100644 --- a/snippets/javascript/number-formatting/format-file-size.md +++ b/snippets/javascript/number-formatting/format-file-size.md @@ -2,7 +2,7 @@ title: Format File Size description: Converts bytes into human-readable file size format. author: jjcantu -tags: javascript,format,size,utility +tags: format,size --- ```js