3.0.3 • Published 5 years ago

match-toy v3.0.3

Weekly downloads
13
License
MIT
Repository
github
Last release
5 years ago

Match-Toy

Build Status Test Coverage Greenkeeper badge npm version Try match-toy on RunKit

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:

Try it now.

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/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.

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:

Other JavaScript libraries:

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.