globrex
Advanced tools
Comparing version 0.0.2 to 0.0.3
{ | ||
"name": "globrex", | ||
"version": "0.0.2", | ||
"version": "0.0.3", | ||
"description": "Glob to regex", | ||
@@ -5,0 +5,0 @@ "main": "src/index.js", |
@@ -24,3 +24,3 @@ <div align="center"> | ||
## Installation | ||
## Install | ||
@@ -44,6 +44,6 @@ ``` | ||
const { regex } = globrex('p*uck'); | ||
regex.test('pot luck'); // true | ||
regex.test('pluck'); // true | ||
regex.test('puck'); // true | ||
const result = globrex('p*uck') | ||
// => { regex: /^p.*uck$/, string: '^p.*uck$', segments: [ /^p.*uck$/ ] } | ||
result.regex.test('pluck'); // true | ||
``` | ||
@@ -54,3 +54,3 @@ | ||
## globrex(glob, opts) | ||
### globrex(glob, options) | ||
@@ -60,6 +60,6 @@ Type: `function`<br> | ||
Transform glob strings into regex. | ||
Transform globs intp regular expressions. | ||
Returns object with the following properties: | ||
#### obj.regex | ||
#### regex | ||
@@ -73,3 +73,3 @@ Type: `RegExp` | ||
#### obj.string | ||
#### string | ||
@@ -80,7 +80,8 @@ Type: `String` | ||
#### obj.segments | ||
#### segments | ||
Type: `Array` | ||
Array of `RegExp` instances seperated by `/`. This can be usable when working with paths or urls. | ||
Array of `RegExp` instances seperated by `/`. | ||
This can be usable when working with paths or urls. | ||
@@ -102,12 +103,4 @@ Example array could be: | ||
#### opts | ||
### options.extended | ||
Type: `Object`<br> | ||
Default: `{ extended: false, globstar: false, strict: false, flags: '' }` | ||
Configuration object, that enables/disable different features. | ||
#### opts.extended | ||
Type: `Boolean`<br> | ||
@@ -123,3 +116,3 @@ Default: `false` | ||
#### opts.globstar | ||
### options.globstar | ||
@@ -129,16 +122,12 @@ Type: `Boolean`<br> | ||
When globstar is `false` the `'/foo/*'` is translated a regexp like | ||
When globstar is `false` globs like `'/foo/*'` are transformed to the following | ||
`'^\/foo\/.*$'` which will match any string beginning with `'/foo/'`. | ||
When globstar is `true`, `'/foo/*'` is translated to regexp like | ||
`'^\/foo\/[^/]*$'` which will match any string beginning with `'/foo/'` BUT | ||
which **does not have** a `'/'` to the right of it. | ||
When the globstar option is `true`, the same `'/foo/*'` glob is transformed to | ||
`'^\/foo\/[^/]*$'` which will match any string beginning with `'/foo/'` that **does not have** a `'/'` to the right of it. `'/foo/*'` will match: `'/foo/bar'`, `'/foo/bar.txt'` but not `'/foo/bar/baz'` or `'/foo/bar/baz.txt'`. | ||
E.g. with `'/foo/*'` these will match: `'/foo/bar'`, `'/foo/bar.txt'` but | ||
these will not `'/foo/bar/baz'`, `'/foo/bar/baz.txt'` | ||
> **Note**: When globstar is `true`, `'/foo/**'` is equivelant to `'/foo/*'` when globstar is `false`. | ||
#### opts.strict | ||
### options.strict | ||
@@ -148,8 +137,6 @@ Type: `Boolean`<br> | ||
Don't be so strict. | ||
Be forgiving about `///` and make everything after the first `/` optional. | ||
This is how bash-globbing works. | ||
Be forgiving about mutiple slashes, like `///` and make everything after the first `/` optional. This is how bash glob works. | ||
#### opts.flags | ||
### options.flags | ||
@@ -162,5 +149,13 @@ Type: `String`<br> | ||
### options.windows | ||
Type: `Boolean`<br> | ||
Default: `System OS` | ||
Split segment as a windows path, otherwise splut as unix. Defaults to the OS running the package. | ||
## References | ||
Learn more about globbing here: | ||
Learn more about advanced globbing here | ||
- [mywiki.wooledge.org/glob](http://mywiki.wooledge.org/glob) | ||
@@ -167,0 +162,0 @@ - [linuxjournal](http://www.linuxjournal.com/content/bash-extended-globbing) |
@@ -1,5 +0,3 @@ | ||
'use strict'; | ||
const isWin = require('os').platform() === 'win32'; | ||
module.exports = globrex; | ||
/** | ||
@@ -13,5 +11,6 @@ * Convert any glob pattern to a JavaScript Regexp object | ||
* @param {String} [opts.flags=''] RegExp globs | ||
* @param {String} [opts.windows] Set to true to split path as a windows path. Defaults to OS. | ||
* @returns {Object} converted object with string, segments and RegExp object | ||
*/ | ||
function globrex(glob, { extended = false, globstar = false, strict = false, flags = ''} = {}) { | ||
function globrex(glob, { extended = false, globstar = false, strict = false, flags = '', windows = isWin} = {}) { | ||
let reStr = ''; | ||
@@ -28,6 +27,6 @@ | ||
if (addLastPart) segment += str; | ||
if (!flags || !~flags.indexOf('g')) { | ||
segment = `^${segment}$`; | ||
if (segment !== '') { | ||
if (!flags || !~flags.indexOf('g')) segment = `^${segment}$`; | ||
segments.push(new RegExp(segment, flags)); | ||
} | ||
segments.push(new RegExp(segment, flags)); | ||
segment = ''; | ||
@@ -52,5 +51,3 @@ } else { | ||
n = glob[i + 1] || null; | ||
switch (c) { | ||
case '\\': | ||
case '$': | ||
@@ -62,8 +59,10 @@ case '^': | ||
break; | ||
case '\\': | ||
add('\\' + c, windows); | ||
if (n === '\\' && !strict) reStr += '?'; | ||
break; | ||
case '/': | ||
add('\\' + c, true); | ||
if (n === '/' && !strict) reStr += '?'; // this is not relevant for segments | ||
add('\\' + c, !windows); | ||
if (n === '/' && !strict) reStr += '?'; | ||
break; | ||
case '(': | ||
@@ -76,3 +75,2 @@ if (ext.length) { | ||
break; | ||
case ')': | ||
@@ -93,3 +91,2 @@ if (ext.length) { | ||
break; | ||
case '|': | ||
@@ -102,3 +99,2 @@ if (ext.length) { | ||
break; | ||
case '+': | ||
@@ -111,3 +107,2 @@ if (n === '(' && extended) { | ||
break; | ||
case '@': | ||
@@ -118,3 +113,2 @@ if (n === '(' && extended) { | ||
} | ||
case '!': | ||
@@ -135,3 +129,2 @@ if (extended) { | ||
} | ||
case '?': | ||
@@ -146,3 +139,2 @@ if (extended) { | ||
} | ||
case '[': | ||
@@ -164,3 +156,2 @@ if (inRange && n === ':') { | ||
} | ||
case ']': | ||
@@ -172,3 +163,2 @@ if (extended) { | ||
} | ||
case '{': | ||
@@ -180,3 +170,2 @@ if (extended) { | ||
} | ||
case '}': | ||
@@ -188,3 +177,2 @@ if (extended) { | ||
} | ||
case ',': | ||
@@ -197,3 +185,2 @@ if (inGroup) { | ||
break; | ||
case '*': | ||
@@ -204,3 +191,2 @@ if (n === '(' && extended) { | ||
} | ||
// Move over all consecutive "*"'s. | ||
@@ -215,3 +201,2 @@ // Also store the previous and next characters | ||
let nextChar = glob[i + 1]; | ||
if (!globstar) { | ||
@@ -226,3 +211,2 @@ // globstar is disabled, so treat any number of "*" as one | ||
(nextChar === '/' || nextChar === undefined); // to the end of the segment | ||
if (isGlobstar) { | ||
@@ -238,3 +222,2 @@ // it's a globstar, so match zero or more path segments | ||
break; | ||
default: | ||
@@ -257,1 +240,3 @@ add(c); | ||
} | ||
module.exports = globrex; |
18770
211
157