to-regex
Advanced tools
Comparing version 1.0.0 to 2.0.0
63
index.js
@@ -5,2 +5,3 @@ 'use strict'; | ||
var not = require('regex-not'); | ||
var MAX_LENGTH = 1024 * 64; | ||
@@ -22,16 +23,7 @@ /** | ||
module.exports = function(pattern, options) { | ||
if (!Array.isArray(pattern)) { | ||
return makeRe(pattern, options); | ||
module.exports = function(patterns, options) { | ||
if (!Array.isArray(patterns)) { | ||
return makeRe(patterns, options); | ||
} | ||
if (!options || (options && options.wrap !== false)) { | ||
for (var i = 0; i < pattern.length; i++) { | ||
var val = pattern[i]; | ||
if (val instanceof RegExp) val = val.source; | ||
pattern[i] = '(?:' + val + ')'; | ||
} | ||
} | ||
return makeRe(pattern.join('|'), options); | ||
return makeRe(patterns.join('|'), options); | ||
}; | ||
@@ -57,14 +49,16 @@ | ||
if (pattern.length > MAX_LENGTH) { | ||
throw new Error('expected pattern to be less than ' + MAX_LENGTH + ' characters'); | ||
} | ||
var key = pattern; | ||
// do this before shallow cloning options, it's a lot faster | ||
if (!options || (options && options.cache !== false)) { | ||
for (var prop in options) { | ||
key += '; ' + prop + '=' + String(options[prop]); | ||
key = createKey(pattern, options); | ||
if (cache.hasOwnProperty(key)) { | ||
return cache[key]; | ||
} | ||
} | ||
if ((!options || (options && options.cache !== false)) && cache.hasOwnProperty(key)) { | ||
return cache[key]; | ||
} | ||
var opts = extend({}, options); | ||
@@ -110,3 +104,5 @@ if (opts.contains === true) { | ||
cacheRegex(regex, key, pattern, opts); | ||
if (opts.cache !== false) { | ||
cacheRegex(regex, key, pattern, opts); | ||
} | ||
return regex; | ||
@@ -122,9 +118,24 @@ } | ||
function cacheRegex(regex, key, pattern, options) { | ||
if (options.cache !== false) { | ||
regex.cached = true; | ||
regex.pattern = pattern; | ||
regex.options = options; | ||
regex.key = key; | ||
cache[key] = regex; | ||
regex.cached = true; | ||
regex.pattern = pattern; | ||
regex.options = options; | ||
regex.key = key; | ||
cache[key] = regex; | ||
} | ||
/** | ||
* Create the key to use for memoization. The key is generated | ||
* by iterating over the options and concatenating key-value pairs | ||
* to the pattern string. | ||
*/ | ||
function createKey(pattern, options) { | ||
if (!options) return pattern; | ||
var key = pattern; | ||
for (var prop in options) { | ||
if (options.hasOwnProperty(prop)) { | ||
key += ';' + prop + '=' + String(options[prop]); | ||
} | ||
} | ||
return key; | ||
} | ||
@@ -131,0 +142,0 @@ |
{ | ||
"name": "to-regex", | ||
"description": "Generate a regex from a string or array of strings.", | ||
"version": "1.0.0", | ||
"version": "2.0.0", | ||
"homepage": "https://github.com/jonschlinkert/to-regex", | ||
@@ -6,0 +6,0 @@ "author": "Jon Schlinkert (https://github.com/jonschlinkert)", |
@@ -177,2 +177,2 @@ # to-regex [![NPM version](https://img.shields.io/npm/v/to-regex.svg?style=flat)](https://www.npmjs.com/package/to-regex) [![NPM downloads](https://img.shields.io/npm/dm/to-regex.svg?style=flat)](https://npmjs.org/package/to-regex) [![Build Status](https://img.shields.io/travis/jonschlinkert/to-regex.svg?style=flat)](https://travis-ci.org/jonschlinkert/to-regex) | ||
_This file was generated by [verb-generate-readme](https://github.com/verbose/verb-generate-readme), v0.1.31, on October 04, 2016._ | ||
_This file was generated by [verb-generate-readme](https://github.com/verbose/verb-generate-readme), v0.1.31, on October 06, 2016._ |
11583
119