UNPKG

@tsed/schema

Version:
51 lines (50 loc) 1.69 kB
import picomatch from "picomatch"; function micromatch(list, patterns) { patterns = [].concat(patterns); list = [].concat(list); let omit = new Set(); let keep = new Set(); let items = new Set(); let negatives = 0; let onResult = (state) => { items.add(state.output); }; for (let i = 0; i < patterns.length; i++) { let isMatch = picomatch(String(patterns[i]), { onResult }, true); let negated = isMatch.state.negated || isMatch.state.negatedExtglob; if (negated) { negatives++; } for (let item of list) { let matched = isMatch(item, true); let match = negated ? !matched.isMatch : matched.isMatch; if (!match) continue; if (negated) { omit.add(matched.output); } else { omit.delete(matched.output); keep.add(matched.output); } } } let result = negatives === patterns.length ? [...items] : [...keep]; return !!result.filter((item) => !omit.has(item)).length; } /** * @ignore */ export function matchGroups(groups, compareWith = []) { const groupsExcludes = groups.filter((group) => group.startsWith("!")).map((group) => group.replace("!", "")); const groupsIncludes = groups.filter((group) => !group.startsWith("!")); if (groupsExcludes.length) { if (compareWith.length && micromatch(groupsExcludes, compareWith)) { return true; } } if (groupsIncludes.length) { return !micromatch(groups.filter((group) => !group.startsWith("!")), compareWith); } return false; }