wildcard-match
Advanced tools
Comparing version 3.0.0 to 4.0.0
@@ -1,7 +0,8 @@ | ||
interface MatchFn { | ||
(sample: string): boolean; | ||
pattern: string; | ||
interface Options { | ||
separator?: string; | ||
} | ||
declare function wildcardMatch(pattern: string, separator?: string): MatchFn; | ||
declare function wildcardMatch(pattern: string | string[], separator?: string | Options): RegExp & { | ||
pattern?: string | string[] | undefined; | ||
options: Options; | ||
}; | ||
export = wildcardMatch; |
@@ -11,3 +11,3 @@ "use strict"; | ||
} | ||
function transformPatternWithSeparators(pattern, separator) { | ||
function buildPatternWithSeparators(pattern, separator) { | ||
let segments = pattern.split(separator); | ||
@@ -34,14 +34,13 @@ return segments.reduce((result, segment, i) => { | ||
} | ||
function wildcardMatch(pattern, separator) { | ||
function buildRegExpPattern(pattern, separator) { | ||
if (Array.isArray(pattern)) { | ||
let regExpPatterns = pattern.map((p) => `^${buildRegExpPattern(p, separator)}$`); | ||
return `(${regExpPatterns.join('|')})`; | ||
} | ||
if (pattern === '**') { | ||
// @ts-expect-error: in this case the sample argument is not needed, | ||
// but making it optional is undesirable | ||
let match = (() => true); | ||
match.pattern = pattern; | ||
match.separator = separator; | ||
return match; | ||
return '^.*$'; | ||
} | ||
let regexpPattern; | ||
let regExpPattern; | ||
if (!separator) { | ||
regexpPattern = escapeRegExpString(pattern) | ||
regExpPattern = escapeRegExpString(pattern) | ||
.replace(/(?<!\\)\?/g, '.') | ||
@@ -51,13 +50,17 @@ .replace(/(?<!\\)\*/g, '.*'); | ||
else if (pattern === '*') { | ||
regexpPattern = `((?!${escapeRegExpString(separator)}).)*`; | ||
regExpPattern = `((?!${escapeRegExpString(separator)}).)*`; | ||
} | ||
else { | ||
regexpPattern = transformPatternWithSeparators(escapeRegExpString(pattern), escapeRegExpString(separator)); | ||
regExpPattern = buildPatternWithSeparators(escapeRegExpString(pattern), escapeRegExpString(separator)); | ||
} | ||
let regexp = new RegExp(`^${regexpPattern}$`); | ||
let match = ((sample) => regexp.test(sample)); | ||
match.pattern = pattern; | ||
match.separator = separator; | ||
return match; | ||
return `^${regExpPattern}$`; | ||
} | ||
function wildcardMatch(pattern, separator) { | ||
let options = typeof separator === 'object' ? separator : { separator }; | ||
let regexpPattern = buildRegExpPattern(pattern, options.separator); | ||
let regExp = new RegExp(regexpPattern); | ||
regExp.pattern = pattern; | ||
regExp.options = options; | ||
return regExp; | ||
} | ||
// Support both CommonJS and ES6-like modules. | ||
@@ -64,0 +67,0 @@ // Could be `wildcardMatch.default = wildcardMatch`, but TypeScript has a bug: |
{ | ||
"name": "wildcard-match", | ||
"version": "3.0.0", | ||
"description": "Check if a string matches a glob-like pattern containing ?, * and **", | ||
"version": "4.0.0", | ||
"description": "Compile a glob-like pattern into a regular expression", | ||
"author": "Alex Schneider <me@schneider.ax>", | ||
@@ -12,3 +12,4 @@ "repository": "https://github.com/axtgr/wildcard-match", | ||
"glob", | ||
"pattern" | ||
"pattern", | ||
"regexp" | ||
], | ||
@@ -15,0 +16,0 @@ "main": "dist/index.js", |
# wildcard-match | ||
Check if a string matches a glob-like pattern containing wildcards. | ||
Compile a glob-like pattern into a regular expression. | ||
```js | ||
match('f?o*')('foobar') | ||
import wcm from 'wildcard-match' | ||
const regExp = wcm('wildc?rd-mat*') | ||
regExp.test('wildcard-match') //=> true | ||
``` | ||
@@ -14,6 +17,6 @@ | ||
- `**` matches any number of segments when used as a whole segment (i.e. `/**/` in the middle, `**/` or `/**` at the beginning/end of a string) | ||
- `**` matches any number of segments when used as a whole segment (i.e. `/**/` in the middle, `**/` at the beginning or `/**` at the end of a separated string) | ||
```js | ||
match('src/**/*.?s', '/')('src/lib/component/index.js') | ||
wcm('src/**/*.?s', '/').test('src/lib/component/index.js') //=> true | ||
``` | ||
@@ -27,6 +30,7 @@ | ||
### wildcardMatch(pattern, separator?): MatchFn | ||
### wildcardMatch(pattern, separator?): RegExp | ||
The default export is a function that takes a pattern and an optional separator. | ||
It compiles the pattern and returns a function for matching strings with it. | ||
The default export is a function that takes a string or an array of strings and an optional | ||
separator (or an options object with a _separator_ property). It compiles the pattern into | ||
a RegExp object that can be used to match strings with the pattern. | ||
@@ -36,12 +40,27 @@ ```js | ||
const match = wcm('foo*/b?r', '/') | ||
match('foo/bar') //=> true | ||
match('foobar') //=> false | ||
const regExp = wcm('foo*/b?r', '/') | ||
regExp.test('foo/bar') //=> true | ||
regExp.test('foobar') //=> false | ||
``` | ||
The returned function has `pattern` and `separator` properties set to the original values. | ||
```js | ||
const regExp = wcm(['one.*', '*.two'], { separator: '.' }) | ||
regExp.test('one.two') //=> true | ||
regExp.test('one.three') //=> true | ||
regExp.test('three.two') //=> true | ||
regExp.test('one') //=> false | ||
regExp.test('two') //=> false | ||
regExp.test('one.two.three') //=> false | ||
regExp.test('three.false') //=> false | ||
``` | ||
The returned RegExp has `pattern` and `options` properties set to the original values. | ||
```js | ||
match.pattern //=> 'foo*/b?r' | ||
match.separator //=> '/' | ||
const regExp = wcm('p?tt?rn', '/') | ||
match.pattern //=> 'p?tt?rn' | ||
match.options //=> { separator: '/' } | ||
``` | ||
@@ -52,7 +71,7 @@ | ||
```js | ||
const match = wcm('foo\\*') | ||
const regExp = wcm('foo\\*') | ||
match('foo') //=> false | ||
match('foobar') //=> false | ||
match('foo*') //=> true | ||
regExp.test('foo') //=> false | ||
regExp.test('foobar') //=> false | ||
regExp.test('foo*') //=> true | ||
``` | ||
@@ -63,7 +82,7 @@ | ||
```js | ||
const match = wcm('foo/**bar') | ||
const regExp = wcm('foo/**bar') | ||
match('foo/bar') //=> true | ||
match('foo/bazbar') //=> true | ||
match('foo/baz/qux/bar') //=> true | ||
regExp.test('foo/bar') //=> true | ||
regExp.test('foo/bazbar') //=> true | ||
regExp.test('foo/baz/qux/bar') //=> true | ||
``` | ||
@@ -77,8 +96,8 @@ | ||
// *? matches any non-empty substring | ||
const match = wcm('*?.js') | ||
const regExp = wcm('*?.js') | ||
match('index.js') //=> true | ||
match('src/index.js') //=> true | ||
match('.js') //=> false | ||
match('src') //=> false | ||
regExp.test('index.js') //=> true | ||
regExp.test('src/index.js') //=> true | ||
regExp.test('.js') //=> false | ||
regExp.test('src') //=> false | ||
``` | ||
@@ -89,10 +108,10 @@ | ||
const match = wcm('src/**/index.?s', '/') | ||
const regExp = wcm('src/**/index.?s', '/') | ||
match('src/index.js') //=> true | ||
match('src/lib/index.ts') //=> true | ||
match('src/lib/component/test/index.ts') //=> true | ||
match('src') //=> false | ||
match('index.js') //=> false | ||
match('src/index.js/lib') //=> false | ||
regExp.test('src/index.js') //=> true | ||
regExp.test('src/lib/index.ts') //=> true | ||
regExp.test('src/lib/component/test/index.ts') //=> true | ||
regExp.test('src') //=> false | ||
regExp.test('index.js') //=> false | ||
regExp.test('src/index.js/lib') //=> false | ||
``` | ||
@@ -103,9 +122,9 @@ | ||
const match = wcm('**.*.example.com', '.') | ||
const regExp = wcm('**.*.example.com', '.') | ||
match('example.com') //=> false | ||
match('foo.example.com') //=> true | ||
match('foo.bar.example.com') //=> true | ||
match('foo.bar.baz.qux.example.com') //=> true | ||
match('foo.example.com.bar') //=> false | ||
regExp.test('example.com') //=> false | ||
regExp.test('foo.example.com') //=> true | ||
regExp.test('foo.bar.example.com') //=> true | ||
regExp.test('foo.bar.baz.qux.example.com') //=> true | ||
regExp.test('foo.example.com.bar') //=> false | ||
``` | ||
@@ -115,2 +134,3 @@ | ||
- [globrex](https://github.com/terkelg/globrex) | ||
- [minimatch](https://github.com/isaacs/minimatch) | ||
@@ -117,0 +137,0 @@ - [micromatch](https://github.com/micromatch/micromatch) |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
10520
76
134