Skip to content

Commit 151c71d

Browse files
committed
Add more JS snippets
1 parent 7218bed commit 151c71d

File tree

1 file changed

+90
-0
lines changed

1 file changed

+90
-0
lines changed

public/data/javascript.json

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,96 @@
184184
],
185185
"tags": ["string", "whitespace"],
186186
"author": "axorax"
187+
},
188+
{
189+
"title": "Pad String on Both Sides",
190+
"description": "Pads a string on both sides with a specified character until it reaches the desired length.",
191+
"code": [
192+
"function padString(str, length, char = ' ') {",
193+
" const totalPad = length - str.length;",
194+
" const padStart = Math.floor(totalPad / 2);",
195+
" const padEnd = totalPad - padStart;",
196+
" return char.repeat(padStart) + str + char.repeat(padEnd);",
197+
"}",
198+
"",
199+
"// Example usage:",
200+
"console.log(padString('hello', 10, '*')); // Output: '**hello***'"
201+
],
202+
"tags": ["string", "pad", "manipulation"],
203+
"author": "axorax"
204+
},
205+
{
206+
"title": "Convert String to Snake Case",
207+
"description": "Converts a given string into snake_case.",
208+
"code": [
209+
"function toSnakeCase(str) {",
210+
" return str.replace(/([a-z])([A-Z])/g, '$1_$2')",
211+
" .replace(/\\s+/g, '_')",
212+
" .toLowerCase();",
213+
"}",
214+
"",
215+
"// Example usage:",
216+
"console.log(toSnakeCase('Hello World Test')); // Output: 'hello_world_test'"
217+
],
218+
"tags": ["string", "case", "snake_case"],
219+
"author": "axorax"
220+
},
221+
{
222+
"title": "Remove Vowels from a String",
223+
"description": "Removes all vowels from a given string.",
224+
"code": [
225+
"function removeVowels(str) {",
226+
" return str.replace(/[aeiouAEIOU]/g, '');",
227+
"}",
228+
"",
229+
"// Example usage:",
230+
"console.log(removeVowels('Hello World')); // Output: 'Hll Wrld'"
231+
],
232+
"tags": ["string", "remove", "vowels"],
233+
"author": "axorax"
234+
},
235+
{
236+
"title": "Mask Sensitive Information",
237+
"description": "Masks parts of a sensitive string, like a credit card or email address.",
238+
"code": [
239+
"function maskSensitiveInfo(str, visibleCount = 4, maskChar = '*') {",
240+
" return str.slice(0, visibleCount) + maskChar.repeat(Math.max(0, str.length - visibleCount));",
241+
"}",
242+
"",
243+
"// Example usage:",
244+
"console.log(maskSensitiveInfo('123456789', 4)); // Output: '1234*****'",
245+
"console.log(maskSensitiveInfo('[email protected]', 2, '#')); // Output: 'ex#############'"
246+
],
247+
"tags": ["string", "mask", "sensitive"],
248+
"author": "axorax"
249+
},
250+
{
251+
"title": "Extract Initials from Name",
252+
"description": "Extracts and returns the initials from a full name.",
253+
"code": [
254+
"function getInitials(name) {",
255+
" return name.split(' ').map(part => part.charAt(0).toUpperCase()).join('');",
256+
"}",
257+
"",
258+
"// Example usage:",
259+
"console.log(getInitials('John Doe')); // Output: 'JD'"
260+
],
261+
"tags": ["string", "initials", "name"],
262+
"author": "axorax"
263+
},
264+
{
265+
"title": "Convert Tabs to Spaces",
266+
"description": "Converts all tab characters in a string to spaces.",
267+
"code": [
268+
"function tabsToSpaces(str, spacesPerTab = 4) {",
269+
" return str.replace(/\\t/g, ' '.repeat(spacesPerTab));",
270+
"}",
271+
"",
272+
"// Example usage:",
273+
"console.log(tabsToSpaces('Hello\\tWorld', 2)); // Output: 'Hello World'"
274+
],
275+
"tags": ["string", "tabs", "spaces"],
276+
"author": "axorax"
187277
}
188278
]
189279
},

0 commit comments

Comments
 (0)