glob-to-regex.js 1.0.1 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/index.d.ts CHANGED
@@ -1,3 +1,7 @@
1
+ export interface GlobOptions {
2
+ /** Treat pattern as case insensitive */
3
+ nocase?: boolean;
4
+ }
1
5
  /**
2
6
  * Convert a glob pattern to a regular expression
3
7
  *
@@ -9,7 +13,7 @@
9
13
  * - `{}` to group conditions (e.g. `{html,txt}`)
10
14
  * - `[abc]`, `[a-z]`, `[!a-z]`, `[!abc]` character classes
11
15
  */
12
- export declare const toRegex: (pattern: string) => RegExp;
16
+ export declare const toRegex: (pattern: string, options?: GlobOptions) => RegExp;
13
17
  /**
14
18
  * A glob pattern to match files paths against. An array or a single pattern
15
19
  * can be provided, if an array is given, then individual patterns will be
@@ -21,4 +25,4 @@ export declare const toRegex: (pattern: string) => RegExp;
21
25
  */
22
26
  export type Pattern = string | RegExp | (string | RegExp)[];
23
27
  export type Matcher = (path: string) => boolean;
24
- export declare const toMatcher: (pattern: Pattern) => Matcher;
28
+ export declare const toMatcher: (pattern: Pattern, options?: GlobOptions) => Matcher;
package/lib/index.js CHANGED
@@ -13,7 +13,7 @@ const escapeRe = (ch) => (/[.^$+{}()|\\]/.test(ch) ? `\\${ch}` : ch);
13
13
  * - `{}` to group conditions (e.g. `{html,txt}`)
14
14
  * - `[abc]`, `[a-z]`, `[!a-z]`, `[!abc]` character classes
15
15
  */
16
- const toRegex = (pattern) => {
16
+ const toRegex = (pattern, options) => {
17
17
  let regexStr = '';
18
18
  let i = 0;
19
19
  // Helper to parse a brace group like {a,b,c}. No nesting support.
@@ -45,7 +45,7 @@ const toRegex = (pattern) => {
45
45
  return '\\{' + escapeRe(cur);
46
46
  }
47
47
  // Convert each part recursively to support globs inside braces
48
- const alt = parts.map((p) => (0, exports.toRegex)(p).source.replace(/^\^/, '').replace(/\$$/, '')).join('|');
48
+ const alt = parts.map((p) => (0, exports.toRegex)(p, options).source.replace(/^\^/, '').replace(/\$$/, '')).join('|');
49
49
  return `(?:${alt})`;
50
50
  };
51
51
  while (i < pattern.length) {
@@ -134,11 +134,12 @@ const toRegex = (pattern) => {
134
134
  break;
135
135
  }
136
136
  }
137
- return new RegExp('^' + regexStr + '$');
137
+ const flags = options?.nocase ? 'i' : '';
138
+ return new RegExp('^' + regexStr + '$', flags);
138
139
  };
139
140
  exports.toRegex = toRegex;
140
141
  const isRegExp = /^\/(.{1,4096})\/([gimsuy]{0,6})$/;
141
- const toMatcher = (pattern) => {
142
+ const toMatcher = (pattern, options) => {
142
143
  const regexes = [];
143
144
  const patterns = Array.isArray(pattern) ? pattern : [pattern];
144
145
  for (const pat of patterns) {
@@ -149,19 +150,15 @@ const toMatcher = (pattern) => {
149
150
  regexes.push(new RegExp(expr, flags));
150
151
  }
151
152
  else {
152
- regexes.push((0, exports.toRegex)(pat));
153
+ regexes.push((0, exports.toRegex)(pat, options));
153
154
  }
154
155
  }
155
156
  else {
156
157
  regexes.push(pat);
157
158
  }
158
159
  }
159
- const length = regexes.length;
160
- return (path) => {
161
- for (let i = 0; i < length; i++)
162
- if (regexes[i].test(path))
163
- return true;
164
- return false;
165
- };
160
+ return regexes.length
161
+ ? new Function('p', 'return ' + regexes.map((r) => r + '.test(p)').join('||'))
162
+ : () => false;
166
163
  };
167
164
  exports.toMatcher = toMatcher;
package/package.json CHANGED
@@ -1,10 +1,10 @@
1
1
  {
2
2
  "name": "glob-to-regex.js",
3
- "packageManager": "yarn@4.9.3",
3
+ "packageManager": "yarn@4.9.4",
4
4
  "publishConfig": {
5
5
  "access": "public"
6
6
  },
7
- "version": "1.0.1",
7
+ "version": "1.1.0",
8
8
  "description": "Transform GLOB patterns to JavaScript regular expressions for fast file path matching.",
9
9
  "author": {
10
10
  "name": "streamich",