

GitHub - tc39/proposal-pattern-matching: Pattern matching syntax for ECMAScript
source link: https://github.com/tc39/proposal-pattern-matching
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

README.md
ECMAScript Pattern Matching
Status
Stage: 0
Author: Kat Marchán (npm, @maybekatz)
Champions: Brian Terlson (Microsoft, @bterlson), Sebastian Markbåge (Facebook, @sebmarkbage), Kat Marchán (npm, @maybekatz)
Introduction
This proposal adds a pattern matching expression to the language, based on the existing Destructuring Binding Patterns.
It's structured into multiple parts:
-
The core proposal for the
match
API, which is based directly on destructuring binding patterns. -
A proposal extending both
match
and regular destructuring withas
patterns, so patterns can both be matched and be assigned to identifiers. -
A proposal to add tagged collection literals, both as construction literals, and as their corresponding destructuring patterns.
-
A document including suggestions for other future proposals, which are dependent on
match
, but do not directly affect the main behavior of the feature.
This proposal draws heavily from corresponding features in Rust, F#, Scala, and Elixir.
Motivating Examples
Matching fetch()
responses:
const res = await fetch(jsonService) const val = match (res) { {status: 200, headers: {'Content-Length': s}} => `size is ${s}`, {status: 404} => 'JSON not found', {status} if (status >= 400) => throw new RequestError(res) }
Terser, more functional handling of Redux reducers. Compare with this same example in the Redux documentation:
function todoApp (state = initialState, action) { const newState = match (action) { {type: 'set-visibility-filter', filter: visFilter} => ({visFilter}), {type: 'add-todo', text} => ({ todos: [...state.todos, {text}] }), {type: 'toggle-todo', index} => ({ todos: state.todos.map((todo, idx) => idx === index ? {...todo, done: !todo.done} : todo ) }), {} => null // ignore unknown actions } return {...state, ...newState} }
Or mixed in with JSX code for quick props handling:
<Fetch url={API_URL}>{ props => match (props) { {loading} => <Loading />, {error} => <Error error={error} />, {data} => <Page data={data} /> } } </Fetch>
(via Divjot Singh)
General structural duck-typing on an API for vector-likes:
const getLength = vector => match (vector) { { x, y, z } => Math.sqrt(x ** 2 + y ** 2 + z ** 2), { x, y } => Math.sqrt(x ** 2 + y ** 2), [...etc] => vector.length } getLength({x: 1, y: 2, z: 3}) // 3.74165
Implementations
pattycake
(pure JS, no syntax sugar)- Babel Plugin
Recommend
-
179
ECMAScript, TC39, and the History of JavaScriptSeptember 28, 2017. 6 minute read.by Tyler McGinnis JavaScript is a living language t...
-
15
If you’re coming from a functional programming language such as Elixir , Haskell or Reas...
-
14
你很常聽到 ES6、ES7、ES2017 等這些名稱嗎?有聽過 ECMA 和 TC39 嗎?ECMAScript 和 JavaScript 又是什麼?規範要去哪裡看?下面會各別介紹。
-
2
JavaScript Records & Tuples Proposal Authors: Robin Ricard (Bloomberg) Rick Button (Bloomberg) Nicolò Ribaudo (Babel) Champions: Robin Ri...
-
9
This document has not been updated since the March 2022 Plenary Discussion of TC39. Details...
-
5
Ecma TC39 JavaScript Decimal proposal The TC39 Decimal proposal is an investigation into adding a built-in data type in JavaScript to represent base-10 decimal numbers. This whole proposal is basically a big open question, and we'd...
-
8
JS Module Blocks JS Module Blocks (“module blocks”) are an effort by Surma. It is the result of a lot of collaboration and prior art, most notably
-
6
Pipe Operator (|>) for JavaScript Stage: 2 Champions: J. S. Choi, James DiGioia, Ron Buckton, Tab Atkins-Bittner, [list incomplete] Former champions
-
5
Deferring Module Evaluation previously known as "Lazy Module Initialization" Status Champion(s): Yulia Startsev Author(s): Yulia Startsev and Guy Bedford Stage: 1
-
5
JavaScript Weekly Issue 675 « Prev ...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK