mirror of
				https://github.com/fooflington/selfdefined.git
				synced 2025-11-03 23:29:03 +00:00 
			
		
		
		
	
		
			
				
	
	
		
			113 lines
		
	
	
		
			4.2 KiB
		
	
	
	
		
			JavaScript
		
	
	
	
	
	
			
		
		
	
	
			113 lines
		
	
	
		
			4.2 KiB
		
	
	
	
		
			JavaScript
		
	
	
	
	
	
"use strict";
 | 
						|
Object.defineProperty(exports, "__esModule", { value: true });
 | 
						|
const path = require("path");
 | 
						|
const globParent = require("glob-parent");
 | 
						|
const micromatch = require("micromatch");
 | 
						|
const GLOBSTAR = '**';
 | 
						|
const ESCAPE_SYMBOL = '\\';
 | 
						|
const COMMON_GLOB_SYMBOLS_RE = /[*?]|^!/;
 | 
						|
const REGEX_CHARACTER_CLASS_SYMBOLS_RE = /\[.*]/;
 | 
						|
const REGEX_GROUP_SYMBOLS_RE = /(?:^|[^@!*?+])\(.*\|.*\)/;
 | 
						|
const GLOB_EXTENSION_SYMBOLS_RE = /[@!*?+]\(.*\)/;
 | 
						|
const BRACE_EXPANSIONS_SYMBOLS_RE = /{.*(?:,|\.\.).*}/;
 | 
						|
function isStaticPattern(pattern, options = {}) {
 | 
						|
    return !isDynamicPattern(pattern, options);
 | 
						|
}
 | 
						|
exports.isStaticPattern = isStaticPattern;
 | 
						|
function isDynamicPattern(pattern, options = {}) {
 | 
						|
    /**
 | 
						|
     * When the `caseSensitiveMatch` option is disabled, all patterns must be marked as dynamic, because we cannot check
 | 
						|
     * filepath directly (without read directory).
 | 
						|
     */
 | 
						|
    if (options.caseSensitiveMatch === false || pattern.includes(ESCAPE_SYMBOL)) {
 | 
						|
        return true;
 | 
						|
    }
 | 
						|
    if (COMMON_GLOB_SYMBOLS_RE.test(pattern) || REGEX_CHARACTER_CLASS_SYMBOLS_RE.test(pattern) || REGEX_GROUP_SYMBOLS_RE.test(pattern)) {
 | 
						|
        return true;
 | 
						|
    }
 | 
						|
    if (options.extglob !== false && GLOB_EXTENSION_SYMBOLS_RE.test(pattern)) {
 | 
						|
        return true;
 | 
						|
    }
 | 
						|
    if (options.braceExpansion !== false && BRACE_EXPANSIONS_SYMBOLS_RE.test(pattern)) {
 | 
						|
        return true;
 | 
						|
    }
 | 
						|
    return false;
 | 
						|
}
 | 
						|
exports.isDynamicPattern = isDynamicPattern;
 | 
						|
function convertToPositivePattern(pattern) {
 | 
						|
    return isNegativePattern(pattern) ? pattern.slice(1) : pattern;
 | 
						|
}
 | 
						|
exports.convertToPositivePattern = convertToPositivePattern;
 | 
						|
function convertToNegativePattern(pattern) {
 | 
						|
    return '!' + pattern;
 | 
						|
}
 | 
						|
exports.convertToNegativePattern = convertToNegativePattern;
 | 
						|
function isNegativePattern(pattern) {
 | 
						|
    return pattern.startsWith('!') && pattern[1] !== '(';
 | 
						|
}
 | 
						|
exports.isNegativePattern = isNegativePattern;
 | 
						|
function isPositivePattern(pattern) {
 | 
						|
    return !isNegativePattern(pattern);
 | 
						|
}
 | 
						|
exports.isPositivePattern = isPositivePattern;
 | 
						|
function getNegativePatterns(patterns) {
 | 
						|
    return patterns.filter(isNegativePattern);
 | 
						|
}
 | 
						|
exports.getNegativePatterns = getNegativePatterns;
 | 
						|
function getPositivePatterns(patterns) {
 | 
						|
    return patterns.filter(isPositivePattern);
 | 
						|
}
 | 
						|
exports.getPositivePatterns = getPositivePatterns;
 | 
						|
function getBaseDirectory(pattern) {
 | 
						|
    return globParent(pattern, { flipBackslashes: false });
 | 
						|
}
 | 
						|
exports.getBaseDirectory = getBaseDirectory;
 | 
						|
function hasGlobStar(pattern) {
 | 
						|
    return pattern.includes(GLOBSTAR);
 | 
						|
}
 | 
						|
exports.hasGlobStar = hasGlobStar;
 | 
						|
function endsWithSlashGlobStar(pattern) {
 | 
						|
    return pattern.endsWith('/' + GLOBSTAR);
 | 
						|
}
 | 
						|
exports.endsWithSlashGlobStar = endsWithSlashGlobStar;
 | 
						|
function isAffectDepthOfReadingPattern(pattern) {
 | 
						|
    const basename = path.basename(pattern);
 | 
						|
    return endsWithSlashGlobStar(pattern) || isStaticPattern(basename);
 | 
						|
}
 | 
						|
exports.isAffectDepthOfReadingPattern = isAffectDepthOfReadingPattern;
 | 
						|
function getNaiveDepth(pattern) {
 | 
						|
    const base = getBaseDirectory(pattern);
 | 
						|
    const patternDepth = pattern.split('/').length;
 | 
						|
    const patternBaseDepth = base.split('/').length;
 | 
						|
    /**
 | 
						|
     * This is a hack for pattern that has no base directory.
 | 
						|
     *
 | 
						|
     * This is related to the `*\something\*` pattern.
 | 
						|
     */
 | 
						|
    if (base === '.') {
 | 
						|
        return patternDepth - patternBaseDepth;
 | 
						|
    }
 | 
						|
    return patternDepth - patternBaseDepth - 1;
 | 
						|
}
 | 
						|
exports.getNaiveDepth = getNaiveDepth;
 | 
						|
function getMaxNaivePatternsDepth(patterns) {
 | 
						|
    return patterns.reduce((max, pattern) => {
 | 
						|
        const depth = getNaiveDepth(pattern);
 | 
						|
        return depth > max ? depth : max;
 | 
						|
    }, 0);
 | 
						|
}
 | 
						|
exports.getMaxNaivePatternsDepth = getMaxNaivePatternsDepth;
 | 
						|
function makeRe(pattern, options) {
 | 
						|
    return micromatch.makeRe(pattern, options);
 | 
						|
}
 | 
						|
exports.makeRe = makeRe;
 | 
						|
function convertPatternsToRe(patterns, options) {
 | 
						|
    return patterns.map((pattern) => makeRe(pattern, options));
 | 
						|
}
 | 
						|
exports.convertPatternsToRe = convertPatternsToRe;
 | 
						|
function matchAny(entry, patternsRe) {
 | 
						|
    const filepath = entry.replace(/^\.[\\/]/, '');
 | 
						|
    return patternsRe.some((patternRe) => patternRe.test(filepath));
 | 
						|
}
 | 
						|
exports.matchAny = matchAny;
 |