JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 1080
  • Score
    100M100P100Q100834F
  • License Apache-2.0

Simple, powerful pattern matcher

Package Exports

  • minta

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (minta) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

Minta

Simple, effective, 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 modern 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: any, passthrough?: boolean): Function

The applied function takes an odd number of <case>, <callback(value)> pairs where the last callback is 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.

Real world examples

// clamp
const a = match(value) (
  value < min, _ => min,
  value < max, _ => value,
  _ => max
);
// fib
function fib(n) {
  return match(n) (
    0, x => 1,
    1, x => 1,
    n >= 2, x => fib(x-1) + fib(x-2),
    _ => n
  );
}
const a = match([1,2,3], true) (
  ['a','b','c'], _ => ['abc']
  [1,2,3],       _ => [123],
  [123],         _ => [5]
  _ => 0
); // [5]
class Example {
  do() {
    return 'thing';
  }
}
const example = new Example();
const action = match(example) (
  RegExp,  () => 'a regex',
  String,  () => 'a string',
  Example, (e) => e.do(),
  () => false
); // 'thing'

License

Apache 2.0