|
893 | 893 | ],
|
894 | 894 | "tags": ["javascript", "sleep", "delay", "utility", "promises"],
|
895 | 895 | "author": "0xHouss"
|
| 896 | + }, |
| 897 | + { |
| 898 | + "title": "Memoize Function", |
| 899 | + "description": "Caches the result of a function based on its arguments to improve performance.", |
| 900 | + "code": [ |
| 901 | + "const memoize = (func) => {", |
| 902 | + " const cache = new Map();", |
| 903 | + " return (...args) => {", |
| 904 | + " const key = JSON.stringify(args);", |
| 905 | + " if (cache.has(key)) {", |
| 906 | + " return cache.get(key);", |
| 907 | + " }", |
| 908 | + " const result = func(...args);", |
| 909 | + " cache.set(key, result);", |
| 910 | + " return result;", |
| 911 | + " };", |
| 912 | + "};", |
| 913 | + "", |
| 914 | + "// Usage:", |
| 915 | + "const factorial = memoize((n) => (n <= 1 ? 1 : n * factorial(n - 1)));", |
| 916 | + "console.log(factorial(5)); // Output: 120", |
| 917 | + "console.log(factorial(5)); // Output: 120 (retrieved from cache)" |
| 918 | + ], |
| 919 | + "tags": ["javascript", "memoization", "optimization", "utility"], |
| 920 | + "author": "axorax" |
| 921 | + }, |
| 922 | + { |
| 923 | + "title": "Once Function", |
| 924 | + "description": "Ensures a function is only called once.", |
| 925 | + "code": [ |
| 926 | + "const once = (func) => {", |
| 927 | + " let called = false;", |
| 928 | + " return (...args) => {", |
| 929 | + " if (!called) {", |
| 930 | + " called = true;", |
| 931 | + " return func(...args);", |
| 932 | + " }", |
| 933 | + " };", |
| 934 | + "};", |
| 935 | + "", |
| 936 | + "// Usage:", |
| 937 | + "const initialize = once(() => console.log('Initialized!'));", |
| 938 | + "initialize(); // Output: Initialized!", |
| 939 | + "initialize(); // No output" |
| 940 | + ], |
| 941 | + "tags": ["javascript", "function", "once", "utility"], |
| 942 | + "author": "axorax" |
| 943 | + }, |
| 944 | + { |
| 945 | + "title": "Curry Function", |
| 946 | + "description": "Transforms a function into its curried form.", |
| 947 | + "code": [ |
| 948 | + "const curry = (func) => {", |
| 949 | + " const curried = (...args) => {", |
| 950 | + " if (args.length >= func.length) {", |
| 951 | + " return func(...args);", |
| 952 | + " }", |
| 953 | + " return (...nextArgs) => curried(...args, ...nextArgs);", |
| 954 | + " };", |
| 955 | + " return curried;", |
| 956 | + "};", |
| 957 | + "", |
| 958 | + "// Usage:", |
| 959 | + "const add = (a, b, c) => a + b + c;", |
| 960 | + "const curriedAdd = curry(add);", |
| 961 | + "console.log(curriedAdd(1)(2)(3)); // Output: 6", |
| 962 | + "console.log(curriedAdd(1, 2)(3)); // Output: 6" |
| 963 | + ], |
| 964 | + "tags": ["javascript", "curry", "function", "utility"], |
| 965 | + "author": "axorax" |
| 966 | + }, |
| 967 | + { |
| 968 | + "title": "Compose Functions", |
| 969 | + "description": "Composes multiple functions into a single function, where the output of one function becomes the input of the next.", |
| 970 | + "code": [ |
| 971 | + "const compose = (...funcs) => (initialValue) => {", |
| 972 | + " return funcs.reduce((acc, func) => func(acc), initialValue);", |
| 973 | + "};", |
| 974 | + "", |
| 975 | + "// Usage:", |
| 976 | + "const add2 = (x) => x + 2;", |
| 977 | + "const multiply3 = (x) => x * 3;", |
| 978 | + "const composed = compose(multiply3, add2);", |
| 979 | + "console.log(composed(5)); // Output: 21 ((5 + 2) * 3)" |
| 980 | + ], |
| 981 | + "tags": ["javascript", "function", "compose", "utility"], |
| 982 | + "author": "axorax" |
| 983 | + }, |
| 984 | + { |
| 985 | + "title": "Rate Limit Function", |
| 986 | + "description": "Limits how often a function can be executed within a given time window.", |
| 987 | + "code": [ |
| 988 | + "const rateLimit = (func, limit, timeWindow) => {", |
| 989 | + " let queue = [];", |
| 990 | + " setInterval(() => {", |
| 991 | + " if (queue.length) {", |
| 992 | + " const next = queue.shift();", |
| 993 | + " func(...next.args);", |
| 994 | + " }", |
| 995 | + " }, timeWindow);", |
| 996 | + " return (...args) => {", |
| 997 | + " if (queue.length < limit) {", |
| 998 | + " queue.push({ args });", |
| 999 | + " }", |
| 1000 | + " };", |
| 1001 | + "};", |
| 1002 | + "", |
| 1003 | + "// Usage:", |
| 1004 | + "const fetchData = () => console.log('Fetching data...');", |
| 1005 | + "const rateLimitedFetch = rateLimit(fetchData, 2, 1000);", |
| 1006 | + "setInterval(() => rateLimitedFetch(), 200); // Only calls fetchData twice every second" |
| 1007 | + ], |
| 1008 | + "tags": ["javascript", "function", "rate-limiting", "utility"], |
| 1009 | + "author": "axorax" |
896 | 1010 | }
|
897 | 1011 | ]
|
898 | 1012 | },
|
|
928 | 1042 | ],
|
929 | 1043 | "tags": ["javascript", "dom", "scroll", "ui"],
|
930 | 1044 | "author": "dostonnabotov"
|
| 1045 | + }, |
| 1046 | + { |
| 1047 | + "title": "Get Element Position", |
| 1048 | + "description": "Gets the position of an element relative to the viewport.", |
| 1049 | + "code": [ |
| 1050 | + "const getElementPosition = (element) => {", |
| 1051 | + " const rect = element.getBoundingClientRect();", |
| 1052 | + " return { x: rect.left, y: rect.top };", |
| 1053 | + "};", |
| 1054 | + "", |
| 1055 | + "// Usage:", |
| 1056 | + "const element = document.querySelector('.my-element');", |
| 1057 | + "const position = getElementPosition(element);", |
| 1058 | + "console.log(position); // { x: 100, y: 150 }" |
| 1059 | + ], |
| 1060 | + "tags": ["javascript", "dom", "position", "utility"], |
| 1061 | + "author": "axorax" |
| 1062 | + }, |
| 1063 | + { |
| 1064 | + "title": "Change Element Style", |
| 1065 | + "description": "Changes the inline style of an element.", |
| 1066 | + "code": [ |
| 1067 | + "const changeElementStyle = (element, styleObj) => {", |
| 1068 | + " Object.entries(styleObj).forEach(([property, value]) => {", |
| 1069 | + " element.style[property] = value;", |
| 1070 | + " });", |
| 1071 | + "};", |
| 1072 | + "", |
| 1073 | + "// Usage:", |
| 1074 | + "const element = document.querySelector('.my-element');", |
| 1075 | + "changeElementStyle(element, { color: 'red', backgroundColor: 'yellow' });" |
| 1076 | + ], |
| 1077 | + "tags": ["javascript", "dom", "style", "utility"], |
| 1078 | + "author": "axorax" |
| 1079 | + }, |
| 1080 | + { |
| 1081 | + "title": "Remove Element", |
| 1082 | + "description": "Removes a specified element from the DOM.", |
| 1083 | + "code": [ |
| 1084 | + "const removeElement = (element) => {", |
| 1085 | + " if (element && element.parentNode) {", |
| 1086 | + " element.parentNode.removeChild(element);", |
| 1087 | + " }", |
| 1088 | + "};", |
| 1089 | + "", |
| 1090 | + "// Usage:", |
| 1091 | + "const element = document.querySelector('.my-element');", |
| 1092 | + "removeElement(element);" |
| 1093 | + ], |
| 1094 | + "tags": ["javascript", "dom", "remove", "utility"], |
| 1095 | + "author": "axorax" |
931 | 1096 | }
|
932 | 1097 | ]
|
933 | 1098 | },
|
|
977 | 1142 | ],
|
978 | 1143 | "tags": ["javascript", "localStorage", "storage", "utility"],
|
979 | 1144 | "author": "dostonnabotov"
|
| 1145 | + }, |
| 1146 | + { |
| 1147 | + "title": "Check if Item Exists in localStorage", |
| 1148 | + "description": "Checks if a specific item exists in localStorage.", |
| 1149 | + "code": [ |
| 1150 | + "const isItemInLocalStorage = (key) => {", |
| 1151 | + " return localStorage.getItem(key) !== null;", |
| 1152 | + "};", |
| 1153 | + "", |
| 1154 | + "// Usage:", |
| 1155 | + "console.log(isItemInLocalStorage('user')); // Output: true or false" |
| 1156 | + ], |
| 1157 | + "tags": ["javascript", "localStorage", "storage", "utility"], |
| 1158 | + "author": "axorax" |
980 | 1159 | }
|
981 | 1160 | ]
|
982 | 1161 | },
|
|
1004 | 1183 | ],
|
1005 | 1184 | "tags": ["javascript", "number", "format", "utility"],
|
1006 | 1185 | "author": "realvishalrana"
|
| 1186 | + }, |
| 1187 | + { |
| 1188 | + "title": "Format Number with Commas", |
| 1189 | + "description": "Formats a number with commas for better readability (e.g., 1000 -> 1,000).", |
| 1190 | + "code": [ |
| 1191 | + "const formatNumberWithCommas = (num) => {", |
| 1192 | + " return num.toString().replace(/\\B(?=(\\d{3})+(?!\\d))/g, ',');", |
| 1193 | + "};", |
| 1194 | + "", |
| 1195 | + "// Usage:", |
| 1196 | + "console.log(formatNumberWithCommas(1000)); // Output: '1,000'", |
| 1197 | + "console.log(formatNumberWithCommas(1234567)); // Output: '1,234,567'", |
| 1198 | + "console.log(formatNumberWithCommas(987654321)); // Output: '987,654,321'" |
| 1199 | + ], |
| 1200 | + "tags": ["javascript", "number", "format", "utility"], |
| 1201 | + "author": "axorax" |
| 1202 | + }, |
| 1203 | + { |
| 1204 | + "title": "Convert Number to Currency", |
| 1205 | + "description": "Converts a number to a currency format with a specific locale.", |
| 1206 | + "code": [ |
| 1207 | + "const convertToCurrency = (num, locale = 'en-US', currency = 'USD') => {", |
| 1208 | + " return new Intl.NumberFormat(locale, {", |
| 1209 | + " style: 'currency',", |
| 1210 | + " currency: currency", |
| 1211 | + " }).format(num);", |
| 1212 | + "};", |
| 1213 | + "", |
| 1214 | + "// Usage:", |
| 1215 | + "console.log(convertToCurrency(1234567.89)); // Output: '$1,234,567.89'", |
| 1216 | + "console.log(convertToCurrency(987654.32, 'de-DE', 'EUR')); // Output: '987.654,32 €'" |
| 1217 | + ], |
| 1218 | + "tags": ["javascript", "number", "currency", "utility"], |
| 1219 | + "author": "axorax" |
| 1220 | + }, |
| 1221 | + { |
| 1222 | + "title": "Convert Number to Roman Numerals", |
| 1223 | + "description": "Converts a number to Roman numeral representation.", |
| 1224 | + "code": [ |
| 1225 | + "const numberToRoman = (num) => {", |
| 1226 | + " const romanNumerals = {", |
| 1227 | + " 1: 'I', 4: 'IV', 5: 'V', 9: 'IX', 10: 'X', 40: 'XL', 50: 'L',", |
| 1228 | + " 90: 'XC', 100: 'C', 400: 'CD', 500: 'D', 900: 'CM', 1000: 'M'", |
| 1229 | + " };", |
| 1230 | + " let result = '';", |
| 1231 | + " Object.keys(romanNumerals).reverse().forEach(value => {", |
| 1232 | + " while (num >= value) {", |
| 1233 | + " result += romanNumerals[value];", |
| 1234 | + " num -= value;", |
| 1235 | + " }", |
| 1236 | + " });", |
| 1237 | + " return result;", |
| 1238 | + "};", |
| 1239 | + "", |
| 1240 | + "// Usage:", |
| 1241 | + "console.log(numberToRoman(1994)); // Output: 'MCMXCIV'", |
| 1242 | + "console.log(numberToRoman(58)); // Output: 'LVIII'" |
| 1243 | + ], |
| 1244 | + "tags": ["javascript", "number", "roman", "utility"], |
| 1245 | + "author": "axorax" |
| 1246 | + }, |
| 1247 | + { |
| 1248 | + "title": "Number to Words Converter", |
| 1249 | + "description": "Converts a number to its word representation in English.", |
| 1250 | + "code": [ |
| 1251 | + "const numberToWords = (num) => {", |
| 1252 | + " const below20 = ['Zero', 'One', 'Two', 'Three', 'Four', 'Five', 'Six', 'Seven', 'Eight', 'Nine', 'Ten', 'Eleven', 'Twelve', 'Thirteen', 'Fourteen', 'Fifteen', 'Sixteen', 'Seventeen', 'Eighteen', 'Nineteen'];", |
| 1253 | + " const tens = ['', '', 'Twenty', 'Thirty', 'Forty', 'Fifty', 'Sixty', 'Seventy', 'Eighty', 'Ninety'];", |
| 1254 | + " const above1000 = ['Hundred', 'Thousand', 'Million', 'Billion'];", |
| 1255 | + " if (num < 20) return below20[num];", |
| 1256 | + " let words = '';", |
| 1257 | + " for (let i = 0; num > 0; i++) {", |
| 1258 | + " if (i > 0 && num % 1000 !== 0) words = above1000[i] + ' ' + words;", |
| 1259 | + " if (num % 100 >= 20) {", |
| 1260 | + " words = tens[Math.floor(num / 10)] + ' ' + words;", |
| 1261 | + " num %= 10;", |
| 1262 | + " }", |
| 1263 | + " if (num < 20) words = below20[num] + ' ' + words;", |
| 1264 | + " num = Math.floor(num / 100);", |
| 1265 | + " }", |
| 1266 | + " return words.trim();", |
| 1267 | + "};", |
| 1268 | + "", |
| 1269 | + "// Usage:", |
| 1270 | + "console.log(numberToWords(123)); // Output: 'One Hundred Twenty Three'", |
| 1271 | + "console.log(numberToWords(2045)); // Output: 'Two Thousand Forty Five'" |
| 1272 | + ], |
| 1273 | + "tags": ["javascript", "number", "words", "utility"], |
| 1274 | + "author": "axorax" |
| 1275 | + }, |
| 1276 | + { |
| 1277 | + "title": "Convert to Scientific Notation", |
| 1278 | + "description": "Converts a number to scientific notation.", |
| 1279 | + "code": [ |
| 1280 | + "const toScientificNotation = (num) => {", |
| 1281 | + " if (isNaN(num)) {", |
| 1282 | + " throw new Error('Input must be a number');", |
| 1283 | + " }", |
| 1284 | + " if (num === 0) {", |
| 1285 | + " return '0e+0';", |
| 1286 | + " }", |
| 1287 | + " const exponent = Math.floor(Math.log10(Math.abs(num)));", |
| 1288 | + " const mantissa = num / Math.pow(10, exponent);", |
| 1289 | + " return `${mantissa.toFixed(2)}e${exponent >= 0 ? '+' : ''}${exponent}`;", |
| 1290 | + "};", |
| 1291 | + "", |
| 1292 | + "// Usage:", |
| 1293 | + "console.log(toScientificNotation(12345)); // Output: '1.23e+4'", |
| 1294 | + "console.log(toScientificNotation(0.0005678)); // Output: '5.68e-4'", |
| 1295 | + "console.log(toScientificNotation(1000)); // Output: '1.00e+3'", |
| 1296 | + "console.log(toScientificNotation(0)); // Output: '0e+0'", |
| 1297 | + "console.log(toScientificNotation(-54321)); // Output: '-5.43e+4'" |
| 1298 | + ], |
| 1299 | + "tags": ["javascript", "number", "scientific", "utility"], |
| 1300 | + "author": "axorax" |
1007 | 1301 | }
|
1008 | 1302 | ]
|
1009 | 1303 | },
|
|
0 commit comments