Various TypeScript/JavaScripts utils functions for node and browser
npm install @vsokolov/utils



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.
- Array Utilities: Flatten, sort, filter, and manipulate arrays with ease
- Date/Time Handling: Comprehensive date parsing, formatting, and manipulation
- String Operations: Common string manipulation and validation functions
- Type Checking: Type-safe type checking utilities
- Object Manipulation: Deep cloning, merging, and property manipulation
- Browser & Node.js Support: Works in both environments
- Fully Typed: Written in TypeScript with full type definitions
- Tree-shakeable: Only include what you use in your bundle
Using npm:
``bash`
npm install @vsokolov/utils
Using yarn:
`bash`
yarn add @vsokolov/utils
Using pnpm:
`bash`
pnpm add @vsokolov/utils
You can import the entire library:
`typescript`
import * as utils from '@vsokolov/utils';
Or import individual functions for better tree-shaking:
`typescript`
import { flattenArray, formatDate, isString } from '@vsokolov/utils';
#### Array Utilities
`typescript
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
`
#### Date Utilities
`typescript
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', ...]
`
#### String Utilities
`typescript
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 - Flatten nested arraysunique
- - Get unique values from arraysortBy(arr: Record
- - Sort array of objects by keyremoveItem
- - Remove items from arrayrandomItem
- - Get random items from arrayintersection
- - Get intersection of two arrayscountBy(array: Array
- - Count occurrences of each value
- formatDate(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 namestimeStamptToDate(timestamp: string | number): string
- - Convert timestamp to date stringdateTimeToCron(date: Date): string
- - Convert Date to cron syntaxcronToDateTime(cronSyntax: string): Date
- - Convert cron syntax to Date
- truncate(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 camelCase
- deepClone - Deep clone an objectmergeDeep(target: object, ...sources: object[]): object
- - Deep merge objectspick
- - Pick properties from objectomit
- - Omit properties from object
- isString(value: any): value is stringisNumber(value: any): value is number
- isObject(value: any): value is object
- isArray(value: any): value is any[]
- isFunction(value: any): value is Function
- isPromise(value: any): value is Promise
- isEmail(value: string): boolean
- isUrl(value: string): boolean
-
This 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.entries
- Object.values
- Promise
- URL
- and URLSearchParams`
This 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.