# matchacho

> Pattern matching for JavaScript

Latest version **0.6.1** (published 2026-09-08) · MIT license · 0 weekly downloads

## Install

```sh
npm install matchacho
pnpm add matchacho
yarn add matchacho
bun add matchacho
```

## Health

**Score 65/100 (B)** — status: active.

Positive: has types; no vulnerabilities; recently updated; high maintenance score; high quality score.

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

## Facts

| | |
|---|---|
| Version | 0.6.1 |
| Published | 2026-09-08 |
| First published | 2023-11-04 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 87.3 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 2 |
| Author | temich |
| Maintainers | agurtovoi |
| Keywords | javascript, pattern matching |

## Links

- npm: https://www.npmjs.com/package/matchacho
- Repository: https://github.com/toa-io/match
- Homepage: https://github.com/toa-io/match#readme
- Issues: https://github.com/toa-io/match/issues
- npm.io page: https://npm.io/package/matchacho

## Recent versions

- 0.6.1 (latest) — 2026-09-08
- 0.6.0 — 2023-11-10
- 0.5.0 — 2023-11-10
- 0.4.1 — 2023-11-07
- 0.4.0 — 2023-11-05
- 0.3.5 — 2023-11-04

## README

# Pattern matching for JavaScript

Read [TC39 proposal](https://github.com/tc39/proposal-pattern-matching).

Supported patterns:

- primitive values
- primitive types
- classes
- regular expressions
- test functions
- arrays of patterns
- objects with any of the above

## Syntax

### Weird

The `match` function takes a value as the first argument,
followed by an even number of arguments, each pair of which is a pattern and a matching result.
Optionally, a default value may be provided as the last argument.

```
match(value,
  pattern, result,
  pattern, result,
  default)
```

The `result` and `default` arguments may be either a value or a function.

### Chaining

Alternately, a more conventional chaining syntax may be used:

```
match(value)
  .when(pattern, result)
  .when(pattern, result)
  .default(default)
```

`default()` must be placed last in the chain, event if no default value is provided.

Chaining syntax is more readable and provides better type inference.

Another option is to create an instance:

```
const cases = match()
  .when(pattern, result)
  .default(default)

cases.match(value)
```

## Usage

```shell
npm i matchacho
```

```javascript
import { match } from 'matchacho'
```

## Real-world examples

```javascript
return match(input,
  String, (item: string) => [item],
  Array, input,
  null, () => [])
```

Same as above with chaining syntax:

```javascript
return match(input)
  .when(String, (item) => [item])
  .when(Array, input)
  .when(null, () => [])
  .default()
```

```javascript
for (const directive of directives)
  mask |= match<number>(directive,
    'private', PRIVATE,
    'public', PUBLIC,
    'no-cache', NO_CACHE,
    0)
```

```javascript
return match(Class,
  Role, () => new Role(value, this.discovery.roles),
  Rule, () => new Rule(value, this.create.bind(this)),
  Incept, () => new Incept(value, this.discovery),
  () => new Class(value))
```

```javascript
throw match(error.code,
  'NOT_ACCEPTABLE', () => new UnsupportedMediaType(),
  'TYPE_MISMATCH', () => new BadRequest(),
  error)
```

```javascript
return match<Buffer>(type,
  'image/heic', async () => await convert({ buffer, format: 'JPEG' }),
  /image\/(?!jpeg$|png$|gif$)/, async () => await sharp(buffer).jpeg().toBuffer(),
  'image/gif', async () => await sharp(buffer).png().toBuffer(),
  buffer)
```

_Examples approved by [Ed](https://github.com/Gems)._

## Reference

```javascript
return match(value,
  // equals
  1, () => 'One!',

  // instanceof
  Readable, (stream) => stream.read(),
  Error, (error) => throw error,

  // test function
  positive, (number) => Math.sqrt(number),
  (x) => x < 0, (number) => Math.sqrt(-number),

  // regular expression
  /(\d+) \+ (\d+)/, (groups) => groups[0] + groups[1],
  /(?<left>\d+) \+ (?<right>\d+)/, (groups) => groups.left + groups.right,

  // object matching
  { statusCode: 200 }, (response) => response.data,

  // array matching
  [0, 1], (numbers) => `${numbers[0]} + ${numbers[1]}`,

  // nested test function
  { statusCode: (x) => x >= 200 && x < 300 }, (response) => response.data,

  // default
  (value) => value
)

function positive (value) {
  return value > 0
}
```

See [tests](source/match.test.ts).

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