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

brace-expansion.mddocs/

Brace Expansion

Comprehensive brace pattern processing for expanding and manipulating brace expressions like {a,b,c} and {1..10}, powered by the braces library.

Capabilities

Brace Processing

Processes brace patterns with options to control expansion behavior.

/**
 * Process the given brace pattern
 * @param {string} pattern - String with brace pattern to process
 * @param {object} options - Any options to change how expansion is performed
 * @returns {string[]} Array of processed patterns
 */
function braces(pattern, options);

Usage Examples:

const { braces } = require('micromatch');

// Basic alternation - returns regex pattern by default
console.log(braces('foo/{a,b,c}/bar'));
//=> ['foo/(a|b|c)/bar']

// Numeric range
console.log(braces('file{1..3}.txt'));
//=> ['file(1|2|3).txt']

// Character range  
console.log(braces('test{a..c}.js'));
//=> ['test(a|b|c).js']

Brace Expansion

Expands brace patterns into individual strings (sets expand: true option automatically).

/**
 * Expand braces with expand option enabled
 * @param {string} pattern - String with brace pattern to expand
 * @param {object} options - Any options to change how expansion is performed
 * @returns {string[]} Array of expanded patterns
 */
function braceExpand(pattern, options);

Usage Examples:

const { braceExpand } = require('micromatch');

// Expanded alternation
console.log(braceExpand('foo/{a,b,c}/bar'));
//=> ['foo/a/bar', 'foo/b/bar', 'foo/c/bar']

// Expanded numeric range
console.log(braceExpand('file{1..3}.txt'));
//=> ['file1.txt', 'file2.txt', 'file3.txt']

// Expanded character range
console.log(braceExpand('test{a..c}.js'));
//=> ['testa.js', 'testb.js', 'testc.js']

// Complex nested patterns
console.log(braceExpand('src/{app,lib}/{*.js,*.ts}'));
//=> ['src/app/*.js', 'src/app/*.ts', 'src/lib/*.js', 'src/lib/*.ts']

Brace Detection Utility

Helper function to detect if a string contains brace patterns (exposed for testing purposes).

/**
 * Helper function to detect brace patterns in strings
 * @param {string} str - String to test for braces
 * @returns {boolean} Returns true if string contains brace patterns
 */
function hasBraces(str);

Usage Examples:

const { hasBraces } = require('micromatch');

console.log(hasBraces('foo/{a,b,c}/bar')); //=> true
console.log(hasBraces('foo/bar')); //=> false
console.log(hasBraces('file{1..10}.txt')); //=> true

Brace Pattern Types

Alternation Patterns

Comma-separated alternatives within braces:

// Basic alternation
'{a,b,c}' → ['a', 'b', 'c']

// With surrounding text
'file.{js,ts,json}' → ['file.js', 'file.ts', 'file.json']

// Nested alternation
'{src,test}/{*.js,*.ts}' → ['src/*.js', 'src/*.ts', 'test/*.js', 'test/*.ts']

Numeric Ranges

Sequential numeric expansion:

// Basic numeric range
'{1..5}' → ['1', '2', '3', '4', '5']

// With zero padding
'{01..05}' → ['01', '02', '03', '04', '05']

// With step increment
'{1..10..2}' → ['1', '3', '5', '7', '9']

// Negative numbers
'{-2..2}' → ['-2', '-1', '0', '1', '2']

Character Ranges

Sequential character expansion:

// Basic character range
'{a..e}' → ['a', 'b', 'c', 'd', 'e']

// Uppercase range
'{A..E}' → ['A', 'B', 'C', 'D', 'E']

// Mixed case (preserves case of endpoints)
'{a..E}' → ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E']

Complex Nested Patterns

Multiple brace patterns can be combined:

// Multiple brace sets
'{a,b}{1,2}' → ['a1', 'a2', 'b1', 'b2']

// Mixed pattern types
'{src,test}/file{1..3}.{js,ts}' → [
  'src/file1.js', 'src/file1.ts', 'src/file2.js', 'src/file2.ts',
  'src/file3.js', 'src/file3.ts', 'test/file1.js', 'test/file1.ts',
  'test/file2.js', 'test/file2.ts', 'test/file3.js', 'test/file3.ts'
]

Options

Expansion Control

  • expand: true - Return array of expanded strings instead of regex pattern
  • nobrace: true - Disable brace expansion (treat braces as literals)

Range Options

  • expandRange: function - Custom function for expanding ranges
  • rangeLimit: number - Limit the number of range expansions (default: 65536)

Format Options

  • nodupes: false - Allow duplicate values in results
  • unescape: true - Remove escaping from brace characters

Error Handling

  • Invalid brace patterns are treated as literal strings
  • Empty brace patterns {} are treated as literal braces
  • Malformed ranges fall back to literal interpretation
  • Very large ranges are limited by rangeLimit option (default: 65536)

Integration Notes

  • The braces() and braceExpand() functions delegate to the braces npm package
  • These functions are used internally by other micromatch functions during pattern processing
  • Brace expansion happens before glob pattern matching in the main micromatch pipeline

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