playground for pattern matching api
pattycake is a little playground being
used to prototype concepts surrounding the TC39 pattern matching
proposal. 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!
$ npm install pattycake
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)
)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:
{} pair with ()match.$.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....rests with array or object matchers, replace the ...rest with $.rest and destructure the array in the fat arrow body.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/, () => ...
)No open issues yet, or sync has not completed.