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

re-tree

v0.1.7

Published

JavaScript Extended regular expression engine - client side, server side and 'angular side' ready.

Downloads

27,170

Readme

#re-tree ##JavaScript Extended regular expression engine - client side, server side and 'angular side' ready.

Build Status

Sometimes you get to a point where your regular expressions get so complex it's a nightmare to maintain. Sometimes it's just easier to split these expressions into smaller - simpler - expression and use flow logic to handle the complex cases. This is where re-tree comes and helps.

Consider the user-agent use case - you need to identify when you're in chrome... using the user-agent string.

  • User agent string for chrome might look like that: Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2049.0 Safari/537.36
  • But a Safari UA might look light that: Mozilla/5.0 (iPad; CPU OS 6_0 like Mac OS X) AppleWebKit/536.26 (KHTML, like Gecko) Version/6.0 Mobile/10A5355d Safari/8536.25
  • And an Opera UA might look light that: Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.90 Safari/537.36 OPR/29.0.1795.47

So you need a very complex RE expression to detect chrome and only chrome. Or you might choose to create a simple RE to detect chrome ('CH'), then a simple RE to detect Safari ('SF') and a simple RE to detect Opera ('OP') and use the following:

var inChrome=CH.test(UA) && !(SF.test(UA) || OP.test(UA));

which is simpler to read and maintain.

This would be the equivalent of using re-tree like that:

var reTree-require('re-tree');
var inChrome=reTree.test(UA,{and:[CH,{not:{or:[SF,OP]}}]});

Even simpler - right?!

Install

$ bower i re-tree -S

Server-Side Usage

var reTree=require('re-tree');

Client-Side Usage

Add script load to HTML:<script type="text/javascript" src=".../re-tree.js"></script>

To use from angular: Add module to your app dependencies: ...angular.module("...", [..."reTree"...])...

API

reTree has two functions:

test(string,exp) : Boolean : return true iff exp matches the string. exec(string,re | array_of_re) : Array : return the results of executing the first matching RE on the string.

The exec function will handle a single RE or an array of RE items. It will find the first matching RE and return the value of execution of the RE on the string.

The test function can test for a full re-tree expression. An re-expression can take one of the following forms:

  1. A string containing a correct RE string
  2. A RegExp instance
  3. An object { not: EXP } where EXP is an re-tree expression
  4. An object { or: [EXP1,EXP2,...] } where EXP1,EXP2... are re-tree expressions
  5. An object { and: [EXP1,EXP2,...] } where EXP1,EXP2... are re-tree expressions

Example:

// This matches Chrome but not Safari and Opera
var re = {
    and: [
        {
            or: [
                /\bChrome\b/,
                /\bCriOS\b/
            ]
        },
        {not: /\bOPR\b/}
    ]
};

Support

Pull-requests with new stuff will be highly appreciated :)

License

MIT License