|
80 | 80 | ],
|
81 | 81 | "tags": ["javascript", "string", "reverse", "utility"],
|
82 | 82 | "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": "Filter out entries in an object where the value is falsy, including empty strings, empty objects, null, and undefined.", |
| 125 | + "code": [ |
| 126 | + "export const filterObject = (object = {}) =>", |
| 127 | + " Object.fromEntries(", |
| 128 | + " Object.entries(object)", |
| 129 | + " .filter(([key, value]) => value !== null && value !== undefined && value !== '' && (typeof value !== 'object' || Object.keys(value).length > 0))", |
| 130 | + " );", |
| 131 | + "", |
| 132 | + "// Usage:", |
| 133 | + "const obj1 = { a: 1, b: null, c: undefined, d: 4, e: '', f: {} };", |
| 134 | + "console.log(filterObject(obj1)); // Output: { a: 1, d: 4 }", |
| 135 | + "", |
| 136 | + "const obj2 = { x: 0, y: false, z: 'Hello', w: [] };", |
| 137 | + "console.log(filterObject(obj2)); // Output: { z: 'Hello' }", |
| 138 | + "", |
| 139 | + "const obj3 = { name: 'John', age: null, address: { city: 'New York' }, phone: '' };", |
| 140 | + "console.log(filterObject(obj3)); // Output: { name: 'John', address: { city: 'New York' } }", |
| 141 | + "", |
| 142 | + "const obj4 = { a: 0, b: '', c: false, d: {}, e: 'Valid' };", |
| 143 | + "console.log(filterObject(obj4)); // Output: { e: 'Valid' }" |
| 144 | + ], |
| 145 | + "tags": ["javascript", "object", "filter", "utility"], |
| 146 | + "author": "realvishalrana" |
| 147 | + }, |
| 148 | + { |
| 149 | + "title": "Get Nested Value", |
| 150 | + "description": "Retrieves the value at a given path in a nested object.", |
| 151 | + "code": [ |
| 152 | + "const getNestedValue = (obj, path) => {", |
| 153 | + " const keys = path.split('.');", |
| 154 | + " return keys.reduce((currentObject, key) => {", |
| 155 | + " return currentObject && typeof currentObject === 'object' ? currentObject[key] : undefined;", |
| 156 | + " }, obj);", |
| 157 | + "};", |
| 158 | + "", |
| 159 | + "// Usage:", |
| 160 | + "const obj = { a: { b: { c: 42 } } };", |
| 161 | + "console.log(getNestedValue(obj, 'a.b.c')); // Output: 42" |
| 162 | + ], |
| 163 | + "tags": ["javascript", "object", "nested", "utility"], |
| 164 | + "author": "realvishalrana" |
| 165 | + }, |
| 166 | + { |
| 167 | + "title": "Unique By Key", |
| 168 | + "description": "Filters an array of objects to only include unique objects by a specified key.", |
| 169 | + "code": [ |
| 170 | + "const uniqueByKey = (key, arr) =>", |
| 171 | + " arr.filter((obj, index, self) => index === self.findIndex((t) => t?.[key] === obj?.[key]));", |
| 172 | + "", |
| 173 | + "// Usage:", |
| 174 | + "const arr = [", |
| 175 | + " { id: 1, name: 'John' },", |
| 176 | + " { id: 2, name: 'Jane' },", |
| 177 | + " { id: 1, name: 'John' }", |
| 178 | + "];", |
| 179 | + "console.log(uniqueByKey('id', arr)); // Output: [{ id: 1, name: 'John' }, { id: 2, name: 'Jane' }]" |
| 180 | + ], |
| 181 | + "tags": ["javascript", "array", "unique", "utility"], |
| 182 | + "author": "realvishalrana" |
83 | 183 | }
|
84 | 184 | ]
|
85 | 185 | },
|
|
313 | 413 | "author": "dostonnabotov"
|
314 | 414 | }
|
315 | 415 | ]
|
316 |
| - } |
| 416 | + }, |
| 417 | +{ |
| 418 | + "categoryName": "Number Formatting", |
| 419 | + "snippets": [ |
| 420 | + { |
| 421 | + "title": "Number Formatter", |
| 422 | + "description": "Formats a number with suffixes (K, M, B, etc.).", |
| 423 | + "code": [ |
| 424 | + "const nFormatter = (num) => {", |
| 425 | + " if (!num) return;", |
| 426 | + " num = parseFloat(num.toString().replace(/[^0-9.]/g, ''));", |
| 427 | + " const suffixes = ['', 'K', 'M', 'B', 'T', 'P', 'E'];", |
| 428 | + " let index = 0;", |
| 429 | + " while (num >= 1000 && index < suffixes.length - 1) {", |
| 430 | + " num /= 1000;", |
| 431 | + " index++;", |
| 432 | + " }", |
| 433 | + " return num.toFixed(2).replace(/\\.0+$|(\\.[0-9]*[1-9])0+$/, '$1') + suffixes[index];", |
| 434 | + "};", |
| 435 | + "", |
| 436 | + "// Usage:", |
| 437 | + "console.log(nFormatter(1234567)); // Output: '1.23M'" |
| 438 | + ], |
| 439 | + "tags": ["javascript", "number", "format", "utility"], |
| 440 | + "author": "realvishalrana" |
| 441 | + } |
| 442 | + ] |
| 443 | +} |
| 444 | + |
317 | 445 | ]
|
0 commit comments