@tempots/std
Version:
Std library for TypeScript. Natural complement to the Tempo libraries.
145 lines (106 loc) • 3.82 kB
Markdown
for TypeScript that provides utility functions and types commonly used in web applications. This package serves as a natural complement to the Tempo libraries but can be used independently in any TypeScript project.
[](https://www.npmjs.com/package/@tempots/std)
[](https://github.com/fponticelli/tempots/blob/main/LICENSE)
```bash
npm install @tempots/std
yarn add @tempots/std
pnpm add @tempots/std
```
The library provides utility functions for working with:
```typescript
import { filterArray, mapArray, uniquePrimitives } from '@tempots/std'
// or
import { filterArray, mapArray, uniquePrimitives } from '@tempots/std/array'
// Filter an array
const numbers = [1, 2, 3, 4, 5]
const evenNumbers = filterArray(numbers, n => n % 2 === 0) // [2, 4]
// Map an array
const doubled = mapArray(numbers, n => n * 2) // [2, 4, 6, 8, 10]
// Get unique values
const withDuplicates = [1, 2, 2, 3, 3, 3]
const unique = uniquePrimitives(withDuplicates) // [1, 2, 3]
```
```typescript
import { capitalizeWords, ellipsis } from '@tempots/std'
// or
import { capitalizeWords, ellipsis } from '@tempots/std/string'
// Capitalize words
const capitalized = capitalizeWords('hello world') // 'Hello World'
// Truncate with ellipsis
const truncated = ellipsis('This is a very long text', 10) // 'This is a...'
```
```typescript
import { deferred } from '@tempots/std'
// or
import { deferred } from '@tempots/std/deferred'
// Create a deferred promise
const { promise, resolve, reject } = deferred<number>()
// Use the promise
promise.then(value => console.log(value))
// Resolve it later
setTimeout(() => resolve(42), 1000)
```
```typescript
import { delayed, interval } from '@tempots/std'
// or
import { delayed, interval } from '@tempots/std/timer'
// Delay execution
const cancelDelay = delayed(() => {
console.log('Executed after 1 second')
}, 1000)
// Set up an interval
const cancelInterval = interval(() => {
console.log('Executed every 2 seconds')
}, 2000)
// Cancel if needed
cancelDelay() // Cancel the delayed execution
cancelInterval() // Stop the interval
```
```typescript
import { Result, success, failure } from '@tempots/std'
// or
import { Result, success, failure } from '@tempots/std/result'
// Create success or failure results
const successResult = success(42)
const failureResult = failure('Something went wrong')
// Handle results
successResult.match({
success: value => console.log(`Success: ${value}`),
failure: error => console.error(`Error: ${error}`)
})
```
The library is organized into the following modules:
- `array` - Array manipulation utilities
- `async-result` - Asynchronous result handling
- `bigint` - BigInt utilities
- `boolean` - Boolean utilities
- `deferred` - Promise deferral utilities
- `domain` - Domain-specific types
- `equal` - Deep equality comparison
- `error` - Error handling utilities
- `function` - Function composition and manipulation
- `json` - JSON utilities
- `number` - Number utilities
- `object` - Object manipulation
- `promise` - Promise utilities
- `regexp` - Regular expression utilities
- `result` - Result type for error handling
- `string` - String manipulation utilities
- `timer` - Timing utilities
- `validation` - Data validation utilities
For comprehensive documentation, visit the [Tempo Documentation Site](https://tempo-ts.com/library/tempots-std.html).
This package is licensed under the Apache License 2.0.
A comprehensive standard library