The pattern matching library for javascript
npm install match-toy




> The pattern matching library for javascript.
Match-Toy is a pattern matching library for javascript with a powerful DSL and support for a wide range of patterns. The best kick off is read the tests, there are tons of them covering all the cases. For complete documentation, please check out the wiki. Another way is by examples:
- Tic-tac-toe
- Other examples
sh
$ npm install match-toy --save
`
Or yarn:
`sh
$ yarn add match-toy
`
Then import/require the module.
`javascript
const { match } = require('match-toy');
// or
import { match } from 'match-toy';
`#### From CDN
Place the snippet into your html:
`html
`
For specific version append the desired version (on the format @x.x.x) before the word match-toy just like this: https://cdn.jsdelivr.net/npm/match-toy@2.0.1/dist/bundle/index.min.js.This file is a bundle in the UMD format. In browser's environments, the module name is in camelcase and available on
window scope.
`javascript
var myFunc = matchToy.match
.case('1', () => 'one')
.end()
`See more in examples.
Usage
Most basic usage:
`javascript
import { match } from 'match-toy';// Create a new pattern matching function
const convertOneToString = match
.case('1', () => 'one')
.end();
convertOneToString(1); // return 'one'
convertOneToString(2); // return undefined
// Create another one, but now we only need
// the value returned by the match
const one = match
.case('1', () => 'one')
.return(1); // using
return() match runs immediatelyone === 'one'; // true
``Other JavaScript libraries:
- https://codemix.github.io/flow-runtime/#/docs/pattern-matching
- https://github.com/HerringtonDarkholme/Pat-Mat
- https://github.com/natefaubion/sparkler
- https://github.com/bramstein/funcy
- https://github.com/FGRibreau/match-when
- https://github.com/z-pattern-matching/z
- https://github.com/dherman/pattern-match
- https://github.com/mcollina/bloomrun