(include?, exclude?, options?)
| 24 | } |
| 25 | |
| 26 | const createFilter: CreateFilter = function createFilter(include?, exclude?, options?) { |
| 27 | const resolutionBase = options && options.resolve; |
| 28 | |
| 29 | const getMatcher = (id: string | RegExp) => |
| 30 | id instanceof RegExp |
| 31 | ? id |
| 32 | : { |
| 33 | test: (what: string) => { |
| 34 | // this refactor is a tad overly verbose but makes for easy debugging |
| 35 | const pattern = getMatcherString(id, resolutionBase); |
| 36 | const fn = pm(pattern, { dot: true }); |
| 37 | const result = fn(what); |
| 38 | |
| 39 | return result; |
| 40 | } |
| 41 | }; |
| 42 | |
| 43 | const includeMatchers = ensureArray(include).map(getMatcher); |
| 44 | const excludeMatchers = ensureArray(exclude).map(getMatcher); |
| 45 | |
| 46 | if (!includeMatchers.length && !excludeMatchers.length) |
| 47 | return (id) => typeof id === 'string' && !id.includes('\0'); |
| 48 | |
| 49 | return function result(id: string | unknown): boolean { |
| 50 | if (typeof id !== 'string') return false; |
| 51 | if (id.includes('\0')) return false; |
| 52 | |
| 53 | const pathId = normalizePath(id); |
| 54 | |
| 55 | for (let i = 0; i < excludeMatchers.length; ++i) { |
| 56 | const matcher = excludeMatchers[i]; |
| 57 | if (matcher instanceof RegExp) { |
| 58 | matcher.lastIndex = 0; |
| 59 | } |
| 60 | if (matcher.test(pathId)) return false; |
| 61 | } |
| 62 | |
| 63 | for (let i = 0; i < includeMatchers.length; ++i) { |
| 64 | const matcher = includeMatchers[i]; |
| 65 | if (matcher instanceof RegExp) { |
| 66 | matcher.lastIndex = 0; |
| 67 | } |
| 68 | if (matcher.test(pathId)) return true; |
| 69 | } |
| 70 | |
| 71 | return !includeMatchers.length; |
| 72 | }; |
| 73 | }; |
| 74 | |
| 75 | export { createFilter as default }; |
no test coverage detected