Blazing fast and accurate glob matcher written in JavaScript, with no dependencies and full support for standard and extended Bash glob features, including braces, extglobs, POSIX brackets, and regular expressions.
83
Direct pattern testing without creating persistent matcher functions. Useful for one-off matching operations and advanced pattern validation.
Test input string against a regex with detailed matching information.
/**
* Test input with the given regex
* @param {string} input - String to test
* @param {RegExp} regex - Regular expression to test against
* @param {object} options - Configuration options
* @param {object} context - Additional context with glob and posix info
* @returns {object} Object with matching info
*/
picomatch.test(input, regex, options, { glob, posix });
interface TestResult {
isMatch: boolean;
match: RegExpExecArray | null;
output: string;
}Usage Examples:
const picomatch = require('picomatch');
// Test with compiled regex
const regex = picomatch.makeRe('*.js');
const result = picomatch.test('file.js', regex);
console.log(result.isMatch); // => true
console.log(result.match); // => ['file.js', ...]
console.log(result.output); // => 'file.js'
// Test with context information
const testResult = picomatch.test('src/app.js', /^(?:src\/[^/]*?\.js)$/, {}, {
glob: 'src/*.js',
posix: true
});
console.log(testResult.isMatch); // => true
// Test with options
const formatResult = picomatch.test('src\\app.js', regex, {
format: (path) => path.replace(/\\/g, '/')
});Quick boolean test for pattern matching without detailed result information.
/**
* Returns true if any of the given patterns match the string
* @param {string} str - String to test
* @param {string|string[]} patterns - Glob pattern(s) to test
* @param {object} options - Configuration options
* @returns {boolean} True if any patterns match
*/
picomatch.isMatch(str, patterns, options);Usage Examples:
// Single pattern test
console.log(picomatch.isMatch('file.js', '*.js')); // => true
console.log(picomatch.isMatch('file.txt', '*.js')); // => false
// Multiple patterns - returns true if ANY match
console.log(picomatch.isMatch('app.ts', ['*.js', '*.ts'])); // => true
// With options
console.log(picomatch.isMatch('FILE.JS', '*.js', { nocase: true })); // => true
// Complex patterns
console.log(picomatch.isMatch('src/utils/helper.js', 'src/**/*.js')); // => true
console.log(picomatch.isMatch('test/app.spec.js', '**/*.spec.js')); // => true
// Negation patterns
console.log(picomatch.isMatch('src/app.js', '!test/**')); // => true
console.log(picomatch.isMatch('test/app.js', '!test/**')); // => falseMatch the basename of a filepath against a pattern or regex.
/**
* Match the basename of a filepath
* @param {string} input - Filepath to test
* @param {RegExp|string} glob - Glob pattern or regex
* @param {object} options - Configuration options
* @returns {boolean} True if basename matches
*/
picomatch.matchBase(input, glob, options);Usage Examples:
// Match basename only
console.log(picomatch.matchBase('path/to/file.js', '*.js')); // => true
console.log(picomatch.matchBase('deeply/nested/app.ts', '*.ts')); // => true
// Using regex
const jsRegex = /\.js$/;
console.log(picomatch.matchBase('src/utils/helper.js', jsRegex)); // => true
// With options
console.log(picomatch.matchBase('FILE.JS', '*.js', { nocase: true })); // => true
// Complex basename matching
console.log(picomatch.matchBase('component.test.js', '*.test.js')); // => true
console.log(picomatch.matchBase('utils/component.test.js', '*.test.js')); // => trueValidate patterns and handle edge cases.
Usage Examples:
// Error handling for invalid patterns
try {
picomatch.isMatch('file.js', ''); // Throws TypeError
} catch (error) {
console.log(error.message); // => 'Expected pattern to be a non-empty string'
}
try {
picomatch.isMatch('file.js', null); // Throws TypeError
} catch (error) {
console.log(error.message); // => 'Expected pattern to be a non-empty string'
}
// Valid empty string input
console.log(picomatch.isMatch('', '*')); // => false
// Testing with various input types
console.log(picomatch.isMatch('123', '*')); // => true
console.log(picomatch.isMatch('', '')); // => false (empty pattern throws)Globstar Patterns:
// Recursive directory matching
console.log(picomatch.isMatch('src/deep/nested/file.js', 'src/**/*.js')); // => true
console.log(picomatch.isMatch('src/file.js', 'src/**/*.js')); // => true
// Globstar boundaries
console.log(picomatch.isMatch('src/file.js', '**/file.js')); // => true
console.log(picomatch.isMatch('deep/path/file.js', '**/file.js')); // => trueExtended Glob Patterns:
// Extglob patterns (advanced)
console.log(picomatch.isMatch('file.js', '*.+(js|ts)')); // => true
console.log(picomatch.isMatch('file.coffee', '*.+(js|ts)')); // => false
// Negation extglobs
console.log(picomatch.isMatch('file.txt', '*.!(js|ts)')); // => true
console.log(picomatch.isMatch('file.js', '*.!(js|ts)')); // => falseBrace Expansion:
// Brace patterns
console.log(picomatch.isMatch('file.js', '*.{js,ts,json}')); // => true
console.log(picomatch.isMatch('config.json', '*.{js,ts,json}')); // => true
// Numeric braces
console.log(picomatch.isMatch('file1.js', 'file{1..3}.js')); // => true
console.log(picomatch.isMatch('file4.js', 'file{1..3}.js')); // => falseInstall with Tessl CLI
npx tessl i tessl/npm-picomatchevals
scenario-1
scenario-2
scenario-3
scenario-4
scenario-5
scenario-6
scenario-7
scenario-8
scenario-9
scenario-10