Brace expansion as known from sh/bash for JavaScript applications
npx @tessl/cli install tessl/npm-brace-expansion@4.0.0Brace expansion as known from sh/bash shells, implemented in JavaScript. This library enables developers to expand brace patterns into arrays of strings, supporting comma-separated lists, numeric sequences, alphabetic sequences, and nested expansions with full compatibility with shell brace expansion behavior.
npm install brace-expansionimport expand from 'brace-expansion';This package is ES module only and does not support CommonJS require().
import expand from 'brace-expansion';
// Comma-separated lists
expand('file-{a,b,c}.jpg');
// => ['file-a.jpg', 'file-b.jpg', 'file-c.jpg']
// Empty options
expand('-v{,,}');
// => ['-v', '-v', '-v']
// Numeric sequences
expand('file{0..2}.jpg');
// => ['file0.jpg', 'file1.jpg', 'file2.jpg']
// Alphabetic sequences
expand('file-{a..c}.jpg');
// => ['file-a.jpg', 'file-b.jpg', 'file-c.jpg']
// Reverse sequences
expand('file{2..0}.jpg');
// => ['file2.jpg', 'file1.jpg', 'file0.jpg']
// Sequences with increments
expand('file{0..4..2}.jpg');
// => ['file0.jpg', 'file2.jpg', 'file4.jpg']
// Alphabetic sequences with increments
expand('file-{a..e..2}.jpg');
// => ['file-a.jpg', 'file-c.jpg', 'file-e.jpg']
// Zero-padded sequences
expand('file{00..10..5}.jpg');
// => ['file00.jpg', 'file05.jpg', 'file10.jpg']
// Nested expansions
expand('{{A..C},{a..c}}');
// => ['A', 'B', 'C', 'a', 'b', 'c']
// Complex nested patterns
expand('ppp{,config,oe{,conf}}');
// => ['ppp', 'pppconfig', 'pppoe', 'pppoeconf']Expands brace patterns into arrays of all possible string combinations.
/**
* Expand brace patterns into arrays of strings
* @param {string} str - Input string containing brace patterns to expand
* @returns {string[]} Array of all possible and valid expansions
*/
function expand(str)Parameters:
str (string): Input string containing brace patterns. Can include comma-separated lists ({a,b,c}), numeric sequences ({1..5}), alphabetic sequences ({a..e}), sequences with increments ({0..10..2}), and nested patterns.Returns:
string[]: Array of all possible and valid expansions. If no valid expansions are found, returns an array containing the original string [str]. Empty or null input returns an empty array [].Supported Expansion Patterns:
Comma-separated lists: {a,b,c} expands to individual options
{red,green,blue} → ['red', 'green', 'blue']{a,,c} → ['a', '', 'c']Numeric sequences: {start..end} or {start..end..increment}
{1..5} → ['1', '2', '3', '4', '5']{0..10..2} → ['0', '2', '4', '6', '8', '10']{3..-2} → ['3', '2', '1', '0', '-1', '-2']{5..1} → ['5', '4', '3', '2', '1']Alphabetic sequences: {start..end} or {start..end..increment}
{a..e} → ['a', 'b', 'c', 'd', 'e']{a..z..2} → ['a', 'c', 'e', 'g', ...]{A..C} → ['A', 'B', 'C']Zero-padded sequences: Leading zeros are preserved
{01..05} → ['01', '02', '03', '04', '05']{007..010} → ['007', '008', '009', '010']Nested expansions: Multiple levels of brace nesting supported
{a,{b,c},d} → ['a', 'b', 'c', 'd']{{A..C},{a..c}} → ['A', 'B', 'C', 'a', 'b', 'c']Combined patterns: Mixing text with multiple brace expansions
file{1..2}.{jpg,png} → ['file1.jpg', 'file1.png', 'file2.jpg', 'file2.png']Special Cases:
$ are not expanded for compatibility reasons (e.g., ${var}, prefix${a,b} → ['prefix${a,b}'])\\{ and \\} are treated as literal characters{} at the start are escaped and preserved: {}file → ['{}file']Error Handling:
Usage Examples:
// URL generation
expand('http://server{1..3}.example.com/api/v{1,2}');
// => ['http://server1.example.com/api/v1', 'http://server1.example.com/api/v2',
// 'http://server2.example.com/api/v1', 'http://server2.example.com/api/v2',
// 'http://server3.example.com/api/v1', 'http://server3.example.com/api/v2']
// File patterns
expand('backup-{2023..2025}-{jan,feb,mar}.{tar,zip}');
// Generates all combinations: backup-2023-jan.tar, backup-2023-jan.zip, etc.
// Command variations
expand('mkdir /usr/local/src/bash/{old,new,dist,bugs}');
// => ['mkdir /usr/local/src/bash/old', 'mkdir /usr/local/src/bash/new',
// 'mkdir /usr/local/src/bash/dist', 'mkdir /usr/local/src/bash/bugs']
// Invalid patterns (return original)
expand('invalid{pattern');
// => ['invalid{pattern']
// Variable-like patterns (not expanded)
expand('${a,b}${c,d}');
// => ['${a,b}${c,d}']
expand('${1..3}');
// => ['${1..3}']
// Mixed patterns with variables
expand('{a,b}${c,d}${e,f}');
// => ['a${c,d}${e,f}', 'b${c,d}${e,f}']
expand('');
// => []