Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 917d710

Browse files
authored
feat(pluginutils): add suffixRegex & support multiple string (#1886)
* feat(pluginutils): add `suffixRegex` & support multiple string * chore: update
1 parent 0862902 commit 917d710

File tree

5 files changed

+104
-12
lines changed

5 files changed

+104
-12
lines changed

packages/pluginutils/README.md

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ export default function myPlugin(options = {}) {
227227

228228
Constructs a RegExp that matches the exact string specified. This is useful for plugin hook filters.
229229

230-
Parameters: `(str: String, flags?: String)`<br>
230+
Parameters: `(str: String | Array[...String], flags?: String)`<br>
231231
Returns: `RegExp`
232232

233233
#### Usage
@@ -236,6 +236,7 @@ Returns: `RegExp`
236236
import { exactRegex } from '@rollup/pluginutils';
237237

238238
exactRegex('foobar'); // /^foobar$/
239+
exactRegex(['foo', 'bar']); // /^(?:foo|bar)$/
239240
exactRegex('foo(bar)', 'i'); // /^foo\(bar\)$/i
240241
```
241242

@@ -275,7 +276,7 @@ normalizePath('foo/bar'); // 'foo/bar'
275276

276277
Constructs a RegExp that matches a value that has the specified prefix. This is useful for plugin hook filters.
277278

278-
Parameters: `(str: String, flags?: String)`<br>
279+
Parameters: `(str: String | Array[...String], flags?: String)`<br>
279280
Returns: `RegExp`
280281

281282
#### Usage
@@ -284,9 +285,27 @@ Returns: `RegExp`
284285
import { prefixRegex } from '@rollup/pluginutils';
285286

286287
prefixRegex('foobar'); // /^foobar/
288+
prefixRegex(['foo', 'bar']); // /^(?:foo|bar)/
287289
prefixRegex('foo(bar)', 'i'); // /^foo\(bar\)/i
288290
```
289291

292+
### suffixRegex
293+
294+
Constructs a RegExp that matches a value that has the specified suffix. This is useful for plugin hook filters.
295+
296+
Parameters: `(str: String | Array[...String], flags?: String)`<br>
297+
Returns: `RegExp`
298+
299+
#### Usage
300+
301+
```js
302+
import { suffixRegex } from '@rollup/pluginutils';
303+
304+
suffixRegex('foobar'); // /foobar$/
305+
suffixRegex(['foo', 'bar']); // /(?:foo|bar)$/
306+
suffixRegex('foo(bar)', 'i'); // /foo\(bar\)$/i
307+
```
308+
290309
## Meta
291310

292311
[CONTRIBUTING](/.github/CONTRIBUTING.md)
Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,26 @@
1-
export function exactRegex(str: string, flags?: string): RegExp {
2-
return new RegExp(`^${escapeRegex(str)}$`, flags);
1+
export function exactRegex(str: string | string[], flags?: string): RegExp {
2+
return new RegExp(`^${combineMultipleStrings(str)}$`, flags);
33
}
44

5-
export function prefixRegex(str: string, flags?: string): RegExp {
6-
return new RegExp(`^${escapeRegex(str)}`, flags);
5+
export function prefixRegex(str: string | string[], flags?: string): RegExp {
6+
return new RegExp(`^${combineMultipleStrings(str)}`, flags);
7+
}
8+
9+
export function suffixRegex(str: string | string[], flags?: string): RegExp {
10+
return new RegExp(`${combineMultipleStrings(str)}$`, flags);
711
}
812

913
const escapeRegexRE = /[-/\\^$*+?.()|[\]{}]/g;
1014
function escapeRegex(str: string): string {
1115
return str.replace(escapeRegexRE, '\\$&');
1216
}
17+
function combineMultipleStrings(str: string | string[]): string {
18+
if (Array.isArray(str)) {
19+
const escapeStr = str.map(escapeRegex).join('|');
20+
if (escapeStr && str.length > 1) {
21+
return `(?:${escapeStr})`;
22+
}
23+
return escapeStr;
24+
}
25+
return escapeRegex(str);
26+
}

packages/pluginutils/src/index.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import dataToEsm from './dataToEsm';
55
import extractAssignedNames from './extractAssignedNames';
66
import makeLegalIdentifier from './makeLegalIdentifier';
77
import normalizePath from './normalizePath';
8-
import { exactRegex, prefixRegex } from './filterUtils';
8+
import { exactRegex, prefixRegex, suffixRegex } from './filterUtils';
99

1010
export {
1111
addExtension,
@@ -16,7 +16,8 @@ export {
1616
extractAssignedNames,
1717
makeLegalIdentifier,
1818
normalizePath,
19-
prefixRegex
19+
prefixRegex,
20+
suffixRegex
2021
};
2122

2223
// TODO: remove this in next major
@@ -29,5 +30,6 @@ export default {
2930
extractAssignedNames,
3031
makeLegalIdentifier,
3132
normalizePath,
32-
prefixRegex
33+
prefixRegex,
34+
suffixRegex
3335
};
Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,75 @@
11
import test from 'ava';
22

3-
import { exactRegex, prefixRegex } from '../';
3+
import { exactRegex, prefixRegex, suffixRegex } from '../';
44

55
test('exactRegex supports without flag parameter', (t) => {
66
t.is(exactRegex('foo').toString(), '/^foo$/');
77
});
88

9+
test('exactRegex supports with multiple string and without flag parameter', (t) => {
10+
t.is(exactRegex(['foo', 'bar']).toString(), '/^(?:foo|bar)$/');
11+
});
12+
913
test('exactRegex supports with flag parameter', (t) => {
1014
t.is(exactRegex('foo', 'i').toString(), '/^foo$/i');
1115
});
1216

17+
test('exactRegex supports with multiple string and flag parameter', (t) => {
18+
t.is(exactRegex(['foo', 'bar'], 'i').toString(), '/^(?:foo|bar)$/i');
19+
});
20+
1321
test('exactRegex escapes special characters for Regex', (t) => {
1422
t.is(exactRegex('foo(bar)').toString(), '/^foo\\(bar\\)$/');
1523
});
1624

25+
test('exactRegex escapes special characters with multiple string for Regex', (t) => {
26+
t.is(exactRegex(['foo(bar)', 'baz(qux)']).toString(), '/^(?:foo\\(bar\\)|baz\\(qux\\))$/');
27+
});
28+
1729
test('prefixRegex supports without flag parameter', (t) => {
1830
t.is(prefixRegex('foo').toString(), '/^foo/');
1931
});
2032

33+
test('prefixRegex supports with multiple string and without flag parameter', (t) => {
34+
t.is(prefixRegex(['foo', 'bar']).toString(), '/^(?:foo|bar)/');
35+
});
36+
2137
test('prefixRegex supports with flag parameter', (t) => {
2238
t.is(prefixRegex('foo', 'i').toString(), '/^foo/i');
2339
});
2440

41+
test('prefixRegex supports with multiple string and flag parameter', (t) => {
42+
t.is(prefixRegex(['foo', 'bar'], 'i').toString(), '/^(?:foo|bar)/i');
43+
});
44+
2545
test('prefixRegex escapes special characters for Regex', (t) => {
2646
t.is(prefixRegex('foo(bar)').toString(), '/^foo\\(bar\\)/');
2747
});
48+
49+
test('prefixRegex escapes special characters with multiple string for Regex', (t) => {
50+
t.is(prefixRegex(['foo(bar)', 'baz(qux)']).toString(), '/^(?:foo\\(bar\\)|baz\\(qux\\))/');
51+
});
52+
53+
test('suffixRegex supports without flag parameter', (t) => {
54+
t.is(suffixRegex('foo').toString(), '/foo$/');
55+
});
56+
57+
test('suffixRegex supports with multiple string and without flag parameter', (t) => {
58+
t.is(suffixRegex(['foo', 'bar']).toString(), '/(?:foo|bar)$/');
59+
});
60+
61+
test('suffixRegex supports with flag parameter', (t) => {
62+
t.is(suffixRegex('foo', 'i').toString(), '/foo$/i');
63+
});
64+
65+
test('suffixRegex supports with multiple string and flag parameter', (t) => {
66+
t.is(suffixRegex(['foo', 'bar'], 'i').toString(), '/(?:foo|bar)$/i');
67+
});
68+
69+
test('suffixRegex escapes special characters for Regex', (t) => {
70+
t.is(suffixRegex('foo(bar)').toString(), '/foo\\(bar\\)$/');
71+
});
72+
73+
test('suffixRegex escapes special characters with multiple string for Regex', (t) => {
74+
t.is(suffixRegex(['foo(bar)', 'baz(qux)']).toString(), '/(?:foo\\(bar\\)|baz\\(qux\\))$/');
75+
});

packages/pluginutils/types/index.d.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ export function dataToEsm(data: unknown, options?: DataToEsmOptions): string;
6767
* @param str the string to match.
6868
* @param flags flags for the RegExp.
6969
*/
70-
export function exactRegex(str: string, flags?: string): RegExp;
70+
export function exactRegex(str: string | string[], flags?: string): RegExp;
7171

7272
/**
7373
* Extracts the names of all assignment targets based upon specified patterns.
@@ -90,7 +90,14 @@ export function normalizePath(filename: string): string;
9090
* @param str the string to match.
9191
* @param flags flags for the RegExp.
9292
*/
93-
export function prefixRegex(str: string, flags?: string): RegExp;
93+
export function prefixRegex(str: string | string[], flags?: string): RegExp;
94+
95+
/**
96+
* Constructs a RegExp that matches a value that has the specified suffix.
97+
* @param str the string to match.
98+
* @param flags flags for the RegExp.
99+
*/
100+
export function suffixRegex(str: string | string[], flags?: string): RegExp;
94101

95102
export type AddExtension = typeof addExtension;
96103
export type AttachScopes = typeof attachScopes;
@@ -101,6 +108,7 @@ export type MakeLegalIdentifier = typeof makeLegalIdentifier;
101108
export type NormalizePath = typeof normalizePath;
102109
export type DataToEsm = typeof dataToEsm;
103110
export type PrefixRegex = typeof prefixRegex;
111+
export type SuffixRegex = typeof suffixRegex;
104112

105113
declare const defaultExport: {
106114
addExtension: AddExtension;
@@ -112,5 +120,6 @@ declare const defaultExport: {
112120
makeLegalIdentifier: MakeLegalIdentifier;
113121
normalizePath: NormalizePath;
114122
prefixRegex: PrefixRegex;
123+
suffixRegex: SuffixRegex;
115124
};
116125
export default defaultExport;

0 commit comments

Comments
 (0)