
Security News
Deno 2.2 Improves Dependency Management and Expands Node.js Compatibility
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
JavaScript Extended regular expression engine - client side, server side and 'angular side' ready.
#re-tree ##JavaScript Extended regular expression engine - client side, server side and 'angular side' ready.
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.
Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2049.0 Safari/537.36
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
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?!
$ bower i re-tree -S
var reTree=require('re-tree');
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"...])...
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:
Example:
// This matches Chrome but not Safari and Opera
var re = {
and: [
{
or: [
/\bChrome\b/,
/\bCriOS\b/
]
},
{not: /\bOPR\b/}
]
};
Pull-requests with new stuff will be highly appreciated :)
0.1.7 (2018-02-23)
<a name="0.1.6"></a>
FAQs
JavaScript Extended regular expression engine - client side, server side and 'angular side' ready.
The npm package re-tree receives a total of 6,693 weekly downloads. As such, re-tree popularity was classified as popular.
We found that re-tree demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
Security News
React's CRA deprecation announcement sparked community criticism over framework recommendations, leading to quick updates acknowledging build tools like Vite as valid alternatives.
Security News
Ransomware payment rates hit an all-time low in 2024 as law enforcement crackdowns, stronger defenses, and shifting policies make attacks riskier and less profitable.