|
168 | 168 | "// Example usage:",
|
169 | 169 | "console.log(countWords('Hello world! This is a test.')); // Output: 6"
|
170 | 170 | ],
|
171 |
| - "tags": ["string", "manipulation", "word count", "count"], |
| 171 | + "tags": ["javascript", "string", "manipulation", "word count", "count"], |
172 | 172 | "author": "axorax"
|
173 | 173 | },
|
174 | 174 | {
|
|
182 | 182 | "// Example usage:",
|
183 | 183 | "console.log(removeWhitespace('Hello world!')); // Output: 'Helloworld!'"
|
184 | 184 | ],
|
185 |
| - "tags": ["string", "whitespace"], |
| 185 | + "tags": ["javascript", "string", "whitespace"], |
186 | 186 | "author": "axorax"
|
187 | 187 | },
|
188 | 188 | {
|
|
608 | 608 | "console.log(timeAgoOrAhead(new Date())); // just now",
|
609 | 609 | "console.log(timeAgoOrAhead(futureDate)); // in x years"
|
610 | 610 | ],
|
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"], |
620 | 612 | "author": "Yugveer06"
|
621 | 613 | },
|
622 | 614 | {
|
|
732 | 724 | "code": [
|
733 | 725 | "const debounce = (func, delay) => {",
|
734 | 726 | " let timeout;",
|
| 727 | + "", |
735 | 728 | " return (...args) => {",
|
736 | 729 | " clearTimeout(timeout);",
|
737 | 730 | " timeout = setTimeout(() => func(...args), delay);",
|
|
774 | 767 | "tags": ["javascript", "utility", "throttle", "performance"],
|
775 | 768 | "author": "dostonnabotov"
|
776 | 769 | },
|
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 | + } |
803 | 813 | ]
|
804 | 814 | },
|
805 | 815 | {
|
|
886 | 896 | }
|
887 | 897 | ]
|
888 | 898 | },
|
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 | + } |
917 | 926 | ]
|
0 commit comments