Skip to content

Commit 9d33fca

Browse files
Merge pull request #52 from 0xHouss/main
Add snippets for Javascript and C
2 parents 6c98606 + a00df69 commit 9d33fca

File tree

5 files changed

+154
-67
lines changed

5 files changed

+154
-67
lines changed

public/data/_index.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@
2323
"lang": "CPP",
2424
"icon": "/icons/cpp.svg"
2525
},
26+
{
27+
"lang": "C",
28+
"icon": "/icons/c.svg"
29+
},
2630
{
2731
"lang": "Rust",
2832
"icon": "/icons/rust.svg"

public/data/c.json

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
[
2+
{
3+
"categoryName": "Basics",
4+
"snippets": [
5+
{
6+
"title": "Hello, World!",
7+
"description": "Prints Hello, World! to the terminal.",
8+
"code": [
9+
"#include <stdio.h> // Includes the input/output library",
10+
"",
11+
"int main() { // Defines the main function",
12+
" printf(\"Hello, World!\\n\") // Outputs Hello, World! and a newline",
13+
"",
14+
" return 0; // indicate the program executed successfully",
15+
"}"
16+
],
17+
"tags": ["c", "printing", "hello-world", "utility"],
18+
"author": "0xHouss"
19+
}
20+
]
21+
},
22+
{
23+
"categoryName": "Mathematical Functions",
24+
"snippets": [
25+
{
26+
"title": "Factorial Function",
27+
"description": "Calculates the factorial of a number.",
28+
"code": [
29+
"int factorial(int x) {",
30+
" int y = 1;",
31+
"",
32+
" for (int i = 2; i <= x; i++)",
33+
" y *= i;",
34+
"",
35+
" return y;",
36+
"}"
37+
],
38+
"tags": ["c", "math", "factorial", "utility"],
39+
"author": "0xHouss"
40+
},
41+
{
42+
"title": "Power Function",
43+
"description": "Calculates the power of a number.",
44+
"code": [
45+
"int power(int x, int n) {",
46+
" int y = 1;",
47+
"",
48+
" for (int i = 0; i < n; i++)",
49+
" y *= x;",
50+
"",
51+
" return y;",
52+
"}"
53+
],
54+
"tags": ["c", "math", "power", "utility"],
55+
"author": "0xHouss"
56+
}
57+
]
58+
}
59+
]

public/data/javascript.json

Lines changed: 74 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@
168168
"// Example usage:",
169169
"console.log(countWords('Hello world! This is a test.')); // Output: 6"
170170
],
171-
"tags": ["string", "manipulation", "word count", "count"],
171+
"tags": ["javascript", "string", "manipulation", "word count", "count"],
172172
"author": "axorax"
173173
},
174174
{
@@ -182,7 +182,7 @@
182182
"// Example usage:",
183183
"console.log(removeWhitespace('Hello world!')); // Output: 'Helloworld!'"
184184
],
185-
"tags": ["string", "whitespace"],
185+
"tags": ["javascript", "string", "whitespace"],
186186
"author": "axorax"
187187
},
188188
{
@@ -608,15 +608,7 @@
608608
"console.log(timeAgoOrAhead(new Date())); // just now",
609609
"console.log(timeAgoOrAhead(futureDate)); // in x years"
610610
],
611-
"tags": [
612-
"javascript",
613-
"date",
614-
"time",
615-
"relative",
616-
"future",
617-
"past",
618-
"utility"
619-
],
611+
"tags": ["javascript", "date", "time", "relative", "future", "past", "utility"],
620612
"author": "Yugveer06"
621613
},
622614
{
@@ -732,6 +724,7 @@
732724
"code": [
733725
"const debounce = (func, delay) => {",
734726
" let timeout;",
727+
"",
735728
" return (...args) => {",
736729
" clearTimeout(timeout);",
737730
" timeout = setTimeout(() => func(...args), delay);",
@@ -774,32 +767,49 @@
774767
"tags": ["javascript", "utility", "throttle", "performance"],
775768
"author": "dostonnabotov"
776769
},
777-
{
778-
"title": "Get Contrast Color",
779-
"description": "Returns either black or white text color based on the brightness of the provided hex color.",
780-
"code": [
781-
"const getContrastColor = (hexColor) => {",
782-
" // Expand short hex color to full format",
783-
" if (hexColor.length === 4) {",
784-
" hexColor = `#${hexColor[1]}${hexColor[1]}${hexColor[2]}${hexColor[2]}${hexColor[3]}${hexColor[3]}`;",
785-
" }",
786-
" const r = parseInt(hexColor.slice(1, 3), 16);",
787-
" const g = parseInt(hexColor.slice(3, 5), 16);",
788-
" const b = parseInt(hexColor.slice(5, 7), 16);",
789-
" const brightness = (r * 299 + g * 587 + b * 114) / 1000;",
790-
" return brightness >= 128 ? \"#000000\" : \"#FFFFFF\";",
791-
"};",
792-
"",
793-
"// Usage:",
794-
"console.log(getContrastColor('#fff')); // Output: #000000 (black)",
795-
"console.log(getContrastColor('#123456')); // Output: #FFFFFF (white)",
796-
"console.log(getContrastColor('#ff6347')); // Output: #000000 (black)",
797-
"console.log(getContrastColor('#f4f')); // Output: #000000 (black)"
798-
],
799-
"tags": ["color", "hex", "contrast", "brightness", "utility"],
800-
"author": "yaya12085"
801-
}
802-
770+
{
771+
"title": "Get Contrast Color",
772+
"description": "Returns either black or white text color based on the brightness of the provided hex color.",
773+
"code": [
774+
"const getContrastColor = (hexColor) => {",
775+
" // Expand short hex color to full format",
776+
" if (hexColor.length === 4) {",
777+
" hexColor = `#${hexColor[1]}${hexColor[1]}${hexColor[2]}${hexColor[2]}${hexColor[3]}${hexColor[3]}`;",
778+
" }",
779+
" const r = parseInt(hexColor.slice(1, 3), 16);",
780+
" const g = parseInt(hexColor.slice(3, 5), 16);",
781+
" const b = parseInt(hexColor.slice(5, 7), 16);",
782+
" const brightness = (r * 299 + g * 587 + b * 114) / 1000;",
783+
" return brightness >= 128 ? \"#000000\" : \"#FFFFFF\";",
784+
"};",
785+
"",
786+
"// Usage:",
787+
"console.log(getContrastColor('#fff')); // Output: #000000 (black)",
788+
"console.log(getContrastColor('#123456')); // Output: #FFFFFF (white)",
789+
"console.log(getContrastColor('#ff6347')); // Output: #000000 (black)",
790+
"console.log(getContrastColor('#f4f')); // Output: #000000 (black)"
791+
],
792+
"tags": ["javascript", "color", "hex", "contrast", "brightness", "utility"],
793+
"author": "yaya12085"
794+
},
795+
{
796+
"title": "Sleep Function",
797+
"description": "Waits for a specified amount of milliseconds before resolving.",
798+
"code": [
799+
"const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));",
800+
"",
801+
"// Usage:",
802+
"async function main() {",
803+
" console.log('Hello');",
804+
" await sleep(2000); // Waits for 2 seconds",
805+
" console.log('World!');",
806+
"}",
807+
"",
808+
"main();"
809+
],
810+
"tags": ["javascript", "sleep", "delay", "utility", "promises"],
811+
"author": "0xHouss"
812+
}
803813
]
804814
},
805815
{
@@ -886,32 +896,31 @@
886896
}
887897
]
888898
},
889-
{
890-
"categoryName": "Number Formatting",
891-
"snippets": [
892-
{
893-
"title": "Number Formatter",
894-
"description": "Formats a number with suffixes (K, M, B, etc.).",
895-
"code": [
896-
"const nFormatter = (num) => {",
897-
" if (!num) return;",
898-
" num = parseFloat(num.toString().replace(/[^0-9.]/g, ''));",
899-
" const suffixes = ['', 'K', 'M', 'B', 'T', 'P', 'E'];",
900-
" let index = 0;",
901-
" while (num >= 1000 && index < suffixes.length - 1) {",
902-
" num /= 1000;",
903-
" index++;",
904-
" }",
905-
" return num.toFixed(2).replace(/\\.0+$|(\\.[0-9]*[1-9])0+$/, '$1') + suffixes[index];",
906-
"};",
907-
"",
908-
"// Usage:",
909-
"console.log(nFormatter(1234567)); // Output: '1.23M'"
910-
],
911-
"tags": ["javascript", "number", "format", "utility"],
912-
"author": "realvishalrana"
913-
}
914-
]
915-
}
916-
899+
{
900+
"categoryName": "Number Formatting",
901+
"snippets": [
902+
{
903+
"title": "Number Formatter",
904+
"description": "Formats a number with suffixes (K, M, B, etc.).",
905+
"code": [
906+
"const nFormatter = (num) => {",
907+
" if (!num) return;",
908+
" num = parseFloat(num.toString().replace(/[^0-9.]/g, ''));",
909+
" const suffixes = ['', 'K', 'M', 'B', 'T', 'P', 'E'];",
910+
" let index = 0;",
911+
" while (num >= 1000 && index < suffixes.length - 1) {",
912+
" num /= 1000;",
913+
" index++;",
914+
" }",
915+
" return num.toFixed(2).replace(/\\.0+$|(\\.[0-9]*[1-9])0+$/, '$1') + suffixes[index];",
916+
"};",
917+
"",
918+
"// Usage:",
919+
"console.log(nFormatter(1234567)); // Output: '1.23M'"
920+
],
921+
"tags": ["javascript", "number", "format", "utility"],
922+
"author": "realvishalrana"
923+
}
924+
]
925+
}
917926
]

public/data/rust.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[
22
{
3-
"categoryName": "Basics",
4-
"snippets": [
3+
"categoryName": "Basics",
4+
"snippets": [
55
{
66
"title": "Hello, World!",
77
"description": "Prints Hello, World! to the terminal.",

public/icons/c.svg

Lines changed: 15 additions & 0 deletions
Loading

0 commit comments

Comments
 (0)