@thalorlabs/utils
Version:
Utility functions for ThalorLabs projects
63 lines (50 loc) • 3.16 kB
Markdown
# @thalorlabs/utils
A TypeScript utility package for ThalorLabs projects.
## Installation
```bash
npm install @thalorlabs/utils
```
## Usage
### Import All Functions
```typescript
import {
capitalize,
extractNumber,
limitDecimals,
camelToSnake,
snakeToCamel,
truncate,
toTitleCase,
clamp,
randomInt,
isNumeric,
} from '@thalorlabs/utils';
```
### Import Individual Functions
```typescript
import { capitalize } from '@thalorlabs/utils/capitalize';
import { extractNumber } from '@thalorlabs/utils/extractNumber';
import { limitDecimals } from '@thalorlabs/utils/limitDecimals';
import { camelToSnake } from '@thalorlabs/utils/camelToSnake';
import { snakeToCamel } from '@thalorlabs/utils/snakeToCamel';
import { truncate } from '@thalorlabs/utils/truncate';
import { toTitleCase } from '@thalorlabs/utils/toTitleCase';
import { clamp } from '@thalorlabs/utils/clamp';
import { randomInt } from '@thalorlabs/utils/randomInt';
import { isNumeric } from '@thalorlabs/utils/isNumeric';
```
## Available Functions
| Function | Description | Example |
| ------------------------------------------------- | -------------------------------------------------------- | ---------------------------------------------- |
| `capitalize(str: string)` | Capitalizes the first character of a string | `capitalize('hello')` → `'Hello'` |
| `extractNumber(value: string \| number)` | Extracts numbers from currency strings | `extractNumber('$123.45')` → `123.45` |
| `limitDecimals(value: number, decimals?: number)` | Limits a number to specified decimal places (default: 2) | `limitDecimals(3.14159)` → `3.14` |
| `camelToSnake(str: string)` | Converts camelCase to snake_case | `camelToSnake('myVariable')` → `'my_variable'` |
| `snakeToCamel(str: string)` | Converts snake_case to camelCase | `snakeToCamel('my_variable')` → `'myVariable'` |
| `truncate(str: string, maxLength: number)` | Truncates string and adds ellipsis if too long | `truncate('Hello world', 8)` → `'Hello...'` |
| `toTitleCase(str: string)` | Converts string to title case | `toTitleCase('hello world')` → `'Hello World'` |
| `clamp(value: number, min: number, max: number)` | Ensures a number stays within bounds | `clamp(5, 0, 10)` → `5` |
| `randomInt(min: number, max: number)` | Generates a random integer in range | `randomInt(1, 10)` → `7` (random) |
| `isNumeric(value: unknown)` | Checks if a value can be interpreted as a number | `isNumeric('123')` → `true` |
## TypeScript Support
This package includes full TypeScript support with type definitions.