Skip to content

alixaxel/genex.js

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

17 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

genex

genex Donate

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