百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
P

pattycake

> 编程语言
开源

用于模式匹配 API 的 playground

100 stars0 点赞2 次浏览
访问官网GitHub

工具介绍

用于模式匹配 API 的 playground

pattycake

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!

Install

$ npm install pattycake

Table of Contents

  • Example
  • 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 ...rests with array or object matchers, replace the ...rest with $.rest and destructure the array in the fat arrow body.
Example
javascript
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/, () => ...
)

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

JavaScript

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月18日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言