Skip to content

Commit 0aca506

Browse files
committed
Update consolidated snippets
1 parent e33b0ad commit 0aca506

File tree

1 file changed

+159
-0
lines changed

1 file changed

+159
-0
lines changed

public/consolidated/all_snippets.json

Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -421,6 +421,132 @@
421421
"utility"
422422
],
423423
"author": "dostonnabotov"
424+
},
425+
{
426+
"title": "Truncate Text",
427+
"description": "Truncates the text to a maximum length and appends '...' if the text exceeds the maximum length.",
428+
"code": [
429+
"const truncateText = (text = '', maxLength = 50) => {",
430+
" return `${text.slice(0, maxLength)}${text.length >= maxLength ? '...' : ''}`;",
431+
"};",
432+
"",
433+
"// Usage:",
434+
"const title = \"Hello, World! This is a Test.\";",
435+
"console.log(truncateText(title)); // Output: 'Hello, World! This is a Test.'",
436+
"console.log(truncateText(title, 10)); // Output: 'Hello, Wor...'"
437+
],
438+
"tags": [
439+
"javascript",
440+
"string",
441+
"truncate",
442+
"utility",
443+
"text"
444+
],
445+
"author": "realvishalrana"
446+
},
447+
{
448+
"title": "Data with Prefix",
449+
"description": "Adds a prefix and postfix to data, with a fallback value.",
450+
"code": [
451+
"const dataWithPrefix = (data, fallback = '-', prefix = '', postfix = '') => {",
452+
" return data ? `${prefix}${data}${postfix}` : fallback;",
453+
"};",
454+
"",
455+
"// Usage:",
456+
"console.log(dataWithPrefix('123', '-', '(', ')')); // Output: '(123)'",
457+
"console.log(dataWithPrefix('', '-', '(', ')')); // Output: '-'",
458+
"console.log(dataWithPrefix('Hello', 'N/A', 'Mr. ', '')); // Output: 'Mr. Hello'",
459+
"console.log(dataWithPrefix(null, 'N/A', 'Mr. ', '')); // Output: 'N/A'"
460+
],
461+
"tags": [
462+
"javascript",
463+
"data",
464+
"utility"
465+
],
466+
"author": "realvishalrana"
467+
}
468+
]
469+
},
470+
{
471+
"language": "javascript",
472+
"categoryName": "Object Manipulation",
473+
"snippets": [
474+
{
475+
"title": "Filter Object",
476+
"description": "Filter out entries in an object where the value is falsy, including empty strings, empty objects, null, and undefined.",
477+
"code": [
478+
"export const filterObject = (object = {}) =>",
479+
" Object.fromEntries(",
480+
" Object.entries(object)",
481+
" .filter(([key, value]) => value !== null && value !== undefined && value !== '' && (typeof value !== 'object' || Object.keys(value).length > 0))",
482+
" );",
483+
"",
484+
"// Usage:",
485+
"const obj1 = { a: 1, b: null, c: undefined, d: 4, e: '', f: {} };",
486+
"console.log(filterObject(obj1)); // Output: { a: 1, d: 4 }",
487+
"",
488+
"const obj2 = { x: 0, y: false, z: 'Hello', w: [] };",
489+
"console.log(filterObject(obj2)); // Output: { z: 'Hello' }",
490+
"",
491+
"const obj3 = { name: 'John', age: null, address: { city: 'New York' }, phone: '' };",
492+
"console.log(filterObject(obj3)); // Output: { name: 'John', address: { city: 'New York' } }",
493+
"",
494+
"const obj4 = { a: 0, b: '', c: false, d: {}, e: 'Valid' };",
495+
"console.log(filterObject(obj4)); // Output: { e: 'Valid' }"
496+
],
497+
"tags": [
498+
"javascript",
499+
"object",
500+
"filter",
501+
"utility"
502+
],
503+
"author": "realvishalrana"
504+
},
505+
{
506+
"title": "Get Nested Value",
507+
"description": "Retrieves the value at a given path in a nested object.",
508+
"code": [
509+
"const getNestedValue = (obj, path) => {",
510+
" const keys = path.split('.');",
511+
" return keys.reduce((currentObject, key) => {",
512+
" return currentObject && typeof currentObject === 'object' ? currentObject[key] : undefined;",
513+
" }, obj);",
514+
"};",
515+
"",
516+
"// Usage:",
517+
"const obj = { a: { b: { c: 42 } } };",
518+
"console.log(getNestedValue(obj, 'a.b.c')); // Output: 42"
519+
],
520+
"tags": [
521+
"javascript",
522+
"object",
523+
"nested",
524+
"utility"
525+
],
526+
"author": "realvishalrana"
527+
},
528+
{
529+
"title": "Unique By Key",
530+
"description": "Filters an array of objects to only include unique objects by a specified key.",
531+
"code": [
532+
"const uniqueByKey = (key, arr) =>",
533+
" arr.filter((obj, index, self) => index === self.findIndex((t) => t?.[key] === obj?.[key]));",
534+
"",
535+
"// Usage:",
536+
"const arr = [",
537+
" { id: 1, name: 'John' },",
538+
" { id: 2, name: 'Jane' },",
539+
" { id: 1, name: 'John' }",
540+
"];",
541+
"console.log(uniqueByKey('id', arr)); // Output: [{ id: 1, name: 'John' }, { id: 2, name: 'Jane' }]"
542+
],
543+
"tags": [
544+
"javascript",
545+
"array",
546+
"unique",
547+
"utility"
548+
],
549+
"author": "realvishalrana"
424550
}
425551
]
426552
},
@@ -709,6 +835,39 @@
709835
}
710836
]
711837
},
838+
{
839+
"language": "javascript",
840+
"categoryName": "Number Formatting",
841+
"snippets": [
842+
{
843+
"title": "Number Formatter",
844+
"description": "Formats a number with suffixes (K, M, B, etc.).",
845+
"code": [
846+
"const nFormatter = (num) => {",
847+
" if (!num) return;",
848+
" num = parseFloat(num.toString().replace(/[^0-9.]/g, ''));",
849+
" const suffixes = ['', 'K', 'M', 'B', 'T', 'P', 'E'];",
850+
" let index = 0;",
851+
" while (num >= 1000 && index < suffixes.length - 1) {",
852+
" num /= 1000;",
853+
" index++;",
854+
" }",
855+
" return num.toFixed(2).replace(/\\.0+$|(\\.[0-9]*[1-9])0+$/, '$1') + suffixes[index];",
856+
"};",
857+
"",
858+
"// Usage:",
859+
"console.log(nFormatter(1234567)); // Output: '1.23M'"
860+
],
861+
"tags": [
862+
"javascript",
863+
"number",
864+
"format",
865+
"utility"
866+
],
867+
"author": "realvishalrana"
868+
}
869+
]
870+
},
712871
{
713872
"language": "python",
714873
"categoryName": "String Manipulation",

0 commit comments

Comments
 (0)