@tedckh/useful-code-snippets
v1.1.0
Published
A collection of useful TypeScript utility functions
Maintainers
Readme
@tedckh/useful-code-snippets
A collection of useful TypeScript utility functions for common development tasks.
Installation
npm install @tedckh/useful-code-snippets
# or
yarn add @tedckh/useful-code-snippetsUsage
import {
debounce,
throttle,
classNames,
formatDate,
deepClone,
generateId,
pick,
omit
} from '@tedckh/useful-code-snippets';
// Debounce and throttle function calls
const debouncedSearch = debounce((query) => {
console.log('Searching for:', query);
}, 500);
const throttledScroll = throttle(() => {
console.log('Scrolling...');
}, 100);
// Combine CSS class names
const className = classNames('btn', { active: true, disabled: false });
// Result: 'btn active'
// Deep clone objects
const original = { user: { name: 'John', age: 30 } };
const cloned = deepClone(original);
// Generate random IDs
const id = generateId(12); // Random 12-character ID
// Object utilities
const user = { name: 'John', age: 30, email: '[email protected]' };
const picked = pick(user, ['name', 'email']); // { name: 'John', email: '[email protected]' }
const omitted = omit(user, ['age']); // { name: 'John', email: '[email protected]' }Available Functions
Function Control
debounce- Delay function execution until after specified timethrottle- Limit function execution to once per time period
Object Utilities
deepClone- Create deep copies of objectspick- Extract specific properties from objectsomit- Remove specific properties from objectsdeepMerge- Deeply merge two objectsisEmpty- Check if object is emptyshapeObject- Shape objects to match schemas
Utilities
classNames- Combine CSS class names with conditional logicformatDate- Format dates with locale supportgenerateId- Generate random ID stringssleep- Async delay utilityclipboard- Clipboard utilitiesdownload- File download helperslocalStorage- Local storage utilities
License
MIT
