match-toy
v3.0.3
Published
The pattern matching library for javascript
Downloads
91
Maintainers
Readme
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:
Installation
From NPM
$ npm install match-toy --save
Or yarn:
$ yarn add match-toy
Then import/require the module.
const { match } = require('match-toy');
// or
import { match } from 'match-toy';
From CDN
Place the snippet into your html:
<script src="https://cdn.jsdelivr.net/npm/match-toy/dist/bundle/index.min.js"></script>
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/[email protected]/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.
var myFunc = matchToy.match
.case('1', () => 'one')
.end()
See more in examples.
Usage
Most basic usage:
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 immediately
one === 'one'; // true
See more about usage in depth.
Built with
Other nice projects and initiatives
Syntax proposals:
- https://github.com/tc39/proposal-pattern-matching
- https://github.com/eborden/JS-Pattern-Matching
- https://gist.github.com/bterlson/da8f02b95b484cd4f8d9
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
Contributing
- Improving or correcting the documentation.
- Translating.
- Finding bugs
- Sharing this project.
- PR are very welcome.
License
This project is licensed under the MIT License - see the LICENSE file for details.
Changelog
See CHANGELOG file for details.