2.7.5 • Published 7 years ago

tailored v2.7.5

Weekly downloads
10
License
MIT
Repository
github
Last release
7 years ago

Tailored

A pattern matching library

This is the pattern matching library ported from elixirscript. It allows you to create functions that will perform pattern matching on the input and either execute the corresponding function or throw a tailored.MatchError.

const tailored = require('tailored');
const _ = tailored.wildcard();
const $ = tailored.parameter();

let fact = tailored.defmatch(
  tailored.clause([0], () => 1),
  tailored.clause([$], (n) => n * fact(n - 1))
);

let response = fact(0); //1
response = fact(10); //3628800

API

  • tailored.defmatch(...clauses): Function - Takes one or more tailored.Clause objects and returns a pattern match function. It cycles through the clauses and if a corresponding pattern matches, and the guard is true, then the matching parameters are passed to the corresponding function that will execute. If no matching clause is found, a tailored.MatchError is thrown.

  • tailored.clause(patterns: Array[any], fn: Function, guard: Function = () => true): tailored.Clause - A helper function for creating tailored.Clause objects. It takes an array of patterns, the function to execute if the pattern matches, and a guard function.

  • tailored.match(pattern: any, expression: any): [any] - Tries to match the pattern with the given expression
  • tailored.wildcard() - Returns a wildcard pattern. Matches on anything.

  • tailored.variable() - Returns a variable pattern. Matches on a value and uses it as a parameter for the clause functions

  • tailored.startsWith(prefix: String) - Returns a startsWith pattern. Matches on strings with the given string as a prefix

  • tailored.headTail() - Returns a headTail pattern. Matches arrays and returns both the head element and the tail elements as parameters

  • tailored.type(type: any, properties: Object = {}) - Returns a type pattern. Match on the type and it's properties for matching patterns.

  • tailored.capture(pattern: any) - Returns a capture pattern. Matches on it's patterns, and then returns the pattern as a parameter

Examples

  • Matches on anything, returning one

    var fn = tailored.defmatch(tailored.clause([_], function () {
      return 1;
    }));
    
    fn("ABC") // 1
  • Using a guard

      let fn = tailored.defmatch(
        tailored.clause([$], (number) => number, (number) => number > 0)
      );
    
      fn(0); //throws MatchError
      fn(3); //returns 3;
  • Match values in an object

    var fn = tailored.defmatch(
      tailored.clause([{ value: $ }], function (val) { return 1 + val; }),
      tailored.clause([{ a: { b: { c: $ } } }], function (val) { return 1 - val; })
    );
    
    fn({value: 20}) //21;
    fn({a: {b: {c: 20}, d: 10 } }) // 19

More examples can be found in the tests

2.7.5

7 years ago

2.7.4

7 years ago

2.7.2

7 years ago

2.7.1

7 years ago

2.7.0

7 years ago

2.6.4

7 years ago

2.6.3

7 years ago

2.6.2

7 years ago

2.6.1

7 years ago

2.6.0

7 years ago

2.5.0

7 years ago

2.4.9

7 years ago

2.4.8

7 years ago

2.4.7

7 years ago

2.4.6

7 years ago

2.4.5

7 years ago

2.4.4

7 years ago

2.4.3

7 years ago

2.4.2

7 years ago

2.4.1

7 years ago

2.4.0

7 years ago

2.3.2

7 years ago

2.3.1

7 years ago

2.3.0

7 years ago

2.2.0

7 years ago

2.0.0

8 years ago

1.0.2

8 years ago

1.0.1

8 years ago

1.0.0

8 years ago

0.0.4

10 years ago

0.0.3

10 years ago

0.0.2

10 years ago