|
2 | 2 | {
|
3 | 3 | "categoryName": "Array Manipulation",
|
4 | 4 | "snippets": [
|
| 5 | + { |
| 6 | + "title": "Slugify String", |
| 7 | + "description": "Converts a string into a URL-friendly slug format.", |
| 8 | + "code": [ |
| 9 | + "const slugify = (string, separator = \"-\") => {", |
| 10 | + " return string", |
| 11 | + " .toString() // Cast to string (optional)", |
| 12 | + " .toLowerCase() // Convert the string to lowercase letters", |
| 13 | + " .trim() // Remove whitespace from both sides of a string (optional)", |
| 14 | + " .replace(/\\s+/g, separator) // Replace spaces with {separator}", |
| 15 | + " .replace(/[^\\w\\-]+/g, \"\") // Remove all non-word chars", |
| 16 | + " .replace(/\\_/g, separator) // Replace _ with {separator}", |
| 17 | + " .replace(/\\-\\-+/g, separator) // Replace multiple - with single {separator}", |
| 18 | + " .replace(/\\-$/g, \"\"); // Remove trailing -", |
| 19 | + "};", |
| 20 | + "", |
| 21 | + "// Usage:", |
| 22 | + "const title = \"Hello, World! This is a Test.\";", |
| 23 | + "console.log(slugify(title)); // Output: 'hello-world-this-is-a-test'", |
| 24 | + "console.log(slugify(title, \"_\")); // Output: 'hello_world_this_is_a_test'" |
| 25 | + ], |
| 26 | + "tags": ["javascript", "string", "slug", "utility"], |
| 27 | + "author": "dostonnabotov" |
| 28 | + }, |
5 | 29 | {
|
6 | 30 | "title": "Remove Duplicates",
|
7 | 31 | "description": "Removes duplicate values from an array.",
|
|
94 | 118 | ]
|
95 | 119 | },
|
96 | 120 | {
|
97 |
| - "categoryName": "Utilities", |
| 121 | + "categoryName": "Function Utilities", |
98 | 122 | "snippets": [
|
| 123 | + { |
| 124 | + "title": "Repeat Function Invocation", |
| 125 | + "description": "Invokes a function a specified number of times.", |
| 126 | + "code": [ |
| 127 | + "const times = (func, n) => {", |
| 128 | + " Array.from(Array(n)).forEach(() => {", |
| 129 | + " func();", |
| 130 | + " });", |
| 131 | + "};", |
| 132 | + "", |
| 133 | + "// Usage:", |
| 134 | + "const randomFunction = () => console.log('Function called!');", |
| 135 | + "times(randomFunction, 3); // Logs 'Function called!' three times" |
| 136 | + ], |
| 137 | + "tags": ["javascript", "function", "repeat", "utility"], |
| 138 | + "author": "dostonnabotov" |
| 139 | + }, |
99 | 140 | {
|
100 | 141 | "title": "Debounce Function",
|
101 | 142 | "description": "Delays a function execution until after a specified time.",
|
|
180 | 221 | "author": "dostonnabotov"
|
181 | 222 | }
|
182 | 223 | ]
|
| 224 | + }, |
| 225 | + { |
| 226 | + "categoryName": "Local Storage", |
| 227 | + "snippets": [ |
| 228 | + { |
| 229 | + "title": "Add Item to localStorage", |
| 230 | + "description": "Stores a value in localStorage under the given key.", |
| 231 | + "code": [ |
| 232 | + "const addToLocalStorage = (key, value) => {", |
| 233 | + " localStorage.setItem(key, JSON.stringify(value));", |
| 234 | + "};", |
| 235 | + "", |
| 236 | + "// Usage:", |
| 237 | + "addToLocalStorage('user', { name: 'John', age: 30 });" |
| 238 | + ], |
| 239 | + "tags": ["javascript", "localStorage", "storage", "utility"], |
| 240 | + "author": "dostonnabotov" |
| 241 | + }, |
| 242 | + { |
| 243 | + "title": "Retrieve Item from localStorage", |
| 244 | + "description": "Retrieves a value from localStorage by key and parses it.", |
| 245 | + "code": [ |
| 246 | + "const getFromLocalStorage = (key) => {", |
| 247 | + " const item = localStorage.getItem(key);", |
| 248 | + " return item ? JSON.parse(item) : null;", |
| 249 | + "};", |
| 250 | + "", |
| 251 | + "// Usage:", |
| 252 | + "const user = getFromLocalStorage('user');", |
| 253 | + "console.log(user); // Output: { name: 'John', age: 30 }" |
| 254 | + ], |
| 255 | + "tags": ["javascript", "localStorage", "storage", "utility"], |
| 256 | + "author": "dostonnabotov" |
| 257 | + }, |
| 258 | + { |
| 259 | + "title": "Clear All localStorage", |
| 260 | + "description": "Clears all data from localStorage.", |
| 261 | + "code": [ |
| 262 | + "const clearLocalStorage = () => {", |
| 263 | + " localStorage.clear();", |
| 264 | + "};", |
| 265 | + "", |
| 266 | + "// Usage:", |
| 267 | + "clearLocalStorage(); // Removes all items from localStorage" |
| 268 | + ], |
| 269 | + "tags": ["javascript", "localStorage", "storage", "utility"], |
| 270 | + "author": "dostonnabotov" |
| 271 | + } |
| 272 | + ] |
183 | 273 | }
|
184 | 274 | ]
|
0 commit comments