# @nerdscomputing/js-pattern

> Tiny javascript pattern matching library

Latest version **0.0.1** (published 2021-10-26) · ISC license · 0 weekly downloads

## Install

```sh
npm install @nerdscomputing/js-pattern
pnpm add @nerdscomputing/js-pattern
yarn add @nerdscomputing/js-pattern
bun add @nerdscomputing/js-pattern
```

## Health

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

Positive: no vulnerabilities.

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

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.0.1 |
| Published | 2021-10-26 |
| First published | 2021-10-26 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 3.6 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | NerdsComputing |
| Maintainers | alexandrubeu, mihairadulescu, mihaimiuta |
| Keywords | pattern-matching |

## Links

- npm: https://www.npmjs.com/package/@nerdscomputing/js-pattern
- npm.io page: https://npm.io/package/@nerdscomputing/js-pattern

## Recent versions

- 0.0.1 (latest) — 2021-10-26

## README

# js-pattern
Tiny javascript library for pattern matching. Inspired by [ts-pattern](https://github.com/gvergnaud/ts-pattern).

## Dependencies
None.

## Usage
The library exposes a single function called `match`. 

Use the `.with` operator to add cases. `.with` receives two parameters:
* condition -> condition for the match, can be literal, an array of literals, a boolean or a lambda
* lambda -> function to run on match

The `.with` operator can be chained endlessly.

Use the `.like` operator for comparing object fields. It can be endlessly chained and combined with the `.with` operator.

```js
import match from 'js-pattern'

const matchResponse = response => match(response)
    .like({status: 200}, () => console.log('OK'))
    .like({status: 400}, () => console.log('Bad arguments'))
    .like({status: 404}, () => console.log('Not found'))
    .otherwise(() => console.log('Unknown error'))
```

Use the `.has` operator for checking for object fields. It can be endlessly chained and combined with the `.with` and `.like` operators.

```js
import match from 'js-pattern'

const person = {
    name: 'Mihai',
    job: 'Programmer'
}

const isEmployed = () => match(person)
    .has('job', () => console.log('Employed'))
    .otherwise(() => console.log('Unemployed'))
```

Use the `.otherwise` operator to add a default case. It takes a single parameter,
`lambda`, which is the function to be run upon match. Using the `.otherwise` operator will run the matching.

Use the `.execute` to run the actual matching.

```js
import match from 'js-pattern'

const sayHiToMihai = name => match(name)
    .with('Mihai Miuta', () => console.log('Hi, my creator'))
    .with(name === 'Mihai Radulescu', () => console.log('Hi, fellow Nerd Mihai'))
    .with(() => name === 'Other Mihai', () => console.log('Hi, fellow Mihai'))
    .otherwise(() => console.log(`Hi, person who's not Mihai`))
```

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