genex.js

Genex module for JavaScript

MIT License

Downloads
5.5K
Stars
22
Committers
1

genex

Counts and expands any given regular expression into all the possible strings that it would be able to match.

Install

npm i genex --save

Usage

const genex = require('genex');
const pattern = genex(/(foo|bar|baz){1,2}|snafu/);

// 13
console.log(pattern.count());

/*
[
  'foo', 'foofoo', 'foobar', 'foobaz',
  'bar', 'barfoo', 'barbar', 'barbaz',
  'baz', 'bazfoo', 'bazbar', 'bazbaz',
  'snafu'
]
*/
console.log(pattern.generate());

The generate() method also accepts an optional callback:

pattern.generate((value) => {
  if (value.startsWith('foo') !== true) {
    return false; // breaks iteration
  }

  console.log(value); // 'foo', 'foofoo', 'foobar', 'foobaz',
});

License

MIT