Skip to content

Commit a50a47e

Browse files
committed
snippets - add a few useful JS snippets
1 parent ef24250 commit a50a47e

File tree

5 files changed

+145
-0
lines changed

5 files changed

+145
-0
lines changed

public/consolidated/javascript.json

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,24 @@
101101
}
102102
]
103103
},
104+
{
105+
"categoryName": "Color Manipulation",
106+
"snippets": [
107+
{
108+
"title": "RGB to Hex Color",
109+
"description": "Converts RGB color values to hexadecimal color code.",
110+
"author": "jjcantu",
111+
"tags": [
112+
"javascript",
113+
"color",
114+
"conversion",
115+
"utility"
116+
],
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"
119+
}
120+
]
121+
},
104122
{
105123
"categoryName": "Date And Time",
106124
"snippets": [
@@ -547,6 +565,19 @@
547565
"contributors": [],
548566
"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"
549567
},
568+
{
569+
"title": "Format File Size",
570+
"description": "Converts bytes into human-readable file size format.",
571+
"author": "jjcantu",
572+
"tags": [
573+
"javascript",
574+
"format",
575+
"size",
576+
"utility"
577+
],
578+
"contributors": [],
579+
"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"
580+
},
550581
{
551582
"title": "Format Number with Commas",
552583
"description": "Formats a number with commas for better readability (e.g., 1000 -> 1,000).",
@@ -656,6 +687,19 @@
656687
"contributors": [],
657688
"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"
658689
},
690+
{
691+
"title": "Deep Clone Object",
692+
"description": "Creates a deep copy of an object or array without reference.",
693+
"author": "jjcantu",
694+
"tags": [
695+
"javascript",
696+
"object",
697+
"clone",
698+
"utility"
699+
],
700+
"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+
},
659703
{
660704
"title": "Filter Object",
661705
"description": "Filter out entries in an object where the value is falsy, including empty strings, empty objects, null, and undefined.",
@@ -930,6 +974,18 @@
930974
"contributors": [],
931975
"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"
932976
},
977+
{
978+
"title": "Generate UUID",
979+
"description": "Generates a UUID (v4) string.",
980+
"author": "jjcantu",
981+
"tags": [
982+
"javascript",
983+
"uuid",
984+
"utility"
985+
],
986+
"contributors": [],
987+
"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"
988+
},
933989
{
934990
"title": "Mask Sensitive Information",
935991
"description": "Masks parts of a sensitive string, like a credit card or email address.",
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
---
2+
title: RGB to Hex Color
3+
description: Converts RGB color values to hexadecimal color code.
4+
author: jjcantu
5+
tags: javascript,color,conversion,utility
6+
---
7+
8+
```js
9+
function rgbToHex(r, g, b) {
10+
const toHex = (n) => {
11+
const hex = n.toString(16);
12+
return hex.length === 1 ? '0' + hex : hex;
13+
};
14+
15+
return '#' + toHex(r) + toHex(g) + toHex(b);
16+
}
17+
18+
// Usage:
19+
console.log(rgbToHex(255, 128, 0)); // Output: "#ff8000"
20+
console.log(rgbToHex(0, 255, 0)); // Output: "#00ff00"
21+
```
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
---
2+
title: Format File Size
3+
description: Converts bytes into human-readable file size format.
4+
author: jjcantu
5+
tags: javascript,format,size,utility
6+
---
7+
8+
```js
9+
function formatFileSize(bytes) {
10+
if (bytes === 0) return '0 Bytes';
11+
12+
const k = 1024;
13+
const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB'];
14+
const i = Math.floor(Math.log(bytes) / Math.log(k));
15+
16+
return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + ' ' + sizes[i];
17+
}
18+
19+
// Usage:
20+
console.log(formatFileSize(1234)); // Output: "1.21 KB"
21+
console.log(formatFileSize(1234567)); // Output: "1.18 MB"
22+
```
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
---
2+
title: Deep Clone Object
3+
description: Creates a deep copy of an object or array without reference.
4+
author: jjcantu
5+
tags: javascript,object,clone,utility
6+
---
7+
8+
```js
9+
function deepClone(obj) {
10+
if (obj === null || typeof obj !== 'object') return obj;
11+
12+
const clone = Array.isArray(obj) ? [] : {};
13+
14+
for (let key in obj) {
15+
if (Object.prototype.hasOwnProperty.call(obj, key)) {
16+
clone[key] = deepClone(obj[key]);
17+
}
18+
}
19+
20+
return clone;
21+
}
22+
23+
// Usage:
24+
const original = { a: 1, b: { c: 2 }, d: [1, 2, 3] };
25+
const cloned = deepClone(original);
26+
console.log(cloned); // Output: { a: 1, b: { c: 2 }, d: [1, 2, 3] }
27+
```
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
---
2+
title: Generate UUID
3+
description: Generates a UUID (v4) string.
4+
author: jjcantu
5+
tags: javascript,uuid,utility
6+
---
7+
8+
```js
9+
function generateUUID() {
10+
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
11+
const r = Math.random() * 16 | 0;
12+
const v = c === 'x' ? r : (r & 0x3 | 0x8);
13+
return v.toString(16);
14+
});
15+
}
16+
17+
// Usage:
18+
console.log(generateUUID()); // Output: "a1b2c3d4-e5f6-4g7h-8i9j-k0l1m2n3o4p5"
19+
```

0 commit comments

Comments
 (0)