forked from elastic/kibana
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmake_matcher.ts
51 lines (45 loc) · 1.53 KB
/
make_matcher.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the "Elastic License
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side
* Public License v 1"; you may not use this file except in compliance with, at
* your election, the "Elastic License 2.0", the "GNU Affero General Public
* License v3.0 only", or the "Server Side Public License, v 1".
*/
import pm from 'picomatch';
export type Matcher = (path: string) => boolean;
/**
* Simplified version of picomatch options, focusing on options useful to Kibana
* that we have actually tested.
*/
export interface MatchOptions {
/**
* One or more glob patterns for excluding strings that should not be matched from the result.
*/
ignore?: string[];
/**
* Make matching case-insensitive. Equivalent to the regex `i` flag.
*/
caseInsensitive?: boolean;
}
/**
* Create a matcher function which will can test if a string matches any
* of the positive patterns and none of the negative patterns
*/
export function makeMatcher(patterns: string[], options?: MatchOptions) {
const negative: string[] = [];
const positive: string[] = [];
for (const e of patterns) {
if (e.startsWith('!')) {
negative.push(e.slice(1));
} else {
positive.push(e);
}
}
const matcher = pm(positive, {
nocase: options?.caseInsensitive,
nonegate: true,
ignore: [...(options?.ignore ?? []), ...negative],
});
return (val: string) => matcher(val);
}