All Projects → pmros → pamatcher

pmros / pamatcher

Licence: MIT license
A pattern matching library for JavaScript iterators

Programming Languages

livescript
113 projects

Projects that are alternatives of or similar to pamatcher

Micromatch
Contributing Pull requests and stars are always welcome. For bugs and feature requests, please create an issue. Please read the contributing guide for advice on opening issues, pull requests, and coding standards.
Stars: ✭ 1,979 (+8504.35%)
Mutual labels:  regex, regular-expression, matcher
Regex
🔤 Swifty regular expressions
Stars: ✭ 311 (+1252.17%)
Mutual labels:  regex, regular-expression
cheat-sheet-pdf
📜 A Cheat-Sheet Collection from the WWW
Stars: ✭ 728 (+3065.22%)
Mutual labels:  regex, regular-expression
es6-template-regex
Regular expression for matching es6 template delimiters in a string.
Stars: ✭ 15 (-34.78%)
Mutual labels:  regex, regular-expression
RegexReplacer
A flexible tool to make complex replacements with regular expression
Stars: ✭ 38 (+65.22%)
Mutual labels:  regex, regular-expression
regex
Regular expressions for Prolog
Stars: ✭ 16 (-30.43%)
Mutual labels:  regex, regular-expression
termco
Regular Expression Counts of Terms and Substrings
Stars: ✭ 24 (+4.35%)
Mutual labels:  regex, regular-expression
compiler-design-lab
These are my programs for compiler design lab work in my sixth semester
Stars: ✭ 47 (+104.35%)
Mutual labels:  regex, regular-expression
CVparser
CVparser is software for parsing or extracting data out of CV/resumes.
Stars: ✭ 28 (+21.74%)
Mutual labels:  regex, regular-expression
dregex
Dregex is a JVM library that implements a regular expression engine using deterministic finite automata (DFA). It supports some Perl-style features and yet retains linear matching time, and also offers set operations.
Stars: ✭ 37 (+60.87%)
Mutual labels:  regex, regular-expression
RgxGen
Regex: generate matching and non matching strings based on regex pattern.
Stars: ✭ 45 (+95.65%)
Mutual labels:  regex, regular-expression
doi-regex
Regular expression for matching DOIs
Stars: ✭ 28 (+21.74%)
Mutual labels:  regex, regular-expression
pcre-heavy
A Haskell regular expressions library that doesn't suck | now on https://codeberg.org/valpackett/pcre-heavy
Stars: ✭ 52 (+126.09%)
Mutual labels:  regex, regular-expression
cregex
A small implementation of regular expression matching engine in C
Stars: ✭ 72 (+213.04%)
Mutual labels:  regex, regular-expression
regex-not
Create a javascript regular expression for matching everything except for the given string.
Stars: ✭ 31 (+34.78%)
Mutual labels:  regex, regular-expression
regexp-expand
Show the ELisp regular expression at point in rx form.
Stars: ✭ 18 (-21.74%)
Mutual labels:  regex, regular-expression
expand-brackets
Expand POSIX bracket expressions (character classes) in glob patterns.
Stars: ✭ 26 (+13.04%)
Mutual labels:  regex, regular-expression
learn-regex
Learn regex the easy way
Stars: ✭ 43,660 (+189726.09%)
Mutual labels:  regex, regular-expression
Regaxor
A regular expression fuzzer.
Stars: ✭ 35 (+52.17%)
Mutual labels:  regex, regular-expression
LLRegex
Regular expression library in Swift, wrapping NSRegularExpression.
Stars: ✭ 18 (-21.74%)
Mutual labels:  regex, regular-expression

pamatcher

npm version Build Status

A pattern matching library for JavaScript iterators.

pamatcher is a JavaScript library that generalizes the notion of regular expressions to any sequence of items of any type. Instead strings, you can use any iterable or iterator as input. Instead of characters you can use any predicate as item matcher. So you can do pattern matching in a general and declarative way.

Installation and usage

You can install pamatcher using npm:

npm install pamatcher

This is an example of use:

var pamatcher = require('pamatcher');

var input = [1, 4, 8, 44, 55];

var matcher = pamatcher(
  (i) => i < 10,
  { repeat: (i) => i%2==0, name: 'mycatch' },
  (i) => i > 10
);

var result = matcher.exec(input);
if(result.test) {
  console.log("Pattern matches!");
  console.log("Captured values: " + result.captures['mycatch'].join(','));
} else {
  console.log("Pattern doesn't match.");
}

In the example, the pattern is simple: match a number lesser than 10, followed by zero or more even numbers and finally a number greater than 10. You test an array and it should print "Pattern matches!". See tests for more examples.

If you want to use pamatcher from browser, you can use jspm. Here you have a complete pamatcher demo for browser at gist (or live at bl.ocks.org). You can play online with pamatcher via jsfiddle.

API

pamatcher(expression)

This is a function that transforms a pattern expression into a matcher. This is the only thing you need to import/require to use pamatcher library.

A pattern expression is a JavaScript object that specify the pattern you want to use. A pattern expression can be:

[function]

A predicate, that is a function that takes an input item, evaluates it and return a boolean. True means "item accepted".

{ value: [whatever], name: [string] }

This is a shortcut for a (deep) equality predicate.

{ sequence: [array of expressions], name: [string] }

A sequence of expressions. It's something like this regex: /abc/ Usually pamatcher can convert arrays of expressions to a sequence expression for a better readability. Also pamatcher function can automatically convert any number of arguments to a sequence expression (see example above).

{ or: [array of expressions], name: [string] }

Logical or of multiple expressions. It's something like this regex: /(a|b|c)/

{ optional: [expression], name: [string] }

An optional expression. It's something like this regex: /a?/

{ repeat: [expression], name: [string] }

A sequence of zero or more expressions repeated. It's something like this regex: /a*/

{ repeat: [expression], min: [int], max: [int], name: [string] }

A sequence from two up to five expressions repeated. It's something like this regex: /a{2,5}/

matcher object

A matcher object can check if your expression matches to an input.

matcher.test(input)

The input is an iterator or an iterable. These are ES6 features. Array, String, Map, Set are iterables.

test method returns true if your pattern expression matchs your input, otherwise it returns false.

matcher.match(input)

matcher method returns an object that contains captured values of each named group

matcher.exec(input)

exec method returns an object that contains test boolean value and captured values of each named group

TODO

  • Pattern expressions.
  • Browser suport.
  • Cardinality for repeat pattern.
  • Capturing groups.
  • Simple expressions using strings like traditional regular expressions.
  • Error handling.
  • More information avaible for predicates (index, previous item...).
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].