@ruxandrafed/simple-utils
v1.2.7
Published
A simple utility package with common helper functions
Maintainers
Readme
@ruxandrafed/simple-utils
A simple utility package with common helper functions for JavaScript/TypeScript projects.
Installation
npm install @ruxandrafed/simple-utilsUsage
import {
capitalize,
randomString,
isValidEmail,
formatCurrency,
debounce,
sleep,
deepClone,
formatDate,
addDaysToDate,
daysDifference,
isDateAfter,
isDateBefore,
generateId,
removeDuplicates,
chunk
} from '@ruxandrafed/simple-utils';
// Capitalize first letter
console.log(capitalize('hello world')); // "Hello world"
// Generate random string
console.log(randomString(10)); // "aB3xY9mK2p"
// Validate email
console.log(isValidEmail('[email protected]')); // true
// Format currency
console.log(formatCurrency(1234.56)); // "$1,234.56"
// Deep clone objects
const original = { a: 1, b: { c: 2 } };
const cloned = deepClone(original);
// Format dates
console.log(formatDate(new Date())); // "2023-12-25"
console.log(formatDate(new Date(), 'dd/MM/yyyy')); // "25/12/2023"
// Date operations
const tomorrow = addDaysToDate(new Date(), 1);
const daysDiff = daysDifference(new Date('2023-12-30'), new Date('2023-12-25')); // 5
// Generate unique IDs
console.log(generateId()); // "1"
console.log(generateId('user')); // "user_2"
// Remove duplicates
console.log(removeDuplicates([1, 2, 2, 3, 3, 4])); // [1, 2, 3, 4]
// Split array into chunks
console.log(chunk([1, 2, 3, 4, 5, 6], 2)); // [[1, 2], [3, 4], [5, 6]]
// Debounce function calls
const debouncedFn = debounce(() => {
console.log('This will only run once after 300ms of inactivity');
}, 300);
// Sleep for 1 second
await sleep(1000);API Reference
capitalize(str: string): string
Capitalizes the first letter of a string.
randomString(length?: number): string
Generates a random string of specified length (default: 8).
isValidEmail(email: string): boolean
Checks if a string is a valid email address.
formatCurrency(amount: number, currency?: string): string
Formats a number as currency (default currency: 'USD').
debounce(func: Function, delay: number): Function
Creates a debounced version of a function that delays execution.
sleep(ms: number): Promise
Returns a promise that resolves after the specified number of milliseconds.
deepClone(obj: T): T
Deep clones an object using lodash.
formatDate(date: Date, formatStr?: string): string
Formats a date using date-fns (default format: 'yyyy-MM-dd').
addDaysToDate(date: Date, days: number): Date
Adds days to a date and returns a new Date object.
daysDifference(date1: Date, date2: Date): number
Calculates the difference in days between two dates.
isDateAfter(date: Date, compareDate: Date): boolean
Checks if a date is after another date.
isDateBefore(date: Date, compareDate: Date): boolean
Checks if a date is before another date.
generateId(prefix?: string): string
Generates a unique ID using lodash (optionally with a prefix).
removeDuplicates(array: T[]): T[]
Removes duplicates from an array using lodash.
chunk(array: T[], size: number): T[][]
Splits an array into chunks of specified size using lodash.
Dependencies
This package includes the following dependencies:
- lodash: For utility functions like deep cloning and array operations
- date-fns: For date manipulation and formatting
License
MIT
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
