Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 599e984

Browse files
✨ Add utility functions and snippets to javascript.json
1 parent 600723c commit 599e984

File tree

2 files changed

+241
-1
lines changed

2 files changed

+241
-1
lines changed

‎public/consolidated/all_snippets.json

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,99 @@
371371
"utility"
372372
],
373373
"author": "dostonnabotov"
374+
},
375+
{
376+
"title": "Truncate Text",
377+
"description": "Truncates the text to a maximum length and appends '...' if the text exceeds the maximum length.",
378+
"code": [
379+
"const truncateText = (text = '', maxLength = 50) => {",
380+
" return `${text.slice(0, maxLength)}${text.length >= maxLength ? '...' : ''}`;",
381+
"};",
382+
"",
383+
"// Usage:",
384+
"const title = \"Hello, World! This is a Test.\";",
385+
"console.log(truncateText(title)); // Output: 'Hello, World! This is a Test.'",
386+
"console.log(truncateText(title, 10)); // Output: 'Hello, Wor...'"
387+
],
388+
"tags": ["javascript", "string", "truncate", "utility", "text"],
389+
"author": "realvishalrana"
390+
},
391+
{
392+
"title": "Data with Prefix",
393+
"description": "Adds a prefix and postfix to data, with a fallback value.",
394+
"code": [
395+
"const dataWithPrefix = (data, fallback = '-', prefix = '', postfix = '') => {",
396+
" return data ? `${prefix}${data}${postfix}` : fallback;",
397+
"};",
398+
"",
399+
"// Usage:",
400+
"console.log(dataWithPrefix('123', '-', '(', ')')); // Output: '(123)'",
401+
"console.log(dataWithPrefix('', '-', '(', ')')); // Output: '-'",
402+
"console.log(dataWithPrefix('Hello', 'N/A', 'Mr. ', '')); // Output: 'Mr. Hello'",
403+
"console.log(dataWithPrefix(null, 'N/A', 'Mr. ', '')); // Output: 'N/A'"
404+
],
405+
"tags": ["javascript", "data", "utility"],
406+
"author": "realvishalrana"
407+
}
408+
]
409+
},
410+
{
411+
"language": "javascript",
412+
"categoryName": "Object Manipulation",
413+
"snippets": [
414+
{
415+
"title": "Filter Object",
416+
"description": "Filters out entries in an object where the value is falsy.",
417+
"code": [
418+
"export const filterObject = (object = {}) =>",
419+
" Object.fromEntries(",
420+
" Object.entries(object)",
421+
" .map(([key, value]) => value && [key, value])",
422+
" .filter((item) => item),",
423+
" );",
424+
"",
425+
"// Usage:",
426+
"const obj = { a: 1, b: null, c: undefined, d: 4 };",
427+
"console.log(filterObject(obj)); // Output: { a: 1, d: 4 }"
428+
],
429+
"tags": ["javascript", "object", "filter", "utility"],
430+
"author": "realvishalrana"
431+
},
432+
{
433+
"title": "Get Nested Value",
434+
"description": "Retrieves the value at a given path in a nested object.",
435+
"code": [
436+
"const getNestedValue = (obj, path) => {",
437+
" const keys = path.split('.');",
438+
" return keys.reduce((currentObject, key) => {",
439+
" return currentObject && typeof currentObject === 'object' ? currentObject[key] : undefined;",
440+
" }, obj);",
441+
"};",
442+
"",
443+
"// Usage:",
444+
"const obj = { a: { b: { c: 42 } } };",
445+
"console.log(getNestedValue(obj, 'a.b.c')); // Output: 42"
446+
],
447+
"tags": ["javascript", "object", "nested", "utility"],
448+
"author": "realvishalrana"
449+
},
450+
{
451+
"title": "Unique By Key",
452+
"description": "Filters an array of objects to only include unique objects by a specified key.",
453+
"code": [
454+
"const uniqueByKey = (key, arr) =>",
455+
" arr.filter((obj, index, self) => index === self.findIndex((t) => t?.[key] === obj?.[key]));",
456+
"",
457+
"// Usage:",
458+
"const arr = [",
459+
" { id: 1, name: 'John' },",
460+
" { id: 2, name: 'Jane' },",
461+
" { id: 1, name: 'John' }",
462+
"];",
463+
"console.log(uniqueByKey('id', arr)); // Output: [{ id: 1, name: 'John' }, { id: 2, name: 'Jane' }]"
464+
],
465+
"tags": ["javascript", "array", "unique", "utility"],
466+
"author": "realvishalrana"
374467
}
375468
]
376469
},
@@ -659,6 +752,33 @@
659752
}
660753
]
661754
},
755+
{
756+
"categoryName": "Number Formatting",
757+
"snippets": [
758+
{
759+
"title": "Number Formatter",
760+
"description": "Formats a number with suffixes (K, M, B, etc.).",
761+
"code": [
762+
"const nFormatter = (num) => {",
763+
" if (!num) return;",
764+
" num = parseFloat(num.toString().replace(/[^0-9.]/g, ''));",
765+
" const suffixes = ['', 'K', 'M', 'B', 'T', 'P', 'E'];",
766+
" let index = 0;",
767+
" while (num >= 1000 && index < suffixes.length - 1) {",
768+
" num /= 1000;",
769+
" index++;",
770+
" }",
771+
" return num.toFixed(2).replace(/\\.0+$|(\\.[0-9]*[1-9])0+$/, '1ドル') + suffixes[index];",
772+
"};",
773+
"",
774+
"// Usage:",
775+
"console.log(nFormatter(1234567)); // Output: '1.23M'"
776+
],
777+
"tags": ["javascript", "number", "format", "utility"],
778+
"author": "realvishalrana"
779+
}
780+
]
781+
},
662782
{
663783
"language": "python",
664784
"categoryName": "String Manipulation",

‎public/data/javascript.json

Lines changed: 121 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,98 @@
8080
],
8181
"tags": ["javascript", "string", "reverse", "utility"],
8282
"author": "dostonnabotov"
83+
},
84+
{
85+
"title": "Truncate Text",
86+
"description": "Truncates the text to a maximum length and appends '...' if the text exceeds the maximum length.",
87+
"code": [
88+
"const truncateText = (text = '', maxLength = 50) => {",
89+
" return `${text.slice(0, maxLength)}${text.length >= maxLength ? '...' : ''}`;",
90+
"};",
91+
"",
92+
"// Usage:",
93+
"const title = \"Hello, World! This is a Test.\";",
94+
"console.log(truncateText(title)); // Output: 'Hello, World! This is a Test.'",
95+
"console.log(truncateText(title, 10)); // Output: 'Hello, Wor...'"
96+
],
97+
"tags": ["javascript", "string", "truncate", "utility", "text"],
98+
"author": "realvishalrana"
99+
},
100+
{
101+
"title": "Data with Prefix",
102+
"description": "Adds a prefix and postfix to data, with a fallback value.",
103+
"code": [
104+
"const dataWithPrefix = (data, fallback = '-', prefix = '', postfix = '') => {",
105+
" return data ? `${prefix}${data}${postfix}` : fallback;",
106+
"};",
107+
"",
108+
"// Usage:",
109+
"console.log(dataWithPrefix('123', '-', '(', ')')); // Output: '(123)'",
110+
"console.log(dataWithPrefix('', '-', '(', ')')); // Output: '-'",
111+
"console.log(dataWithPrefix('Hello', 'N/A', 'Mr. ', '')); // Output: 'Mr. Hello'",
112+
"console.log(dataWithPrefix(null, 'N/A', 'Mr. ', '')); // Output: 'N/A'"
113+
],
114+
"tags": ["javascript", "data", "utility"],
115+
"author": "realvishalrana"
116+
}
117+
]
118+
},
119+
{
120+
"categoryName": "Object Manipulation",
121+
"snippets": [
122+
{
123+
"title": "Filter Object",
124+
"description": "Filters out entries in an object where the value is falsy.",
125+
"code": [
126+
"export const filterObject = (object = {}) =>",
127+
" Object.fromEntries(",
128+
" Object.entries(object)",
129+
" .map(([key, value]) => value && [key, value])",
130+
" .filter((item) => item),",
131+
" );",
132+
"",
133+
"// Usage:",
134+
"const obj = { a: 1, b: null, c: undefined, d: 4 };",
135+
"console.log(filterObject(obj)); // Output: { a: 1, d: 4 }"
136+
],
137+
"tags": ["javascript", "object", "filter", "utility"],
138+
"author": "realvishalrana"
139+
},
140+
{
141+
"title": "Get Nested Value",
142+
"description": "Retrieves the value at a given path in a nested object.",
143+
"code": [
144+
"const getNestedValue = (obj, path) => {",
145+
" const keys = path.split('.');",
146+
" return keys.reduce((currentObject, key) => {",
147+
" return currentObject && typeof currentObject === 'object' ? currentObject[key] : undefined;",
148+
" }, obj);",
149+
"};",
150+
"",
151+
"// Usage:",
152+
"const obj = { a: { b: { c: 42 } } };",
153+
"console.log(getNestedValue(obj, 'a.b.c')); // Output: 42"
154+
],
155+
"tags": ["javascript", "object", "nested", "utility"],
156+
"author": "realvishalrana"
157+
},
158+
{
159+
"title": "Unique By Key",
160+
"description": "Filters an array of objects to only include unique objects by a specified key.",
161+
"code": [
162+
"const uniqueByKey = (key, arr) =>",
163+
" arr.filter((obj, index, self) => index === self.findIndex((t) => t?.[key] === obj?.[key]));",
164+
"",
165+
"// Usage:",
166+
"const arr = [",
167+
" { id: 1, name: 'John' },",
168+
" { id: 2, name: 'Jane' },",
169+
" { id: 1, name: 'John' }",
170+
"];",
171+
"console.log(uniqueByKey('id', arr)); // Output: [{ id: 1, name: 'John' }, { id: 2, name: 'Jane' }]"
172+
],
173+
"tags": ["javascript", "array", "unique", "utility"],
174+
"author": "realvishalrana"
83175
}
84176
]
85177
},
@@ -313,5 +405,33 @@
313405
"author": "dostonnabotov"
314406
}
315407
]
316-
}
408+
},
409+
{
410+
"categoryName": "Number Formatting",
411+
"snippets": [
412+
{
413+
"title": "Number Formatter",
414+
"description": "Formats a number with suffixes (K, M, B, etc.).",
415+
"code": [
416+
"const nFormatter = (num) => {",
417+
" if (!num) return;",
418+
" num = parseFloat(num.toString().replace(/[^0-9.]/g, ''));",
419+
" const suffixes = ['', 'K', 'M', 'B', 'T', 'P', 'E'];",
420+
" let index = 0;",
421+
" while (num >= 1000 && index < suffixes.length - 1) {",
422+
" num /= 1000;",
423+
" index++;",
424+
" }",
425+
" return num.toFixed(2).replace(/\\.0+$|(\\.[0-9]*[1-9])0+$/, '1ドル') + suffixes[index];",
426+
"};",
427+
"",
428+
"// Usage:",
429+
"console.log(nFormatter(1234567)); // Output: '1.23M'"
430+
],
431+
"tags": ["javascript", "number", "format", "utility"],
432+
"author": "realvishalrana"
433+
}
434+
]
435+
}
436+
317437
]

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /