|
5 | 5 | { |
6 | 6 | "title":"Remove Duplicates", |
7 | 7 | "description":"Removes duplicate values from an array.", |
8 | | -"code":"const removeDuplicates = (arr) => [...new Set(arr)];\n\n// Usage:\nconst numbers = [1, 2, 2, 3, 4, 4, 5];\nconsole.log(removeDuplicates(numbers)); // Output: [1, 2, 3, 4, 5]", |
| 8 | +"code": [ |
| 9 | +"const removeDuplicates = (arr) => [...new Set(arr)];", |
| 10 | +"", |
| 11 | +"// Usage:", |
| 12 | +"const numbers = [1, 2, 2, 3, 4, 4, 5];", |
| 13 | +"console.log(removeDuplicates(numbers)); // Output: [1, 2, 3, 4, 5]" |
| 14 | + ], |
9 | 15 | "tags": ["javascript","array","deduplicate","utility"], |
10 | 16 | "author":"dostonnabotov" |
11 | 17 | }, |
12 | 18 | { |
13 | 19 | "title":"Flatten Array", |
14 | 20 | "description":"Flattens a multi-dimensional array.", |
15 | | -"code":"const flattenArray = (arr) => arr.flat(Infinity);\n\n// Usage:\nconst nestedArray = [1, [2, [3, [4]]]];\nconsole.log(flattenArray(nestedArray)); // Output: [1, 2, 3, 4]", |
| 21 | +"code": [ |
| 22 | +"const flattenArray = (arr) => arr.flat(Infinity);", |
| 23 | +"", |
| 24 | +"// Usage:", |
| 25 | +"const nestedArray = [1, [2, [3, [4]]]];", |
| 26 | +"console.log(flattenArray(nestedArray)); // Output: [1, 2, 3, 4]" |
| 27 | + ], |
16 | 28 | "tags": ["javascript","array","flatten","utility"], |
17 | 29 | "author":"dostonnabotov" |
18 | 30 | } |
|
24 | 36 | { |
25 | 37 | "title":"Capitalize String", |
26 | 38 | "description":"Capitalizes the first letter of a string.", |
27 | | -"code":"const capitalize = (str) => str.charAt(0).toUpperCase() + str.slice(1);\n\n// Usage:\nconsole.log(capitalize('hello')); // Output: 'Hello'", |
| 39 | +"code": [ |
| 40 | +"const capitalize = (str) => str.charAt(0).toUpperCase() + str.slice(1);", |
| 41 | +"", |
| 42 | +"// Usage:", |
| 43 | +"console.log(capitalize('hello')); // Output: 'Hello'" |
| 44 | + ], |
28 | 45 | "tags": ["javascript","string","capitalize","utility"], |
29 | 46 | "author":"dostonnabotov" |
30 | 47 | }, |
31 | 48 | { |
32 | 49 | "title":"Reverse String", |
33 | 50 | "description":"Reverses the characters in a string.", |
34 | | -"code":"const reverseString = (str) => str.split('').reverse().join('');\n\n// Usage:\nconsole.log(reverseString('hello')); // Output: 'olleh'", |
| 51 | +"code": [ |
| 52 | +"const reverseString = (str) => str.split('').reverse().join('');", |
| 53 | +"", |
| 54 | +"// Usage:", |
| 55 | +"console.log(reverseString('hello')); // Output: 'olleh'" |
| 56 | + ], |
35 | 57 | "tags": ["javascript","string","reverse","utility"], |
36 | 58 | "author":"dostonnabotov" |
37 | 59 | } |
|
43 | 65 | { |
44 | 66 | "title":"Format Date", |
45 | 67 | "description":"Formats a date in 'YYYY-MM-DD' format.", |
46 | | -"code":"const formatDate = (date) => date.toISOString().split('T')[0];\n\n// Usage:\nconsole.log(formatDate(new Date())); // Output: '2024-12-10'", |
| 68 | +"code": [ |
| 69 | +"const formatDate = (date) => date.toISOString().split('T')[0];", |
| 70 | +"", |
| 71 | +"// Usage:", |
| 72 | +"console.log(formatDate(new Date())); // Output: '2024-12-10'" |
| 73 | + ], |
47 | 74 | "tags": ["javascript","date","format","utility"], |
48 | 75 | "author":"dostonnabotov" |
49 | 76 | }, |
50 | 77 | { |
51 | 78 | "title":"Get Time Difference", |
52 | 79 | "description":"Calculates the time difference in days between two dates.", |
53 | | -"code":"const getTimeDifference = (date1, date2) => {\n const diff = Math.abs(date2 - date1);\n return Math.ceil(diff / (1000 * 60 * 60 * 24));\n};\n\n// Usage:\nconst date1 = new Date('2024-01-01');\nconst date2 = new Date('2024-12-31');\nconsole.log(getTimeDifference(date1, date2)); // Output: 365", |
| 80 | +"code": [ |
| 81 | +"const getTimeDifference = (date1, date2) => {", |
| 82 | +" const diff = Math.abs(date2 - date1);", |
| 83 | +" return Math.ceil(diff / (1000 * 60 * 60 * 24));", |
| 84 | +"};", |
| 85 | +"", |
| 86 | +"// Usage:", |
| 87 | +"const date1 = new Date('2024-01-01');", |
| 88 | +"const date2 = new Date('2024-12-31');", |
| 89 | +"console.log(getTimeDifference(date1, date2)); // Output: 365" |
| 90 | + ], |
54 | 91 | "tags": ["javascript","date","time-difference","utility"], |
55 | 92 | "author":"dostonnabotov" |
56 | 93 | } |
|
62 | 99 | { |
63 | 100 | "title":"Debounce Function", |
64 | 101 | "description":"Delays a function execution until after a specified time.", |
65 | | -"code":"const debounce = (func, delay) => {\n let timeout;\n return (...args) => {\n clearTimeout(timeout);\n timeout = setTimeout(() => func(...args), delay);\n };\n};\n\n// Usage:\nwindow.addEventListener('resize', debounce(() => console.log('Resized!'), 500));", |
| 102 | +"code": [ |
| 103 | +"const debounce = (func, delay) => {", |
| 104 | +" let timeout;", |
| 105 | +" return (...args) => {", |
| 106 | +" clearTimeout(timeout);", |
| 107 | +" timeout = setTimeout(() => func(...args), delay);", |
| 108 | +" };", |
| 109 | +"};", |
| 110 | +"", |
| 111 | +"// Usage:", |
| 112 | +"window.addEventListener('resize', debounce(() => console.log('Resized!'), 500));" |
| 113 | + ], |
66 | 114 | "tags": ["javascript","utility","debounce","performance"], |
67 | 115 | "author":"dostonnabotov" |
68 | 116 | }, |
69 | 117 | { |
70 | 118 | "title":"Throttle Function", |
71 | 119 | "description":"Limits a function execution to once every specified time interval.", |
72 | | -"code":"const throttle = (func, limit) => {\n let lastFunc;\n let lastRan;\n return (...args) => {\n const context = this;\n if (!lastRan) {\n func.apply(context, args);\n lastRan = Date.now();\n } else {\n clearTimeout(lastFunc);\n lastFunc = setTimeout(() => {\n if (Date.now() - lastRan >= limit) {\n func.apply(context, args);\n lastRan = Date.now();\n }\n }, limit - (Date.now() - lastRan));\n }\n };\n};\n\n// Usage:\ndocument.addEventListener('scroll', throttle(() => console.log('Scrolled!'), 1000));", |
| 120 | +"code": [ |
| 121 | +"const throttle = (func, limit) => {", |
| 122 | +" let lastFunc;", |
| 123 | +" let lastRan;", |
| 124 | +" return (...args) => {", |
| 125 | +" const context = this;", |
| 126 | +" if (!lastRan) {", |
| 127 | +" func.apply(context, args);", |
| 128 | +" lastRan = Date.now();", |
| 129 | +" } else {", |
| 130 | +" clearTimeout(lastFunc);", |
| 131 | +" lastFunc = setTimeout(() => {", |
| 132 | +" if (Date.now() - lastRan >= limit) {", |
| 133 | +" func.apply(context, args);", |
| 134 | +" lastRan = Date.now();", |
| 135 | +" }", |
| 136 | +" }, limit - (Date.now() - lastRan));", |
| 137 | +" }", |
| 138 | +" };", |
| 139 | +"};", |
| 140 | +"", |
| 141 | +"// Usage:", |
| 142 | +"document.addEventListener('scroll', throttle(() => console.log('Scrolled!'), 1000));" |
| 143 | + ], |
73 | 144 | "tags": ["javascript","utility","throttle","performance"], |
74 | 145 | "author":"dostonnabotov" |
75 | 146 | } |
|
81 | 152 | { |
82 | 153 | "title":"Toggle Class", |
83 | 154 | "description":"Toggles a class on an element.", |
84 | | -"code":"const toggleClass = (element, className) => {\n element.classList.toggle(className);\n};\n\n// Usage:\nconst element = document.querySelector('.my-element');\ntoggleClass(element, 'active');", |
| 155 | +"code": [ |
| 156 | +"const toggleClass = (element, className) => {", |
| 157 | +" element.classList.toggle(className);", |
| 158 | +"};", |
| 159 | +"", |
| 160 | +"// Usage:", |
| 161 | +"const element = document.querySelector('.my-element');", |
| 162 | +"toggleClass(element, 'active');" |
| 163 | + ], |
85 | 164 | "tags": ["javascript","dom","class","utility"], |
86 | 165 | "author":"dostonnabotov" |
87 | 166 | }, |
88 | 167 | { |
89 | 168 | "title":"Smooth Scroll to Element", |
90 | 169 | "description":"Scrolls smoothly to a specified element.", |
91 | | -"code":"const smoothScroll = (element) => {\n element.scrollIntoView({ behavior: 'smooth' });\n};\n\n// Usage:\nconst target = document.querySelector('#target');\nsmoothScroll(target);", |
| 170 | +"code": [ |
| 171 | +"const smoothScroll = (element) => {", |
| 172 | +" element.scrollIntoView({ behavior: 'smooth' });", |
| 173 | +"};", |
| 174 | +"", |
| 175 | +"// Usage:", |
| 176 | +"const target = document.querySelector('#target');", |
| 177 | +"smoothScroll(target);" |
| 178 | + ], |
92 | 179 | "tags": ["javascript","dom","scroll","ui"], |
93 | 180 | "author":"dostonnabotov" |
94 | 181 | } |
|