|
885 | 885 | "description": "Composes multiple functions into a single function, where the output of one function becomes the input of the next.",
|
886 | 886 | "code": [
|
887 | 887 | "const compose = (...funcs) => (initialValue) => {",
|
888 | | - " return funcs.reduceRight((acc, func) => func(acc), initialValue);", |
| 888 | + " return funcs.reduce((acc, func) => func(acc), initialValue);", |
889 | 889 | "};",
|
890 | 890 | "",
|
891 | 891 | "// Usage:",
|
|
897 | 897 | "tags": ["javascript", "function", "compose", "utility"],
|
898 | 898 | "author": "axorax"
|
899 | 899 | },
|
900 | | - { |
901 | | - "title": "Pipe Functions", |
902 | | - "description": "Pipes multiple functions into a single function, where the output of one function becomes the input of the next.", |
903 | | - "code": [ |
904 | | - "const pipe = (...funcs) => (initialValue) => {", |
905 | | - " return funcs.reduce((acc, func) => func(acc), initialValue);", |
906 | | - "};", |
907 | | - "", |
908 | | - "// Usage:", |
909 | | - "const add2 = (x) => x + 2;", |
910 | | - "const multiply3 = (x) => x * 3;", |
911 | | - "const piped = pipe(add2, multiply3);", |
912 | | - "console.log(piped(5)); // Output: 21 ((5 + 2) * 3)" |
913 | | - ], |
914 | | - "tags": ["javascript", "function", "pipe", "utility"], |
915 | | - "author": "axorax" |
916 | | - }, |
917 | | - { |
918 | | - "title": "Once Per Interval", |
919 | | - "description": "Ensures that a function can only be invoked once within a specific time interval.", |
920 | | - "code": [ |
921 | | - "const oncePerInterval = (func, interval) => {", |
922 | | - " let lastCallTime = 0;", |
923 | | - " return (...args) => {", |
924 | | - " const now = Date.now();", |
925 | | - " if (now - lastCallTime >= interval) {", |
926 | | - " lastCallTime = now;", |
927 | | - " func(...args);", |
928 | | - " }", |
929 | | - " };", |
930 | | - "};", |
931 | | - "", |
932 | | - "// Usage:", |
933 | | - "const logMessage = () => console.log('Message logged');", |
934 | | - "const logOncePerSecond = oncePerInterval(logMessage, 1000);", |
935 | | - "setInterval(() => logOncePerSecond(), 200); // Logs once per second" |
936 | | - ], |
937 | | - "tags": ["javascript", "function", "interval", "utility"], |
938 | | - "author": "axorax" |
939 | | - }, |
940 | 900 | {
|
941 | 901 | "title": "Rate Limit Function",
|
942 | 902 | "description": "Limits how often a function can be executed within a given time window.",
|
|
1062 | 1022 | ],
|
1063 | 1023 | "tags": ["javascript", "dom", "remove", "utility"],
|
1064 | 1024 | "author": "axorax"
|
1065 | | - }, |
1066 | | - { |
1067 | | - "title": "Toggle Visibility", |
1068 | | - "description": "Toggles the visibility of an element.", |
1069 | | - "code": [ |
1070 | | - "const toggleVisibility = (element) => {", |
1071 | | - " const currentDisplay = window.getComputedStyle(element).display;", |
1072 | | - " element.style.display = currentDisplay === 'none' ? 'block' : 'none';", |
1073 | | - "};", |
1074 | | - "", |
1075 | | - "// Usage:", |
1076 | | - "const element = document.querySelector('.my-element');", |
1077 | | - "toggleVisibility(element);" |
1078 | | - ], |
1079 | | - "tags": ["javascript", "dom", "visibility", "toggle"], |
1080 | | - "author": "axorax" |
1081 | 1025 | }
|
1082 | 1026 | ]
|
1083 | 1027 | },
|
|
1143 | 1087 | "author": "axorax"
|
1144 | 1088 | },
|
1145 | 1089 | {
|
1146 | | - "title": "Check Storage Capacity", |
1147 | | - "description": "Checks the available space in the localStorage.", |
| 1090 | + "title": "Check bytes used", |
| 1091 | + "description": "Checks the amount of bytes used in the localStorage.", |
1148 | 1092 | "code": [
|
1149 | | - "const checkLocalStorageCapacity = () => {", |
| 1093 | + "const checkBytesUsed = () => {", |
1150 | 1094 | " let spaceUsed = 0;",
|
1151 | 1095 | " for (let i = 0; i < localStorage.length; i++) {",
|
1152 | 1096 | " spaceUsed += localStorage.key(i).length + localStorage.getItem(localStorage.key(i)).length;",
|
|
1155 | 1099 | "};",
|
1156 | 1100 | "",
|
1157 | 1101 | "// Usage:",
|
1158 | | - "checkLocalStorageCapacity();" |
| 1102 | + "checkBytesUsed();" |
1159 | 1103 | ],
|
1160 | 1104 | "tags": ["javascript", "localStorage", "storage", "utility"],
|
1161 | 1105 | "author": "axorax"
|
|
1276 | 1220 | "tags": ["javascript", "number", "words", "utility"],
|
1277 | 1221 | "author": "axorax"
|
1278 | 1222 | },
|
1279 | | - { |
1280 | | - "title": "Format Large Numbers with Abbreviations", |
1281 | | - "description": "Formats large numbers with abbreviations like K, M, B, etc.", |
1282 | | - "code": [ |
1283 | | - "const abbreviateNumber = (num) => {", |
1284 | | - " const abbreviations = ['K', 'M', 'B', 'T'];", |
1285 | | - " let i = 0;", |
1286 | | - " while (num >= 1000 && i < abbreviations.length) {", |
1287 | | - " num /= 1000;", |
1288 | | - " i++;", |
1289 | | - " }", |
1290 | | - " return num.toFixed(1) + abbreviations[i];", |
1291 | | - "};", |
1292 | | - "", |
1293 | | - "// Usage:", |
1294 | | - "console.log(abbreviateNumber(2500)); // Output: '2.5K'", |
1295 | | - "console.log(abbreviateNumber(10500000)); // Output: '10.5M'", |
1296 | | - "console.log(abbreviateNumber(1000000000)); // Output: '1.0B'" |
1297 | | - ], |
1298 | | - "tags": ["javascript", "number", "abbreviate", "utility"], |
1299 | | - "author": "axorax" |
1300 | | - }, |
1301 | 1223 | {
|
1302 | 1224 | "title": "Convert to Scientific Notation",
|
1303 | 1225 | "description": "Converts a number to scientific notation.",
|
|
0 commit comments