-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
573 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
node_modules | ||
.DS_Store |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
language: node_js | ||
node_js: | ||
- 8 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
'use strict'; | ||
|
||
const glob = require('./src'); | ||
|
||
let pattern = 'p*uck'; | ||
|
||
console.log(glob(pattern)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2018 Terkel Gjervig Nielsen | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
# globalyzer | ||
|
||
> Detect and extract the glob part of a string | ||
Utility to detect if a string contains a glob and then split it in a glob and none-glob part. | ||
|
||
## Install | ||
|
||
``` | ||
npm install globalyzer --save | ||
``` | ||
|
||
## Usage | ||
|
||
```js | ||
const globalyzer = require('globalyzer'); | ||
|
||
globalyzer('foo/bar/.git/'); | ||
// => { base: 'foo/bar/.git/', glob: '', isGlob: false } | ||
|
||
globalyzer('foo/bar/**/baz'); | ||
// => { base: 'foo/bar', glob: '**/baz', isGlob: true } | ||
``` | ||
|
||
|
||
## API | ||
|
||
### globalyzer(glob, options) | ||
|
||
Type: `function`<br> | ||
Returns: `{ base, glob, isGlob }` | ||
|
||
Returns an object with the (non-glob) base path and the actual pattern. | ||
|
||
#### options.strict | ||
|
||
Type: `Boolean`<br> | ||
Default: `true` | ||
|
||
Be strict about what's a glob and what's not | ||
|
||
|
||
#### glob | ||
|
||
Type: `String` | ||
|
||
Glob string to analyze. | ||
|
||
|
||
## Credit | ||
|
||
This is a fork of [is-glob](https://github.com/micromatch/is-glob) and [glob-base](https://github.com/micromatch/glob-base) | ||
|
||
|
||
## License | ||
|
||
MIT © [Terkel Gjervig](https://terkel.com) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
'use strict'; | ||
|
||
const os = require('os'); | ||
const path = require('path'); | ||
const isWin = os.platform() === 'win32'; | ||
|
||
const CHARS = { '{': '}', '(': ')', '[': ']'}; | ||
const STRICT = /\\(.)|(^!|\*|[\].+)]\?|\[[^\\\]]+\]|\{[^\\}]+\}|\(\?[:!=][^\\)]+\)|\([^|]+\|[^\\)]+\)|(\\).|([@?!+*]\(.*\)))/; | ||
const RELAXED = /\\(.)|(^!|[*?{}()[\]]|\(\?)/; | ||
|
||
/** | ||
* Detect if a string cointains glob | ||
* @param {String} str Input string | ||
* @param {Object} [options] Configuration object | ||
* @param {Boolean} [options.strict=true] Use relaxed regex if true | ||
* @returns {Boolean} true if string contains glob | ||
*/ | ||
function isglob(str, { strict = true } = {}) { | ||
if (str === '') return false; | ||
let match, rgx = strict ? STRICT : RELAXED; | ||
|
||
while ((match = rgx.exec(str))) { | ||
if (match[2]) return true; | ||
let idx = match.index + match[0].length; | ||
|
||
// if an open bracket/brace/paren is escaped, | ||
// set the index to the next closing character | ||
let open = match[1]; | ||
let close = open ? CHARS[open] : null; | ||
if (open && close) { | ||
let n = str.indexOf(close, idx); | ||
if (n !== -1) idx = n + 1; | ||
} | ||
|
||
str = str.slice(idx); | ||
} | ||
return false; | ||
} | ||
|
||
|
||
/** | ||
* Find the static part of a glob-path, | ||
* split path and return path part | ||
* @param {String} str Path/glob string | ||
* @returns {String} static path section of glob | ||
*/ | ||
function parent(str, { strict = false } = {}) { | ||
if (isWin && str.includes('/')) | ||
str = str.split('\\').join('/'); | ||
|
||
// special case for strings ending in enclosure containing path separator | ||
if (/[\{\[].*[\/]*.*[\}\]]$/.test(str)) str += '/'; | ||
|
||
// preserves full path in case of trailing path separator | ||
str += 'a'; | ||
|
||
do {str = path.dirname(str)} | ||
while (isglob(str, {strict}) || /(^|[^\\])([\{\[]|\([^\)]+$)/.test(str)); | ||
|
||
// remove escape chars and return result | ||
return str.replace(/\\([\*\?\|\[\]\(\)\{\}])/g, '$1'); | ||
}; | ||
|
||
|
||
/** | ||
* Parse a glob path, and split it by static/glob part | ||
* @param {String} pattern String path | ||
* @param {Object} [opts] Options | ||
* @param {Object} [opts.strict=false] Use strict parsing | ||
* @returns {Object} object with parsed path | ||
*/ | ||
function globalyzer(pattern, opts = {}) { | ||
let base = parent(pattern, opts); | ||
let isGlob = isglob(pattern, opts); | ||
let glob; | ||
|
||
if (base != '.') { | ||
glob = pattern.substr(base.length); | ||
if (glob.startsWith('/')) glob = glob.substr(1); | ||
} else { | ||
glob = pattern; | ||
} | ||
|
||
if (!isGlob) { | ||
base = path.dirname(pattern); | ||
glob = base !== '.' ? pattern.substr(base.length) : pattern; | ||
} | ||
|
||
if (glob.startsWith('./')) glob = glob.substr(2); | ||
if (glob.startsWith('/')) glob = glob.substr(1); | ||
|
||
return { base, glob, isGlob }; | ||
} | ||
|
||
|
||
module.exports = globalyzer; |
Oops, something went wrong.