# rustmatchjs

> Rust match is cool. I wish it were in js

Latest version **0.0.4** (published 2022-08-10) · MIT license · 0 weekly downloads

## Install

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

## 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.4 |
| Published | 2022-08-10 |
| First published | 2022-08-10 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 4.5 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 1 |
| Author | ben armstrong |
| Maintainers | benarmstrong |
| Keywords | match, rust |

## Links

- npm: https://www.npmjs.com/package/rustmatchjs
- Repository: https://github.com/benarmstrongg/rustmatchjs
- npm.io page: https://npm.io/package/rustmatchjs

## Recent versions

- 0.0.4 (latest) — 2022-08-10
- 0.0.3 — 2022-08-10
- 0.0.2 — 2022-08-10
- 0.0.1 — 2022-08-10

## README

# rustmatchjs

![ts](https://badgen.net/badge/-/TypeScript?icon=typescript&label&labelColor=blue&color=555555)
![bundle size](https://img.badgesize.io/benarmstrongg/rustmatchjs/main/dist/index.js)
![bundle size](https://img.badgesize.io/benarmstrongg/rustmatchjs/main/dist/index.js?compression=gzip)

Rust `match` is cool. I wish it were in js.

This package provides a set of functions to replicate the Rust language's `match` expression. Unlike the `switch` statement in Javascript, `match` is an expression, which makes it perfect for conditionally assigning one value based on another in a readable way.

The first matching arm will be executed and its value returned. For `match.val` arms, a shallow comparison is performed to allow matching on objects. Thanks to some magic from Typescript, this can come kinda close to Rust `match`. The input type, `I`, will be inferred from the first argument and the output type, `O`, will be inferred from the type or return type of the first arm's `res` arg. Finally, a default arm *must* be included as the last argument. Since Typescript can't make sure your branches are exhaustive, this ensures some value is always returned.

## Usage:
```typescript
const message = match(response,
    match.val({ ...response, code: 200, error: null }, 'Fetched successfully!'),
    match.val({ ...response, code: 404 }, () => {
        console.log('Not found');
        return '404 :0';
    }),
    match.fn(response => response.code >= 500 && response.code < 600, () => {
        console.log(`Server error ${response.code}. Err: ${response.error}`);
        return 'Not your fault friend';
    }),
    match.default('I do not know how to handle this'),
    // alias for match.default
    match._(() => 'I do not know how to handle this')
);
```

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