npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

regex-trie

v1.0.4

Published

Create a regular expression to match any of the phrases added to the trie (inspired by Dan Kogai's Regexp::Trie Perl module.)

Downloads

365

Readme

RegexTrie

Create a regular expression to match any of the phrases added to the trie (inspired by Dan Kogai's Regexp::Trie Perl module.

Installation and Usage

Node

  1. npm install regex-trie
  2. require and use (see the Usage section for more comprehensive usage instructions.)
var RegexTrie = require('regex-trie'),
    trie      = new RegexTrie(),
    regex     = trie.add('foo').add('bar').toRegExp();

Browser

  1. npm install regex-trie
  2. create your application using RegexTrie:
// app.js
var RegexTrie = require('regex-trie'),
    trie      = new RegexTrie(),
    regex     = trie.add('foo').add('bar').toRegExp();

console.log(regex);
  1. Use browserfy to create the the browser-safe package, e.g.: browserify app.js -o bundle.js.

Usage

var RegexTrie = require('regex-trie');

// Create a new RegexTrie instance
var trie = new RegexTrie();

// Add phrases to the trie
trie.add('foo')
    .add('bar')
    .add('baz');

// You can use an array to add phrases if you'd rather (duplicate
// pharses are ignored.)
trie.add(['foo', 'bar', 'baz']);

// Fetch a RegExp to represent all the phrases in the trie
var regex = trie.toRegExp(); // regex => /(?:foo|ba[rz])/

// What matches?
var things_to_match = ['foo', 'bar', 'baz', 'bat', 'fun', 'food'],
    match_results   = things_to_match.map(regex.test, regex);

console.log(match_results);
// => [ true, true, true, false, false, true ]

Methods

RegexTrie() (constructor)

Creates a new instance of RegexTrie. Currently doesn't accept any options however this will likely change as the module evolves.

.add(phrase_to_add)

Adds a new phrase to the trie. Accepts singleton arguments, or an array of phrases. Ignores any values which aren't literals (objects, bools, arrays, etc).

    trie.add('foo')
        .add('bar')
        .add('baz')
        .add(['who', 'what', 'when', 'where'];

All numbers (except NaN) are coerced into strings before being added.

Before adding new phrases, the trie is checked to see whether or not that phrase already exists (using contains).

.contains(phrase)

Will check to see if the trie contains a phrase which matches phrase, and return true or false if the phrase does or does not exist.

.toRegExp()

Returns a RegExp instance which should match each individual phrase in the tree. The trie will escape any character that matches: /([^A-Za-z0-9_])/. For example, if the following values are added, the pipe (OR) will be escaped:

    trie.add(['foo', '|', 'bar'].toRegExp();
    // => (?:foo|\||bar)

Regex Specific Details

The RegExp returned by regex() is a non-capturing, un-anchored regular expression meaning it'll never capture its matches and all of the following phrases will still match:

    var regex = trie.add(['foo', 'bar', 'car']).toRegExp();

    ['fool', 'afool', 'bart', 'abart', 'acar', 'acard'].forEach( function (word) {
        console.log(regex.test(word));
    });
    // Output => true, true, true, true, true, true

Development

regex-trie uses Gulp as its build system. Currently gulpfile defines a few tasks:

  • lint -- JSHint (see .jshintrc for this project's settings)
  • test -- runs mocha from gulp
  • docs -- yuidocjs to produce development documentation
  • watch -- watches for changes to JS files in ./test/ and ./lib/ and runs the lint task
  • default -- by default the watch task runs (which runs lint)
  • continuous -- runs watch (which runs lint) and test on every JS file change.

Development Dependencies

Please see package.json for the latest development dependencies. At the time of writing, you'll need:

    "mocha": "~1.17.1"
    "should": "~3.1.2"
    "gulp-jshint": "~1.4.0"
    "gulp-util": "~2.2.14"
    "gulp": "~3.5.2"
    "gulp-watch": "~0.5.0"
    "blanket": "~1.1.6"
    "gulp-yuidoc": "~0.1.0"

Testing

The tests within regex-trie use mocha with should.js assertions. To test the module, just run mocha from your terminal.

TODO

List of things to add aren't in any specific order.

  1. Regex options to configure capturing and anchoring
  2. Cache compiled trie branches (to speed up RegExp generation)

License

See LICENSE.txt for license rights and limitations (MIT).