Skip to content

Commit bff9dd6

Browse files
committed
Add javascript sleep snippet
1 parent bb7320d commit bff9dd6

File tree

2 files changed

+46
-28
lines changed

2 files changed

+46
-28
lines changed

public/data/javascript.json

Lines changed: 44 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,7 @@
357357
"code": [
358358
"const debounce = (func, delay) => {",
359359
" let timeout;",
360+
"",
360361
" return (...args) => {",
361362
" clearTimeout(timeout);",
362363
" timeout = setTimeout(() => func(...args), delay);",
@@ -399,32 +400,49 @@
399400
"tags": ["javascript", "utility", "throttle", "performance"],
400401
"author": "dostonnabotov"
401402
},
402-
{
403-
"title": "Get Contrast Color",
404-
"description": "Returns either black or white text color based on the brightness of the provided hex color.",
405-
"code": [
406-
"const getContrastColor = (hexColor) => {",
407-
" // Expand short hex color to full format",
408-
" if (hexColor.length === 4) {",
409-
" hexColor = `#${hexColor[1]}${hexColor[1]}${hexColor[2]}${hexColor[2]}${hexColor[3]}${hexColor[3]}`;",
410-
" }",
411-
" const r = parseInt(hexColor.slice(1, 3), 16);",
412-
" const g = parseInt(hexColor.slice(3, 5), 16);",
413-
" const b = parseInt(hexColor.slice(5, 7), 16);",
414-
" const brightness = (r * 299 + g * 587 + b * 114) / 1000;",
415-
" return brightness >= 128 ? \"#000000\" : \"#FFFFFF\";",
416-
"};",
417-
"",
418-
"// Usage:",
419-
"console.log(getContrastColor('#fff')); // Output: #000000 (black)",
420-
"console.log(getContrastColor('#123456')); // Output: #FFFFFF (white)",
421-
"console.log(getContrastColor('#ff6347')); // Output: #000000 (black)",
422-
"console.log(getContrastColor('#f4f')); // Output: #000000 (black)"
423-
],
424-
"tags": ["color", "hex", "contrast", "brightness", "utility"],
425-
"author": "yaya12085"
426-
}
427-
403+
{
404+
"title": "Get Contrast Color",
405+
"description": "Returns either black or white text color based on the brightness of the provided hex color.",
406+
"code": [
407+
"const getContrastColor = (hexColor) => {",
408+
" // Expand short hex color to full format",
409+
" if (hexColor.length === 4) {",
410+
" hexColor = `#${hexColor[1]}${hexColor[1]}${hexColor[2]}${hexColor[2]}${hexColor[3]}${hexColor[3]}`;",
411+
" }",
412+
" const r = parseInt(hexColor.slice(1, 3), 16);",
413+
" const g = parseInt(hexColor.slice(3, 5), 16);",
414+
" const b = parseInt(hexColor.slice(5, 7), 16);",
415+
" const brightness = (r * 299 + g * 587 + b * 114) / 1000;",
416+
" return brightness >= 128 ? \"#000000\" : \"#FFFFFF\";",
417+
"};",
418+
"",
419+
"// Usage:",
420+
"console.log(getContrastColor('#fff')); // Output: #000000 (black)",
421+
"console.log(getContrastColor('#123456')); // Output: #FFFFFF (white)",
422+
"console.log(getContrastColor('#ff6347')); // Output: #000000 (black)",
423+
"console.log(getContrastColor('#f4f')); // Output: #000000 (black)"
424+
],
425+
"tags": ["color", "hex", "contrast", "brightness", "utility"],
426+
"author": "yaya12085"
427+
},
428+
{
429+
"title": "Sleep Function",
430+
"description": "Waits for a specified amount of milliseconds before resolving.",
431+
"code": [
432+
"const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));",
433+
"",
434+
"// Usage:",
435+
"async function main() {",
436+
" console.log('Hello');",
437+
" await sleep(2000); // Waits for 2 seconds",
438+
" console.log('World!');",
439+
"}",
440+
"",
441+
"main();"
442+
],
443+
"tags": ["sleep", "delay", "utility", "promises"],
444+
"author": "0xHouss"
445+
}
428446
]
429447
},
430448
{

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.",

0 commit comments

Comments
 (0)