1 | 'use strict';
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 | const SimpleMatcher = require('./SimpleMatcher');
|
8 | const RegexMatcher = require('./RegexMatcher');
|
9 |
|
10 | const IS_PATTERN_REGEX = new RegExp('\\*|#');
|
11 |
|
12 | const cache = new Map();
|
13 |
|
14 | const createMatcher = (pattern) => {
|
15 | if (!IS_PATTERN_REGEX.test(pattern))
|
16 | return new SimpleMatcher(pattern);
|
17 | return RegexMatcher(pattern);
|
18 | };
|
19 |
|
20 | const matcher = (pattern) => {
|
21 | if (!cache.has(pattern))
|
22 | cache.set(pattern, createMatcher(pattern));
|
23 | return cache.get(pattern);
|
24 | };
|
25 |
|
26 | const clearCache = () => cache.clear();
|
27 |
|
28 | const match = (pattern, text) => matcher(pattern).match(text);
|
29 |
|
30 | module.exports = {
|
31 | match,
|
32 | matcher,
|
33 | clearCache,
|
34 | };
|
35 |
|