https://github.com/zkat/pattycake
playground for pattern matching api
https://github.com/zkat/pattycake
Last synced: 7 months ago
JSON representation
playground for pattern matching api
- Host: GitHub
- URL: https://github.com/zkat/pattycake
- Owner: zkat
- License: other
- Created: 2018-03-22T01:19:45.000Z (almost 8 years ago)
- Default Branch: latest
- Last Pushed: 2018-03-29T07:50:25.000Z (over 7 years ago)
- Last Synced: 2025-04-28T17:08:02.062Z (8 months ago)
- Language: JavaScript
- Homepage: https://github.com/tc39/proposal-pattern-matching
- Size: 399 KB
- Stars: 99
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Contributing: CONTRIBUTING.md
- License: LICENSE.md
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
# pattycake [](https://npm.im/pattycake) [](https://npm.im/pattycake) [](https://travis-ci.org/zkat/pattycake) [](https://ci.appveyor.com/project/zkat/pattycake) [](https://coveralls.io/github/zkat/pattycake?branch=latest)
[`pattycake`](https://github.com/zkat/pattycake) is a little playground being
used to prototype concepts surrounding the [TC39 pattern matching
proposal](https://github.com/tc39/proposal-pattern-matching). It's not a spec,
it's not a standard, and it doesn't represent the actual look and feel of the JS
feature. But it'll help figure out what that could actually be!
## Install
`$ npm install pattycake`
## Table of Contents
* [Example](#example)
* [API](#api)
### Example
```javascript
import match, {$} from 'pattycake'
const res = await fetch(jsonService)
const val = match (res) (
{
status: 200,
headers: {'Content-Length': $}
}, ({
headers: {'Content-Length', s}}
) => `size is ${s}`,
{status: 404}, () => 'JSON not found',
$({status: $}, ({status}) => status >= 400), () => throw new RequestError(res)
)
```
### API
This documentation described the sugared version of the `match` expression. The
API exported by `pattycake` is similar, but uses functions and different syntax
for the same underlying concepts.
To convert a sugary `match` to a `pattycake` match:
1. Replace the main `{}` pair with `()`
2. Separate match clauses and bodies into matcher expressions and a fat arrow function, using the parameter list for the fat arrow for destructuring.
3. Replace any variable clauses in the match side with `match.$`.
4. If using guards, convert the guard to a function and pass it as the last argument to `match.$`. If you weren't already using `match.$` for a certain clause (because it wasn't necessary), wrap that clause with `match.$` and pass the guard function as the second argument.
5. If using `...rest`s with array or object matchers, replace the `...rest` with `$.rest` and destructure the array in the fat arrow body.
##### Example
```js
match (x) {
{a: 1, b} => ...,
[1, 2, ...etc] => ...,
1 => ...,
'string' => ...,
true => ...,
null => ...,
/regexhere/ => ...
}
// Converts to...
const $ = match.$
match (x) (
{a: 1, b: $}, ({b}) => ...,
[1, 2, $.rest], ([a, b, ...etc]) => ...,
1, () => ...,
'string', () => ...,
true, () => ...,
null, () => ...,
/regexhere/, () => ...
)
```