# match-when **Repository Path**: mirrors_FGRibreau/match-when ## Basic Information - **Project Name**: match-when - **Description**: :shell: Pattern matching for modern JavaScript - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2020-09-24 - **Last Updated**: 2026-05-17 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README ### match-when - Pattern matching for modern JavaScript [![Circle CI](https://img.shields.io/circleci/project/FGRibreau/match-when/master.svg?style=flat)](https://circleci.com/gh/FGRibreau/match-when/tree/master) [![Coverage Status](https://img.shields.io/coveralls/FGRibreau/match-when/master.svg)](https://coveralls.io/github/FGRibreau/match-when?branch=master) ![deps](https://img.shields.io/david/fgribreau/match-when.svg?style=flat) ![Version](https://img.shields.io/npm/v/match-when.svg?style=flat) ![extra](https://img.shields.io/badge/actively%20maintained-yes-ff69b4.svg?style=flat) [![Twitter Follow](https://img.shields.io/twitter/follow/fgribreau.svg?style=flat)](https://twitter.com/FGRibreau) [![Get help on Codementor](https://cdn.codementor.io/badges/get_help_github.svg)](https://www.codementor.io/francois-guillaume-ribreau?utm_source=github&utm_medium=button&utm_term=francois-guillaume-ribreau&utm_campaign=github) [![Slack](https://img.shields.io/badge/Slack-Join%20our%20tech%20community-17202A?logo=slack)](https://join.slack.com/t/fgribreau/shared_invite/zt-edpjwt2t-Zh39mDUMNQ0QOr9qOj~jrg) > Finally a **clear**, **succinct** and *safe* syntax to do Pattern Matching in modern JavaScript. [(backstory)](http://blog.fgribreau.com/2015/12/match-when-pattern-matching-for-modern.html) #### Shameless plug - [Open-Source Webhook as a Service](https://www.hook0.com/) - [Looking for a managed Keycloak IAM ?](https://www.cloud-iam.com/) - [**Charts, simple as a URL**. No more server-side rendering pain, 1 url = 1 chart](http://bit.ly/2dPZfbn) #### Usage The setup is pretty simple, simply require the library with `match` and `when` and you are ready to go! ```js const {match, when} = require('match-when'); ``` or globally ```js require('match-when/register'); // `match` and `when` are now globally available ``` Now let's see how we would write a factorial function: ```js const fact = match({ [when(0)]: 1, [when()]: (n) => n * fact(n-1) }); fact(10); // 3628800 ``` Clear and simple right? Alternatively, `match(, patternSpecification)` can be used to instantly perform a match: ```js function fact(n){ return match(n, { [when(0)]: 1, [when()]: (n) => n * fact(n-1) }); } fact(10); // 3628800 ```

Note that `when()` is a catch-all pattern and, if used, should always be the last condition. If you forget it `match()` will throw a `MissingCatchAllPattern` exception if nothing was matched. ##### Setup ``` npm i match-when -S ``` ##### High Order Functions `match` works well with high order functions like `map`, `filter` (and so on) too: ```js [2, 4, 1, 2].map(match({ [when(1)]: "one", [when(2)]: "two", [when()]: "many" })); // [ 'two', 'many', 'one', 'two' ] ``` ##### Arrays It also works with **arrays**: ```js function length(list){ return match({ [when([])]: 0, [when()]: ([head, ...tail]) => 1 + length(tail) })(list); } length([1, 1, 1]); // 3 ``` ##### OR Sadly JavaScript does not offer us a way to overload operators so we're stuck with `when.or`: ```js function parseArgument(arg){ return match({ [when.or("-h", "--help")]: () => displayHelp, [when.or("-v", "--version")]: () => displayVersion, [when()]: (whatever) => unknownArgument.bind(null, whatever) })(arg); } parseArgument(process.argv.slice(1)); // displayHelp || displayVersion || (binded)unknownArgument ``` ##### AND ```js const protocols = repositories.map(match({ [when.and({useGit:true}, {useSSH: true})]: 'git+ssh:', [when.and({useGit:true}, {useHTTP: true})]: 'git+http:', [when.and({useGit:true}, {useHTTPS: true})]: 'git+https:', [when()]: 'unsupported:' })) ``` ##### Regular Expressions match-when supports [regular expressions](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp) as well: ```js ['hey.com', 'fg@plop.com', 'fg+plop@plop.com', 'wat'].filter(match({ [when(/\S+@\S+\.\S+/)]: false, // **seems** to be a valid email (unsafe regex for doc purpose only) [when()]: true // the email could be invalid, return it })); // ['hey.com', 'wat'] ``` ##### Range ```js [12, 42, 99, 101].map(match({ [when.range(0, 41)]: '< answer', [when.range(43, 100)]: '> answer', [when(42)]: 'answer', [when()]: '< 0, or > 100' })); // ['< answer', 'answer', '> answer', '< 0, or > 100'] ``` ### Supported patterns: - `{ x1: pattern1, ..., xn: patternn }` - matches any object with property names `x1` to `xn` matching patterns `pattern1` to `patternn`, respectively. Only the own properties of the pattern are used. - `[pattern0, ..., patternn]` - matches any object with property names 0 to n matching patterns `pattern0` to `patternn`, respectively. - `/pattern/flags` - matches any values than pass the regular expression test - `when.range(low, high)` matches any number value in the range [low, high], `low` and `high` included. - `when.or(pattern0, ..., patternn)` - matches if at [least one](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/some) `pattern` matches. - `when.and(pattern0, ..., patternn)` - matches if [every](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/every) `pattern` matches. ### Todo: I will accept PR with their associated tests for the following features: - define and implement some syntax to support wildcards [todo-list inspired by pattern-match from dherman](https://github.com/dherman/pattern-match#patterns). \* *well, of course, they are not keywords but simple functions* #### Why/How? - the slides

capture d ecran 2016-03-17 23 35 44

#### Why/how? - the talk in **french (sorry)**

capture d ecran 2016-03-17 23 35 44

## Development sponsored by iAdvize

I work at [iAdvize](http://iadvize.com) as a Lead Developer and Architect. iAdvize is the **leading real-time customer engagement platform in Europe** and is used in 40 different countries. We are one of the french startup with the [fastest growth](http://www.iadvize.com/fr/wp-content/uploads/sites/2/2014/11/CP-Fast-50.pdf) and one of [the **greatest place to work** in **France**](https://vimeo.com/122438055). We are looking for a [**NodeJS backend developer**](http://smrtr.io/FqP79g), a [Scala backend developer](http://smrtr.io/FqP79g), a [**JavaScript frontend developer**](http://smrtr.io/wR-y4Q), a [Full-stack Developer](http://smrtr.io/SGhrew) and a [DevOps System Engineer](http://smrtr.io/OIFFMQ) in Paris or Nantes. **[Send me a tweet](https://twitter.com/FGRibreau) if you have any questions**! ## [The Story](http://blog.fgribreau.com/2015/12/match-when-pattern-matching-for-modern.html) ## [Changelog](/CHANGELOG.md)