A comprehensive collection of TypeScript/JavaScript utility functions for both Node.js and browser environments. This library provides a wide range of helper functions for common programming tasks, including array manipulation, date handling, string operations, and more.
Using npm:
npm install @vsokolov/utils
Using bun:
bun add @vsokolov/utils
Using pnpm:
pnpm add @vsokolov/utils
You can import the entire library:
import * as utils from '@vsokolov/utils';
Or import individual functions for better tree-shaking:
import { flattenArray, formatDate, isString } from '@vsokolov/utils';
import { flattenArray, sortBy, unique } from '@vsokolov/utils';
// Flatten nested arrays
const nested = [1, [2, [3, [4]], 5]];
const flat = flattenArray(nested); // [1, 2, 3, 4, 5]
// Get unique values
const duplicates = [1, 2, 2, 3, 3, 3];
const uniqueValues = unique(duplicates); // [1, 2, 3]
// Sort array of objects
const users = [
{ name: 'John', age: 30 },
{ name: 'Jane', age: 25 },
{ name: 'Doe', age: 35 }
];
const sortedUsers = sortBy(users, 1, 'age'); // Sort by age in ascending order
import { formatDate, getMonthList, timeAgo } from '@vsokolov/utils';
// Format date
const today = new Date();
const formatted = formatDate(today); // '2023-06-30'
// Time ago
const yesterday = new Date();
yesterday.setDate(yesterday.getDate() - 1);
const timeSince = timeAgo(yesterday); // '1 day ago'
// Get month names
const months = getMonthList(); // ['January', 'February', ...]
import { isEmail, slugify, truncate } from '@vsokolov/utils';
// Truncate string
truncate('This is a long string', 10); // 'This is a...'
// Create URL-friendly slug
slugify('Hello World!'); // 'hello-world'
// Validate email
const isValid = isEmail('test@example.com'); // true
flattenArray<T>(array: T[]): T[] - Flatten nested arraysunique<T>(array: T[]): T[] - Get unique values from arraysortBy(arr: Record<string, any>[], order: 1 | -1, key: string) - Sort array of objects by keyremoveItem<T>(array: T[], values: T[]): T[] - Remove items from arrayrandomItem<T>(array: T[], count: number): T[] - Get random items from arrayintersection<T>(arr1: T[], arr2: T[]): T[] - Get intersection of two arrayscountBy(array: Array<number | string>): Record<string, number> - Count occurrences of each valueformatDate(date?: Date): string - Format date as YYYY-MM-DDtimeAgo(date: Date): string - Get time ago string (e.g., "2 hours ago")getMonthList(): string[] - Get list of month namestimestampToDate(timestamp: string | number): string - Convert timestamp to date stringdateTimeToCron(date: Date): string - Convert Date to cron syntaxcronToDateTime(cronSyntax: string): Date - Convert cron syntax to Datetruncate(str: string, maxLength: number, suffix = '...'): string - Truncate string with ellipsisslugify(str: string): string - Convert string to URL-friendly slugcamelToKebab(str: string): string - Convert camelCase to kebab-casekebabToCamel(str: string): string - Convert kebab-case to camelCasedeepClone<T>(obj: T): T - Deep clone an objectmergeDeep(target: object, ...sources: object[]): object - Deep merge objectspick<T, K extends keyof T>(obj: T, keys: K[]): Pick<T, K> - Pick properties from objectomit<T, K extends keyof T>(obj: T, keys: K[]): Omit<T, K> - Omit properties from objectisString(value: any): value is stringisNumber(value: any): value is numberisObject(value: any): value is objectisArray(value: any): value is any[]isFunction(value: any): value is FunctionisPromise(value: any): value is Promise<any>isEmail(value: string): booleanisUrl(value: string): booleanThis library supports all modern browsers and Node.js 14+. For older browsers, you may need to include polyfills for:
Array.prototype.flat (or use the provided flattenArray function)Object.entriesObject.valuesPromiseURL and URLSearchParamsThis project is licensed under the MIT License - see the LICENSE file for details.
If you find this library useful, please consider giving it a ⭐️ on GitHub.
See CHANGELOG.md for a list of changes in each version.