UNPKG

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