Skip to content

Commit c4b617c

Browse files
authored
Add excludeDuplicatePatterns (#12)
1 parent 625dfc4 commit c4b617c

File tree

3 files changed

+90
-3
lines changed

3 files changed

+90
-3
lines changed

index.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,3 +95,17 @@ export function globToRegex(...globs: readonly string[]): RegExp {
9595

9696
return new RegExp(globs.map(x => getRawGlobRegex(x)).join('|'));
9797
}
98+
99+
export function excludeDuplicatePatterns(matchPatterns: readonly string[]): string[] {
100+
if (matchPatterns.includes('<all_urls>')) {
101+
return ['<all_urls>'];
102+
}
103+
104+
if (matchPatterns.includes('*://*/*')) {
105+
return ['*://*/*'];
106+
}
107+
108+
return matchPatterns.filter(possibleSubset => !matchPatterns.some(possibleSuperset =>
109+
possibleSubset !== possibleSuperset && patternToRegex(possibleSuperset).test(possibleSubset),
110+
));
111+
}

readme.md

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,20 @@ import {patternToRegex} from 'webext-patterns';
2424

2525
## Usage
2626

27-
> **Note**
28-
> Firefox and Chrome handle globs very slighly differently. `webext-patterns` defaults to Chrome’s logic, but if it detects a Firefox userAgent it will produce a Firefox-compatible regex.
29-
3027
```js
3128
patternToRegex('http://*/*');
3229
// Returns /^http:[/][/][^/]+[/].+$/
3330

3431
globToRegex('*.example.com');
3532
// Returns /\.example\.com$/
33+
34+
excludeDuplicatePatterns(['https://*.google.com/*', 'https://google.com/*']);
35+
// Returns ['https://*.google.com/*']
3636
```
3737

38+
> **Note**
39+
> Firefox and Chrome handle patterns very slighly differently. `webext-patterns` defaults to Chrome’s logic, but if it detects a Firefox userAgent it will produce a Firefox-compatible regex.
40+
3841
## API
3942

4043
#### patternToRegex(pattern1, pattern2, etc)
@@ -84,6 +87,19 @@ googleRegex.test('https://google.it/search'); // -> true
8487
googleRegex.test('https://google.de/search'); // -> false
8588
```
8689

90+
#### excludeDuplicatePatterns([pattern1, pattern2, etc])
91+
92+
Accepts an array of patterns and returns a filtered array without the patterns that are already covered by others. For example `"https://*/*"` already covers all "https" URLs, so having `"https://google.com/*"` in the array won't make any difference and therefore it's dropped.
93+
94+
```js
95+
excludeDuplicatePatterns([
96+
"https://*/*",
97+
"https://google.com/*",
98+
"https://*.example.com/*",
99+
]);
100+
// Returns ["https://*/*"]
101+
```
102+
87103
## Related
88104

89105
### Permissions

test/duplicate-patterns.js

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import test from 'ava';
2+
import {excludeDuplicatePatterns} from '../index.js';
3+
4+
test('excludeDuplicatePatterns', t => {
5+
t.deepEqual(
6+
excludeDuplicatePatterns([
7+
'http://neverssl.com/*',
8+
'https://*.example.com/*',
9+
'<all_urls>',
10+
'https://fregante.com/*',
11+
'*://*/*',
12+
]),
13+
['<all_urls>'],
14+
'<all_urls> should catch all',
15+
);
16+
17+
t.deepEqual(
18+
excludeDuplicatePatterns([
19+
'http://neverssl.com/*',
20+
'https://*.example.com/*',
21+
'*://*/*',
22+
'https://fregante.com/*',
23+
]),
24+
['*://*/*'],
25+
'*://*/* should catch all',
26+
);
27+
28+
t.deepEqual(
29+
excludeDuplicatePatterns([
30+
'http://*.example.com/*',
31+
'https://*/*',
32+
'https://fregante.com/*',
33+
]),
34+
['http://*.example.com/*', 'https://*/*'],
35+
'https://*/* should drop all other https origins',
36+
);
37+
38+
t.deepEqual(
39+
excludeDuplicatePatterns([
40+
'https://git.example.com/*',
41+
'https://*.example.com/*',
42+
'https://example.com/*',
43+
'https://fregante.com/*',
44+
]),
45+
['https://*.example.com/*', 'https://fregante.com/*'],
46+
'A subdomain star should drop all other same-domain origins',
47+
);
48+
49+
t.deepEqual(
50+
excludeDuplicatePatterns([
51+
'https://git.example.com/*',
52+
'https://git.example.com/fregante/*',
53+
]),
54+
['https://git.example.com/*'],
55+
'A pathname star should drop all other same-origin origins',
56+
);
57+
});

0 commit comments

Comments
 (0)