# @toa.io/generic

> Toa Generic Tools

Latest version **0.24.0-alpha.0** (published 2023-11-16) · MIT license · 0 weekly downloads

## Install

```sh
npm install @toa.io/generic
pnpm add @toa.io/generic
yarn add @toa.io/generic
bun add @toa.io/generic
```

## Health

**Score 20/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities.

Warnings: low downloads; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.24.0-alpha.0 |
| Published | 2023-11-16 |
| First published | 2023-03-11 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Node | >= 18.0.0 |
| Dependencies | 1 |
| Unpacked size | 90.3 KB |
| Known vulnerabilities | 0 (+1 in 1 direct dependencies) |
| Install scripts | no |
| Author | Artem Gurtovoi |
| Maintainers | agurtovoi |

## Links

- npm: https://www.npmjs.com/package/@toa.io/generic
- Repository: https://github.com/toa-io/toa
- Homepage: https://toa.io
- Issues: https://github.com/toa-io/toa/issues
- npm.io page: https://npm.io/package/@toa.io/generic

## Dependencies (1)

- [uuid](https://npm.io/package/uuid.md) 9.0.0

## Recent versions

- 0.24.0-alpha.0 (latest) — 2023-11-16
- 1.0.0-alpha.311 (alpha) — 2026-09-17
- 0.10.1-alpha.56 (canary) — 2023-05-30
- 0.4.0-dev.9 (dev) — 2023-04-10
- 1.0.0-alpha.310 — 2026-09-16
- 1.0.0-alpha.304 — 2026-09-13
- 1.0.0-alpha.301 — 2026-09-11
- 1.0.0-alpha.292 — 2026-09-07
- 1.0.0-alpha.291 — 2026-09-07
- 1.0.0-alpha.289 — 2026-09-07
- 1.0.0-alpha.286 — 2026-09-06
- 1.0.0-alpha.283 — 2026-09-04
- 1.0.0-alpha.282 — 2026-09-03
- 1.0.0-alpha.278 — 2026-09-03
- 1.0.0-alpha.277 — 2026-09-03
- … 183 more at https://npm.io/package/@toa.io/generic/versions

## README

# Toa Generic Tools

Library is for internal use only. Barely documented. If you really need to understand something,
see [tests](source).

## Range

`range(input: string): number[]`

Transforms:

- `1-3` into `[1, 2, 3]`
- `1..3` into `[1, 2, 3]`
- `1-3, 5, 10..12` into `[1, 2, 3, 5, 10, 11, 12]`

See [tests](test/range.test.js).

## Shards

`shards(input: string): string[]`

Transforms `amqp://shard{0-2}.domain.com` into:

```javascript
[
  'amqp://shard0.domain.com',
  'amqp://shard1.domain.com',
  'amqp://shard2.domain.com'
]
```

Uses [`range`](#range).

See [tests](test/shards.test.js).

## Entries

`entries(object: object): [key, value][]`

`Object.entries` including Symbols.

## Generate

`generate(generate: function): object`

`generate` function's signature is `(segments: string[], value?: any): any | void`

`segments` are nested property names, used to access the generated object and optional `value` is
passed if property was assigned with a value.

### Example

```javascript
const generator = (segments, [key, value]) => {} // any property is an object
const object = generate(generator)

const _0 = object.a // will call callback with (['a'])
const _1 = object.a.b // (['a', 'b'])

object.a.b.c = 1 // (['a', 'b', 'c'], 1)
```

## Echo

`echo(input: string, variables?: Record<string, string>): string`

Returns the input string.
Substitutes variables to a placeholders following `${NAME}` syntax.
If no variables are passed, then environment variables are used.

### Example

#### Environment variables

```javascript
process.env['FOO'] = 'bar'

const output = echo('foo: ${FOO}')

console.log(output) // foo: bar
```

#### Custom variables

```javascript
const variables = { foo: 'world' }
const output = echo('hello ${foo}', variables)

console.log(output) // hello world
```

### Index substitutions

`echo(input: string, values: string[]): string`

When the second argument is an `Array`, its values are substituted to a placeholders following `{N}`
syntax.

```javascript
echo('make {0} not {1}', ['love', 'war'])
```

### Arguments substitution

When the second argument is a `string`, it and next arguments are substituted as
an [array](#index-substitutions).

```javascript
echo('make {0} not {1}', 'love', 'war')
```

## Map

`map(object: object, transformation: Function): object`

Traverse through a given plain object replacing its values (or key-value pairs) with a given
transformation function.
If the transformation function returns `undefined` then the current key-value pair will remain
unchanged.

Transformation function signature is:

`(value: any) => any | void`

or

`(key: string, value: any) => [string, any] | void`

## Plain

`plain(candidate: any): boolean`

Returns `true` if an argument is a POJO, `false` otherwise.

## Filter

`async filter(array: any[], test): any[]`

`test` function signature is `async (any) => boolean`

Async array filter.

## Pick

`pick(source: object, properties: string[]): object`

Constructs an object by picking the set of `properties` from `source` object.

## Trim

`trim (input: string): string`

Trim lines of multiline text.

## Buffer

Buffer a stream.

`buffer (stream: Readable): Buffer`

## Memo

Meoizes function returned value.

---
_Source: https://npm.io/package/@toa.io/generic · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
