to-regex
Advanced tools
Comparing version 0.1.1 to 1.0.0
66
index.js
@@ -22,16 +22,15 @@ 'use strict'; | ||
module.exports = function(pattern, options) { | ||
options = options || {}; | ||
if (Array.isArray(pattern)) { | ||
if (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 + ')'; | ||
} | ||
if (!Array.isArray(pattern)) { | ||
return makeRe(pattern, 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 + ')'; | ||
} | ||
pattern = pattern.join('|'); | ||
} | ||
return makeRe(pattern, options); | ||
return makeRe(pattern.join('|'), options); | ||
}; | ||
@@ -59,5 +58,5 @@ | ||
// do this before shallow cloning options, it's a lot faster | ||
if (options && options.cache !== false) { | ||
if (!options || (options && options.cache !== false)) { | ||
for (var prop in options) { | ||
key += ':' + prop + ':' + String(options[prop]); | ||
key += '; ' + prop + '=' + String(options[prop]); | ||
} | ||
@@ -86,4 +85,5 @@ } | ||
var close = opts.strictClose !== false ? '$' : ''; | ||
var flags = opts.flags || ''; | ||
var regex; | ||
var flags = opts.flags || ''; | ||
if (opts.nocase === true && !/i/.test(flags)) { | ||
@@ -98,15 +98,35 @@ flags += 'i'; | ||
var str = open + '(?:' + pattern + ')' + close; | ||
var re = new RegExp(str, flags); | ||
if (opts.cache !== false) { | ||
re.cached = true; | ||
cache[key] = re; | ||
regex = new RegExp(str, flags); | ||
} catch (err) { | ||
if (opts.strictErrors !== false) { | ||
err.key = key; | ||
err.pattern = pattern; | ||
err.originalOptions = options; | ||
err.createdOptions = opts; | ||
throw err; | ||
} | ||
return re; | ||
} catch (err) { | ||
if (opts.strictErrors !== false) throw err; | ||
return /.^/; //<= match nothing | ||
regex = /.^/; //<= match nothing | ||
} | ||
cacheRegex(regex, key, pattern, opts); | ||
return regex; | ||
} | ||
/** | ||
* Cache generated regex. This can result in dramatic speed improvements | ||
* and simplify debugging by adding options and pattern to the regex. It can be | ||
* disabled by passing setting `options.cache` to false. | ||
*/ | ||
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; | ||
} | ||
} | ||
/** | ||
* Expose `makeRe` | ||
@@ -113,0 +133,0 @@ */ |
{ | ||
"name": "to-regex", | ||
"description": "Generate a regex from a string or array of strings.", | ||
"version": "0.1.1", | ||
"version": "1.0.0", | ||
"homepage": "https://github.com/jonschlinkert/to-regex", | ||
@@ -42,11 +42,5 @@ "author": "Jon Schlinkert (https://github.com/jonschlinkert)", | ||
"verb": { | ||
"related": { | ||
"list": [ | ||
"to-regex-range", | ||
"is-glob", | ||
"has-glob", | ||
"path-regex" | ||
] | ||
"toc": { | ||
"method": "preWrite" | ||
}, | ||
"toc": false, | ||
"layout": "default", | ||
@@ -59,4 +53,9 @@ "tasks": [ | ||
], | ||
"lint": { | ||
"reflinks": true | ||
"related": { | ||
"list": [ | ||
"has-glob", | ||
"is-glob", | ||
"path-regex", | ||
"to-regex-range" | ||
] | ||
}, | ||
@@ -66,4 +65,7 @@ "reflinks": [ | ||
"verb-generate-readme" | ||
] | ||
], | ||
"lint": { | ||
"reflinks": true | ||
} | ||
} | ||
} |
110
README.md
@@ -5,2 +5,22 @@ # 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) | ||
## Table of Contents | ||
- [Install](#install) | ||
- [Usage](#usage) | ||
- [Options](#options) | ||
* [options.contains](#optionscontains) | ||
* [options.negate](#optionsnegate) | ||
* [options.nocase](#optionsnocase) | ||
* [options.flags](#optionsflags) | ||
* [options.cache](#optionscache) | ||
- [About](#about) | ||
* [Related projects](#related-projects) | ||
* [Contributing](#contributing) | ||
* [Building docs](#building-docs) | ||
* [Running tests](#running-tests) | ||
* [Author](#author) | ||
* [License](#license) | ||
_(TOC generated by [verb](https://github.com/verbose/verb) using [markdown-toc](https://github.com/jonschlinkert/markdown-toc))_ | ||
## Install | ||
@@ -19,18 +39,96 @@ | ||
console.log(toRegex('foo')) | ||
console.log(toRegex('foo')); | ||
//=> /^(?:foo)$/ | ||
console.log(toRegex('foo', {negate: true})) | ||
console.log(toRegex('foo', {negate: true})); | ||
//=> /^(?:(?:(?!^(?:foo)$).)*)$/ | ||
console.log(toRegex('foo', {contains: true})) | ||
console.log(toRegex('foo', {contains: true})); | ||
//=> /(?:foo)/ | ||
console.log(toRegex(['foo', 'bar'], {negate: true})) | ||
console.log(toRegex(['foo', 'bar'], {negate: true})); | ||
//=> /^(?:(?:(?!^(?:(?:foo)|(?:bar))$).)*)$/ | ||
console.log(toRegex(['foo', 'bar'], {negate: true, contains: true})) | ||
console.log(toRegex(['foo', 'bar'], {negate: true, contains: true})); | ||
//=> /^(?:(?:(?!(?:(?:foo)|(?:bar))).)*)$/ | ||
``` | ||
## Options | ||
### options.contains | ||
**Type**: `Boolean` | ||
**Default**: `undefined` | ||
Generate a regex that will match any string that _contains_ the given pattern. By default, regex is strict will only return true for exact matches. | ||
```js | ||
var toRegex = require('to-regex'); | ||
console.log(toRegex('foo', {contains: true})); | ||
//=> /(?:foo)/ | ||
``` | ||
### options.negate | ||
**Type**: `Boolean` | ||
**Default**: `undefined` | ||
Create a regex that will match everything except the given pattern. | ||
```js | ||
var toRegex = require('to-regex'); | ||
console.log(toRegex('foo', {negate: true})); | ||
//=> /^(?:(?:(?!^(?:foo)$).)*)$/ | ||
``` | ||
### options.nocase | ||
**Type**: `Boolean` | ||
**Default**: `undefined` | ||
Adds the `i` flag, to enable case-insensitive matching. | ||
```js | ||
var toRegex = require('to-regex'); | ||
console.log(toRegex('foo', {nocase: true})); | ||
//=> /^(?:foo)$/i | ||
``` | ||
Alternatively you can pass the flags you want directly on [options.flags](#options.flags). | ||
### options.flags | ||
**Type**: `String` | ||
**Default**: `undefined` | ||
Define the flags you want to use on the generated regex. | ||
```js | ||
var toRegex = require('to-regex'); | ||
console.log(toRegex('foo', {flags: 'gm'})); | ||
//=> /^(?:foo)$/gm | ||
console.log(toRegex('foo', {flags: 'gmi', nocase: true})); //<= handles redundancy | ||
//=> /^(?:foo)$/gmi | ||
``` | ||
### options.cache | ||
**Type**: `Boolean` | ||
**Default**: `true` | ||
Generated regex is cached based on the provided string and options. As a result, runtime compilation only happens once per pattern (as long as options are also the same), which can result in dramatic speed improvements. | ||
This also helps with debugging, since adding options and pattern are added to the generated regex. | ||
**Disable caching** | ||
```js | ||
toRegex('foo', {cache: false}); | ||
``` | ||
## About | ||
@@ -81,2 +179,2 @@ | ||
_This file was generated by [verb-generate-readme](https://github.com/verbose/verb-generate-readme), v0.1.31, on September 26, 2016._ | ||
_This file was generated by [verb-generate-readme](https://github.com/verbose/verb-generate-readme), v0.1.31, on October 04, 2016._ |
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
11360
109
0
177