Skip to content

Commit 0b19841

Browse files
committed
Added some snippets for javascript
1 parent 7657066 commit 0b19841

File tree

1 file changed

+91
-1
lines changed

1 file changed

+91
-1
lines changed

public/data/javascript.json

Lines changed: 91 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,30 @@
22
{
33
"categoryName": "Array Manipulation",
44
"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+
},
529
{
630
"title": "Remove Duplicates",
731
"description": "Removes duplicate values from an array.",
@@ -94,8 +118,25 @@
94118
]
95119
},
96120
{
97-
"categoryName": "Utilities",
121+
"categoryName": "Function Utilities",
98122
"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+
},
99140
{
100141
"title": "Debounce Function",
101142
"description": "Delays a function execution until after a specified time.",
@@ -180,5 +221,54 @@
180221
"author": "dostonnabotov"
181222
}
182223
]
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+
]
183273
}
184274
]

0 commit comments

Comments
 (0)