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

tabatkins avatar zkat avatar

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.