Git Product home page Git Product logo

pattycake's Introduction

pattycake npm version license Travis AppVeyor Coverage Status

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

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
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/, () => ...
)

pattycake's People

Contributors

zkat avatar tabatkins avatar

Stargazers

aaa avatar Roman Hossain Shaon avatar monkeytao avatar  avatar Kyle Chamberlain avatar chunming, chen avatar Wayne Lai avatar Darcy Turk avatar Jakub Jirutka avatar Daniel André da Silva avatar Marcio Junior avatar  avatar Niels Heisterkamp avatar Pierre Beitz avatar Dima Semyushkin avatar Pedro Pablo Aste Kompen avatar Oskar Andersson avatar Nut Sornchumni avatar SunskyXH avatar Jason Morganson avatar  avatar Mehul Patel avatar Daniel Rubin avatar Eirik L. Vullum avatar Guilherme J. Tramontina avatar Kleber Correia avatar Josh Burgess avatar Fabian Beuke avatar Clay Dunston avatar Andrew Clancy avatar Christopher Pappas avatar Doğa Yüksel avatar Mark Feltner avatar Balaj Marius avatar Theo avatar Phil Helm avatar Abdelrahman Elsaidy avatar Brandon Smith avatar Josh Abernathy avatar Dotan J. Nahum avatar Sven Lito avatar Charly Poly avatar Andrey avatar Travis Arnold avatar Eduardo Rabelo avatar Léopold Szabatura avatar Kiwi avatar Cyril Schumacher avatar Geoff Whatley avatar Odi avatar Gilles De Mey avatar endziu avatar J. Felix Etcetera avatar Andreas Herd avatar Benjamin Diedrichsen avatar Iddan Aaronsohn avatar Karl Horky avatar Tim Arney avatar Géraud Henrion avatar Xavier Carpentier avatar Babak B. avatar Lay Flags avatar Thiago Santos avatar XXX YYY avatar Larry Eliemenye avatar WWWillems avatar Andrew Bradley avatar Daniel Gempesaw avatar George Lima avatar Vincent Chan avatar Zhang Zhi avatar Gabriel avatar Bohdan Horbatenko avatar Gavin Ray avatar Adhy Wiranata P avatar Brandon Orther avatar Rene Loperena avatar Davy Duperron avatar Artur Parkhisenko avatar Lucas avatar Daniel Sousa avatar Michele Bertoli avatar Patrick Lienau avatar Ilya Radchenko avatar Alasdair Smith avatar endetti avatar Colin avatar Giuseppe avatar Corentin Leruth avatar The web walker avatar Tom_Vandivier avatar Aaron Dancer 傅子威 avatar Kevin Swiber avatar Claire Neveu avatar Tomasz Cichocinski avatar Dalibor Gogic avatar andreGarvin avatar Yerko Palma avatar

Watchers

James Cloos avatar Guillaume FORTAINE avatar  avatar

Forkers

tabatkins iq-scm

pattycake's Issues

Variable assignment

Follows the precedent of every other match implementation I could find. This is about as universal as I think this gets? Unless I misread/misunderstood what another language is doing or missed an exception.

I think Swift requires let to bind inside its match statement (I could be wrong—I don't program in Swift—I've just seen a few code snippets here and there).

switch (15, "example", 3.14) {
    case (_, _, let pi): print ("pi: \(pi)")
}

I believe this is to allow both const and let style bindings within the case scope.

P.S. Not necessarily advocating for this, just thought of it when I was reading the README. It might be a worthwhile point to add that the spec assumes one of the two binding types.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.