Comparing version 0.0.2 to 1.0.3
@@ -0,1 +1,3 @@ | ||
var jsesc = require('jsesc'); | ||
/** | ||
@@ -14,3 +16,3 @@ * @module regex-trie | ||
* | ||
* @class RegexTrie | ||
* @class RegexTrie | ||
* @constructor | ||
@@ -34,7 +36,7 @@ */ | ||
* an array before being added. Only alphanumeric values will be added. | ||
* Objects, booleans, arrays, etc will all be ignored (failed attempts to | ||
* Objects, booleans, arrays, etc will all be ignored (failed attempts to | ||
* add values are silent.) | ||
* | ||
* @method add() | ||
* @param phrase_to_add {array|string|number} | ||
* @method add() | ||
* @param phrase_to_add {array|string|number} | ||
* @chainable | ||
@@ -80,6 +82,14 @@ */ | ||
RegexTrie.prototype.regex = function (options) { | ||
RegexTrie.prototype.toRegExp = function () { | ||
if ( this._num_phrases_in_trie === 0 ) return; | ||
var result = this.toString(); | ||
return new RegExp(result); | ||
}; | ||
RegexTrie.prototype.toString = function () { | ||
if ( this._num_phrases_in_trie === 0 ) return; | ||
var _walk_trie = function (trie, this_arg) { | ||
@@ -101,3 +111,3 @@ | ||
walk_result = | ||
walk_result = | ||
this._quotemeta(key) + _walk_trie(trie[key], this_arg); | ||
@@ -117,3 +127,3 @@ | ||
var result = _walk_trie(this._trie, this); | ||
return new RegExp(result); | ||
return result; | ||
}; | ||
@@ -123,4 +133,4 @@ | ||
var group_has_one_element = function (el) { | ||
return el.length === 1; | ||
var group_has_one_element = function (el) { | ||
return el.length === 1; | ||
}, | ||
@@ -167,3 +177,3 @@ result = ""; | ||
if ( ! this._is_phrase_valid(phrase_to_fetch) && | ||
if ( ! this._is_phrase_valid(phrase_to_fetch) && | ||
this._num_phrases_in_trie > 0 ) { | ||
@@ -212,3 +222,5 @@ return false; | ||
return phrase.replace(/([^A-Za-z0-9_])/g, "\\$1"); | ||
return phrase | ||
.replace(/([\t\n\f\r\\\$\(\)\*\+\-\.\?\[\]\^\{\|\}])/g, '\\$1') | ||
.replace(/[^\x20-\x7E]/g, jsesc); | ||
}; | ||
@@ -215,0 +227,0 @@ |
{ | ||
"name": "regex-trie", | ||
"description": "Create a regular expression to match any of the phrases added to the trie (inspired by Dan Kogai's Regexp::Trie Perl module.)", | ||
"version": "0.0.2", | ||
"version": "1.0.3", | ||
"homepage": "https://github.com/alexeld/regex-trie", | ||
@@ -28,3 +28,8 @@ "keywords": [ | ||
], | ||
"dependencies": {}, | ||
"scripts": { | ||
"test": "mocha" | ||
}, | ||
"dependencies": { | ||
"jsesc": "^0.5.0" | ||
}, | ||
"devDependencies": { | ||
@@ -31,0 +36,0 @@ "mocha": "~1.17.1", |
@@ -13,3 +13,3 @@ # RegexTrie | ||
trie = new RegexTrie(), | ||
regex = trie.add('foo').add('bar').regex(); | ||
regex = trie.add('foo').add('bar').toRegExp(); | ||
``` | ||
@@ -24,3 +24,3 @@ | ||
trie = new RegexTrie(), | ||
regex = trie.add('foo').add('bar').regex(); | ||
regex = trie.add('foo').add('bar').toRegExp(); | ||
@@ -30,3 +30,3 @@ console.log(regex); | ||
3. Use [browserfy](https://github.com/substack/node-browserify) to create the | ||
the browser-safe package, e.g.: `browserfy app.js -o bundle.js`. | ||
the browser-safe package, e.g.: `browserify app.js -o bundle.js`. | ||
@@ -45,3 +45,3 @@ ### Usage | ||
// You can use an array to add phrases if you'd rather (duplicate | ||
// You can use an array to add phrases if you'd rather (duplicate | ||
// pharses are ignored.) | ||
@@ -51,3 +51,3 @@ trie.add(['foo', 'bar', 'baz']); | ||
// Fetch a RegExp to represent all the phrases in the trie | ||
var regex = trie.regex(); // regex => /(?:foo|ba[rz])/ | ||
var regex = trie.toRegExp(); // regex => /(?:foo|ba[rz])/ | ||
@@ -82,4 +82,3 @@ // What matches? | ||
All numbers (except `NaN`) are coerced in to their decimal equivalents before | ||
being added. | ||
All numbers (except `NaN`) are coerced into strings before being added. | ||
@@ -94,3 +93,3 @@ Before adding new phrases, the trie is checked to see whether or not that | ||
### `.regex()` | ||
### `.toRegExp()` | ||
@@ -102,3 +101,3 @@ Returns a `RegExp` instance which should match each individual phrase in the | ||
```javascript | ||
trie.add(['foo', '|', 'bar'].regex(); | ||
trie.add(['foo', '|', 'bar'].toRegExp(); | ||
// => (?:foo|\||bar) | ||
@@ -114,3 +113,3 @@ ``` | ||
```javascript | ||
var regex = trie.add(['foo', 'bar', 'car']).regex(); | ||
var regex = trie.add(['foo', 'bar', 'car']).toRegExp(); | ||
@@ -117,0 +116,0 @@ ['fool', 'afool', 'bart', 'abart', 'acar', 'acard'].forEach( function (word) { |
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
12925
168
0
1
157
+ Addedjsesc@^0.5.0
+ Addedjsesc@0.5.0(transitive)