UNPKG

archunit

Version:

ArchUnit TypeScript is an architecture testing library, to specify and assert architecture rules in your TypeScript app

107 lines 2.95 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.RegexFactory = exports.getPatternString = void 0; const minimatch_1 = require("minimatch"); /** * Helper function to extract readable pattern strings from regex * This function formats regex patterns for display purposes by removing excessive escaping */ function getPatternString(pattern) { // For display purposes, return the original regex source without double escaping const source = pattern.source; // Remove excessive escaping for common cases const result = source.replace(/\\\\(.)/g, '\\$1'); return result; } exports.getPatternString = getPatternString; class RegexFactory { static escapeRegex(str) { return str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); //.replace('/', '\\/'); } static fileNameMatcher(name) { let regExp; if (typeof name === 'string') { regExp = this.globToRegExp(name); } else { regExp = name; } return { regExp, options: { target: 'filename', }, }; } static classNameMatcher(name) { let regExp; if (typeof name === 'string') { regExp = this.globToRegExp(name); } else { regExp = name; } return { regExp, options: { target: 'classname', }, }; } static folderMatcher(folder) { let regExp; if (typeof folder === 'string') { regExp = this.globToRegExp(folder); } else { regExp = folder; } return { regExp, options: { target: 'path-no-filename', }, }; } static pathMatcher(path) { let regExp; if (typeof path === 'string') { regExp = this.globToRegExp(path); } else { regExp = path; } return { regExp, options: { target: 'path', }, }; } /** * Creates a filter for exact file path matching * @param filePath Exact file path to match */ static exactFileMatcher(filePath) { const escapedPath = this.escapeRegex(filePath.replace(/\\/g, '/')); const regExp = new RegExp(`^${escapedPath}$`); return { regExp, options: { target: 'path', }, }; } } exports.RegexFactory = RegexFactory; //private static containsGlobSyntax(inp: string): boolean { // return inp.includes('*') || inp.includes('?'); //} RegexFactory.globToRegExp = (pattern) => { const ret = minimatch_1.minimatch.makeRe(pattern); if (typeof ret === 'boolean') { throw new Error('invalid pattern'); } return ret; }; //# sourceMappingURL=regex-factory.js.map