CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-micromatch

Glob matching for javascript/node.js, a replacement and faster alternative to minimatch and multimatch

90

1.50x
Overview
Eval results
Files

utility-functions.mddocs/

Utility Functions

Specialized utility functions for specific matching scenarios including negation filtering, object key matching, and advanced collection operations.

Capabilities

Negation Filtering

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']

Object Key Filtering

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' }

Advanced Collection Operations

These utilities build on the core matching functionality to provide specialized collection handling:

Negation Logic

The not() function implements inverse matching logic:

  1. Runs the main micromatch function to find all matches
  2. Returns all items from the original list that were NOT in the matches
  3. Preserves original order of non-matching items
  4. Supports all the same options as the main micromatch function

Object Key Processing

The matchKeys() function applies glob matching to object keys:

  1. Extracts all keys from the input object using Object.keys()
  2. Filters keys using the main micromatch function
  3. Constructs a new object with only the matching keys and their values
  4. Throws TypeError if the first argument is not an object

Error Handling

Not Function

  • Handles empty input lists gracefully (returns empty array)
  • Processes all the same pattern types as main micromatch function
  • Supports negation patterns within the patterns parameter
  • Preserves array order of non-matching items

MatchKeys Function

  • Throws TypeError if first argument is not an object
  • Handles objects with no enumerable keys (returns empty object)
  • Processes all key types that can be converted to strings
  • Returns new object instance (does not modify original)

Use Cases

File System Operations

const 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']

Configuration Management

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 }

API Route Processing

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-micromatch

docs

boolean-testing.md

brace-expansion.md

index.md

main-matching.md

pattern-processing.md

utility-functions.md

tile.json