|
357 | 357 | "code": [
|
358 | 358 | "const debounce = (func, delay) => {",
|
359 | 359 | " let timeout;",
|
| 360 | + "", |
360 | 361 | " return (...args) => {",
|
361 | 362 | " clearTimeout(timeout);",
|
362 | 363 | " timeout = setTimeout(() => func(...args), delay);",
|
|
399 | 400 | "tags": ["javascript", "utility", "throttle", "performance"],
|
400 | 401 | "author": "dostonnabotov"
|
401 | 402 | },
|
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 | + } |
428 | 446 | ]
|
429 | 447 | },
|
430 | 448 | {
|
|
0 commit comments