# typematch

> A small TypeScript library to do pattern matching

Latest version **0.5.2** (published 2015-11-27) · MIT license · 0 weekly downloads

## Install

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

## Health

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

Positive: has types; no vulnerabilities; high quality score.

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

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.5.2 |
| Published | 2015-11-27 |
| First published | 2015-11-26 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 0 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 13 |
| Author | Thales Mello |
| Maintainers | thalesmello |
| Keywords | typescript, pattern, matching |

## Links

- npm: https://www.npmjs.com/package/typematch
- Repository: https://github.com/thalesmello/typematch
- Homepage: https://github.com/thalesmello/typematch#readme
- Issues: https://github.com/thalesmello/typematch/issues
- npm.io page: https://npm.io/package/typematch

## Alternatives

- [@openai/codex-sdk](https://npm.io/package/@openai/codex-sdk.md) — 731.4K weekly downloads
- [babel-plugin-transform-react-jsx](https://npm.io/package/babel-plugin-transform-react-jsx.md) — 565.0K weekly downloads
- [babel-helper-remove-or-void](https://npm.io/package/babel-helper-remove-or-void.md) — 508.5K weekly downloads
- [@pnpm/store-controller-types](https://npm.io/package/@pnpm/store-controller-types.md) — 186.9K weekly downloads
- [react-native-signature-canvas](https://npm.io/package/react-native-signature-canvas.md) — 155.6K weekly downloads

## Recent versions

- 0.5.2 (latest) — 2015-11-27
- 0.5.1 — 2015-11-26
- 0.5.0 — 2015-11-26
- 0.4.0 — 2015-11-26
- 0.3.0 — 2015-11-26
- 0.2.0 — 2015-11-26
- 0.1.0 — 2015-11-26

## README

# TypeMatch
A tiny TypeScript library for pattern matching with focus on type safety.

## Introdution
Pattern matching is very popular in functional programming, and there are several libraries that try to bring it to JavaScript, such as [pun](https://github.com/CRogers/pun).

This library is an attempt to provide similar functionality with emphasis on type safety using TypeScript.

# How to use it

```typescript
import tm = require('typematch');

// Importing wildcard
var _ = tm._;

{
	let result = tm.match1(1,
		[0, () => 'no'],
		[1, () => 'it matches']);
	console.log(result); // it matches
}

{
	let result = tm.match2('two', 'arguments',
		['statically', 'checked', () => '=D'],
		[_, 'arguments', () => 'you can use wildcards'])
	console.log(result); // you can use wildcards
}

{
	let fibonacci = (n: number) => {
		return tm.match1(n,
			[0, () => 1],
			[_, () => n * fibonacci(n - 1)])
	}
	
	console.log(fibonacci(5)); // 120
}

{
	let fizzbuzz = (n: number) => {
		return tm.match2(n % 3, n % 5,
			[0, 0, () => 'fizzbuzz'],
			[0, _, () => 'fizz'],
			[_, 0, () => 'buzz']);
	}
	console.log(fizzbuzz(15)); // fizzbuzz
	console.log(fizzbuzz(14)); // undefined
}

{
	let result = tm.match4('static', 4, 'the', true,
		['dynamic', 4, 'the', false, () => 'javascript'],
		['static', 4, 'the', true, (pass, the, args, over) => pass + ' typescript']);
	console.log(result); // static typescript
}
```

## Type safety using JavaScript

You can use this library with JavaScript and have code completion with Visual Studio Code.

To do so, just include reference to the typings definition on the top of your code.

```
/// <reference path="node_modules/typematch/bin/src/bundle.d.ts" />
```

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