Glob matching for javascript/node.js, a replacement and faster alternative to minimatch and multimatch
90
Specialized utility functions for specific matching scenarios including negation filtering, object key matching, and advanced collection operations.
Returns strings that do NOT match any of the given patterns - the inverse of the main micromatch function.
/**
* Returns a list of strings that do not match any of the given patterns
* @param {string[]} list - Array of strings to match
* @param {string|string[]} patterns - One or more glob pattern to use for matching
* @param {object} options - See available options for changing how matches are performed
* @returns {string[]} Returns an array of strings that do not match the given patterns
*/
function not(list, patterns, options);Usage Examples:
const { not } = require('micromatch');
// Basic negation filtering
console.log(not(['a.a', 'b.b', 'c.c'], '*.a'));
//=> ['b.b', 'c.c']
// Multiple patterns
const files = ['app.js', 'test.js', 'readme.md', 'config.json'];
console.log(not(files, ['*.js', '*.md']));
//=> ['config.json']
// With complex patterns
console.log(not(['foo.js', 'bar.txt', 'baz.js'], '**/*.js'));
//=> ['bar.txt']Filters the keys of an object using glob patterns, returning a new object with only matching keys.
/**
* Filter the keys of the given object with the given glob pattern and options
* @param {object} object - The object with keys to filter
* @param {string|string[]} patterns - One or more glob patterns to use for matching
* @param {object} options - See available options for changing how matches are performed
* @returns {object} Returns an object with only keys that match the given patterns
*/
function matchKeys(object, patterns, options);Usage Examples:
const { matchKeys } = require('micromatch');
// Basic key matching
const obj = { aa: 'a', ab: 'b', ac: 'c', ba: 'd' };
console.log(matchKeys(obj, '*b'));
//=> { ab: 'b' }
// Multiple patterns
console.log(matchKeys(obj, ['a*', 'ba']));
//=> { aa: 'a', ab: 'b', ac: 'c', ba: 'd' }
// API endpoint filtering example
const routes = {
'GET /users': 'getUsers',
'POST /users': 'createUser',
'GET /posts': 'getPosts',
'DELETE /users': 'deleteUser'
};
console.log(matchKeys(routes, 'GET *'));
//=> { 'GET /users': 'getUsers', 'GET /posts': 'getPosts' }These utilities build on the core matching functionality to provide specialized collection handling:
The not() function implements inverse matching logic:
The matchKeys() function applies glob matching to object keys:
Object.keys()TypeError if the first argument is not an objectTypeError if first argument is not an objectconst files = ['src/app.js', 'src/app.test.js', 'dist/app.js', 'README.md'];
// Get non-source files
const nonSource = not(files, 'src/**');
//=> ['dist/app.js', 'README.md']
// Get non-test files
const nonTest = not(files, '**/*.test.*');
//=> ['src/app.js', 'dist/app.js', 'README.md']const config = {
'app.port': 3000,
'app.host': 'localhost',
'db.host': 'localhost',
'db.port': 5432,
'cache.enabled': true
};
// Get all app settings
const appConfig = matchKeys(config, 'app.*');
//=> { 'app.port': 3000, 'app.host': 'localhost' }
// Get all port settings
const portConfig = matchKeys(config, '*.port');
//=> { 'app.port': 3000, 'db.port': 5432 }const routes = {
'GET /api/users': 'listUsers',
'POST /api/users': 'createUser',
'GET /api/posts': 'listPosts',
'DELETE /api/users/:id': 'deleteUser'
};
// Get all GET routes
const getRoutes = matchKeys(routes, 'GET *');
// Exclude DELETE routes
const safeRoutes = matchKeys(routes, ['*', '!DELETE *']);Install with Tessl CLI
npx tessl i tessl/npm-micromatchdocs
evals
scenario-1
scenario-2
scenario-3
scenario-4
scenario-5
scenario-6
scenario-7
scenario-8
scenario-9
scenario-10