# functional-match-case

> A functional approach to using switch.

Latest version **1.0.3** (published 2018-07-26) · MIT license · 0 weekly downloads

## Install

```sh
npm install functional-match-case
pnpm add functional-match-case
yarn add functional-match-case
bun add functional-match-case
```

## Health

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

Positive: no vulnerabilities.

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

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.3 |
| Published | 2018-07-26 |
| First published | 2018-05-30 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 11.8 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 2 |
| Author | danicfilip.com |
| Maintainers | filipdanic |
| Keywords | helper, utility, one-liners |

## Links

- npm: https://www.npmjs.com/package/functional-match-case
- Repository: https://github.com/filipdanic/matchcase
- Issues: https://github.com/filipdanic/matchcase/issues
- npm.io page: https://npm.io/package/functional-match-case

## Dependencies (1)

- [execute-if-function](https://npm.io/package/execute-if-function.md) ^1.0.0

## Alternatives

- [lodash.assign](https://npm.io/package/lodash.assign.md) — 2.3M weekly downloads
- [lodash.chunk](https://npm.io/package/lodash.chunk.md) — 1.8M weekly downloads
- [react-native-ios-utilities](https://npm.io/package/react-native-ios-utilities.md) — 138.5K weekly downloads
- [@technically/lodash](https://npm.io/package/@technically/lodash.md) — 50.9K weekly downloads
- [@fluid-topics/ft-icon](https://npm.io/package/@fluid-topics/ft-icon.md) — 20.6K weekly downloads

## Recent versions

- 1.0.3 (latest) — 2018-07-26
- 1.0.2 — 2018-05-30
- 1.0.1 — 2018-05-30
- 1.0.0 — 2018-05-30

## README

# functional-match-case

Use lazy, functional programming friendly hash maps instead of `switch` statements.

## Getting Started

**Install**

```bash
npm install functional-match-case
# or
yarn add functional-match-case
```

**Use it!**
```javascript
import matchCase from 'functional-match-case';
const match = matchCase({
  cars: 1,
  trucks: 2,
  other: getOtherCode, // a function to call in case of “other” case
})(defaultValue); // defaultValue could be anything, even a ref to a function that should be called!
```

## Example:

Now for some more detailed examples and use cases:

Turn:
```javascript
switch(someValue) {
  case A:
  case B:
    return resultA;
  case C:
    return resultB;
  case D:
    return functionC();
  default:
    return defaultValue;
}
```

Into:

```javascript
import matchCase from 'functional-match-case';

const match = matchCase({
  [A]: resultA,
  [B]: resultA,
  [C]: resultB,
  [D]: functionC, // just the ref, will be called when needed
})(defaultValue);

// Then whenever needed:

match(someValue);
```

## Extra Benefits:

**Reusable cases. For example:**

```javascript
// fileA.js
export const someMatchCase = {
  [A]: resultA,
  [B]: resultB,
};
```

```javascript
// fileB.js
export const anotherMatchCase = {
  [C]: resultC,
  [D]: functionD,
}
```

```javascript
// fileC.js
export const yetAnotherMatchCase = {
   ...someMatchCase,
   ...anotherMatchCase,
   [F]: resultF,
};

```

**Lazy**

You could use a simple hash map instead of a `switch`. (Assuming no need for a `default` case.)

But, if you added functions like this:
```javascript
const hash = {
  keyA: functionA(),
  keyB: 401,
  keyC: functionC(),
  keyD: 200,
}
```

Then those functions would be executed and evaluated right away.

With `functional-match-case` you just pass a reference and it will be executed when needed.

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