0.0.6 • Published 2 years ago

@garrettmk/ts-match v0.0.6

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

ts-match

ts-match is a versatile pattern-matching library, written in TypeScript. You can use it to easily filter or validate any kind of value using simple, declarative JSON. ts-match is small, fast and dependency-free!

Prerequisites

  • JavaScript ES6+
  • TypeScript 2.8+

Installation

npm install @garrettmk/ts-match

Usage

Import matches and/or withExpression, define an expression, and start matching (or filtering):

import { matches, withExpression } from 'ts-match';

const peopleWithCats = {
  pets: {
    any: {
      type: 'cat'
    }
  }
};

matches(person, peopleWithCats);                // true or false
people.filter(withExpression(peopleWithCats));  // An array of people with cats

Expressions

The simplest expression is an object with a single key-value pair, representing an operator and some value:

{ eq: 5 }

In this case, the expression tells matches to compare it's first argument with 5 using the === operator:

matches(5, { eq: 5 });  // true
matches(5, { eq: 3 });  // false

Expressions can be combined using the and and or operators:

// Matches numbers less than 10 and greater than 5
{ and: [
  { lt: 10 },
  { gt: 0 }
]}
// Equivalent: combine both operators into a single object
{
  lt: 10,
  gt: 0
}

// Matches numbers less than 10 or greater than 20
{ or: [
  { lt: 10 },
  { gt: 20 }
]}
// Equivalent: put both expressions in an array
[
  { lt: 10 },
  { gt: 20 }
]

Operator Reference

Keyr-valueDescription
eqanyEquals
neanyNot Equals
ltnumberLess than
ltenumberLess than or equal to
gtnumberGreater than
gtenumberGreater than or equal to
restringTest a string with RegExp.test()
lengthExpressionForMatch to an object's length property
includesExpressionForCheck an array's contents with Array.some()
anyExpressionSame as includes
everyExpressionCheck an array's contents with Array.every()
noneExpressionOpposite of every
keysExpressionFor<Array>Check an object's keys
valuesExpressionFor<Array>Check an object's values
andExpressionFor[]Match every expression
orExpressionFor[]Match any expression
escapedExpressionMapDo not interpret any key of the r-value as an operator

Examples

import { matches, withExpression } from 'ts-match';

const numbers = [0, 1, 2, 3, 4];
const strings = ['foo', 'bar', 'baz'];
const people = [
  {
    name: 'Alice',
    pets: [{
      name: 'Fido',
      type: 'dog'
    }]
  },
  {
    name: 'Bob',
    pets: [{
      name: 'Scratchy',
      type: 'cat'
    }]
  }
];

// Numbers less than 2
numbers.filter(withExpression({ lt: 2 }));              // [0, 1]
// Numbers less than 4 AND greater than 1
numbers.filter(withExpression({ lt: 4, gt: 1 }))        // [2, 3]
// Numbers less than 1 OR greater than 3
numbers.filter(withExpression([ { lt: 1 }, { gt: 3 }])) // [0, 4]

// Strings equal to 'foo'
strings.filter(withExpression('foo'));                  // ['foo']
// Strings matching a regex
strings.filter(withExpression({ re: /ba./i }));         // ['bar', 'baz']

// People with a cat for a pet
people.filter(withExpression({
  pets: {
    any: {
      type: 'cat'
    }
  }
}));                                                    // [{ Bob }]
// People with any pet
people.filter(withExpression({
  pets: {
    length: { gt: 0 }
  }
}));                                                    // [{ Alice }, { Bob }]

Contributing to ts-match

To contribute to ts-match, follow these steps:

  1. Fork this repository.
  2. Create a branch: git checkout -b <branch_name>.
  3. Make your changes and commit them: git commit -m '<commit_message>'
  4. Push to the original branch: git push origin <project_name>/<location>
  5. Create the pull request.

Alternatively see the GitHub documentation on creating a pull request.

Contact

If you want to contact me you can reach me at garrettmyrick@gmail.com.

License

This project uses the following license: MIT.