|
371 | 371 | "utility" |
372 | 372 | ], |
373 | 373 | "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" |
467 | 374 | } |
468 | 375 | ] |
469 | 376 | }, |
|
752 | 659 | } |
753 | 660 | ] |
754 | 661 | }, |
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 | | - }, |
782 | 662 | { |
783 | 663 | "language": "python", |
784 | 664 | "categoryName": "String Manipulation", |
|
0 commit comments