项目作者: jahan-addison

项目描述 :
functional pattern matcher
高级语言: TypeScript
项目地址: git://github.com/jahan-addison/minta.git
创建时间: 2017-04-16T01:02:36Z
项目社区:https://github.com/jahan-addison/minta

开源协议:Apache License 2.0

下载


Minta

Simple, effective, functional pattern matcher.

What is pattern matching?

Pattern matching is the act of checking a given sequence of tokens for the presence of the constituents of some pattern. In contrast to pattern recognition, the match usually has to be exact. The patterns have the form of either sequences or tree structures. Uses of pattern matching include outputting the locations (if any) of a pattern within a token sequence, to output some component of the matched pattern, and to substitute the matching pattern with some other token sequence (i.e., search and replace).

Details

Minta was inspired by the pattern matching systems in Rust, Haskell, and other functional languages.

To build the project, run npm run build.
To run the test suite, run npm test.

Minta provides a utility match function:

match(pattern: Pattern, passthrough?: boolean): (...cases: <Pattern | Callback>) => any

Pattern may be any primitive value (string, boolean, number, null, etc), an object (or constructor), an array (tuple), or a regular expression.

The applied function takes an odd number of ( Pattern case, callback(value) ) pairs, with the last callbackbeing the default case. The syntax fairly resembles rust’s pattern matching.

When passthrough is true, cases that match will apply on the transformed values, useful for building parsers.

How to use

Minta works without Typescript and may be installed with yarn or npm:

yarn add minta

or,

npm install --save minta.

Then you can import match:

  1. import { match } from 'minta';
  2. const data = match(someValue()) (
  3. 'the pattern', (e) => e + ' is this value',
  4. /another?/, _ => 'that value',
  5. otherwise => false
  6. );

Real world examples

  1. // clamp
  2. const a = match(value) (
  3. value < min, _ => min,
  4. value < max, _ => value,
  5. otherwise => max
  6. );
  1. // fibonacci
  2. function fib(n) {
  3. return match(n) (
  4. 0, x => 1,
  5. 1, x => 1,
  6. n >= 2, x => fib(x-1) + fib(x-2),
  7. otherwise => n
  8. );
  9. }
  1. // regex matching
  2. const type = match(fileType) (
  3. /\.js/, () => 'javascript',
  4. /\.scss/, () => 'sass',
  5. /\.json/, () => 'json',
  6. /\.yml/, () => 'yaml',
  7. otherwise => 'json'
  8. );
  1. // check for null-like values
  2. const check = match(isNull()) (
  3. undefined, _ => 'undefined',
  4. false, _ => 'false',
  5. null, _ => 'null',
  6. otherwise => 'default'
  7. ); // 'null'
  1. // passthrough (parsing)
  2. const a = match([1,2,3], true) (
  3. ['a','b','c'], _ => ['abc']
  4. [1,2,3], _ => [123],
  5. [123], _ => [5]
  6. otherwise => 0
  7. ); // [5]
  1. // class cases
  2. class Example {
  3. do() {
  4. return 'example thing';
  5. }
  6. }
  7. const example = new Example();
  8. const action = match(example) (
  9. RegExp, () => 'a regex',
  10. String, () => 'a string',
  11. Example, (e) => e.do(),
  12. otherwise => false
  13. ); // 'example thing'

License

Apache 2.0