@se-oss/braces is a high-performance, V8-safe numeric and alphabetical range expander and regular expression compiler.
pnpm add @se-oss/bracesInstall using your favorite package manager
npm
npm install @se-oss/bracesyarn
yarn add @se-oss/bracesExpand numeric or alphabetical range patterns with optional stepping.
import { expandRange } from '@se-oss/braces';
// Numeric ranges
expandRange('1..3'); // ['1', '2', '3']
// Alphabetical ranges
expandRange('a..c'); // ['a', 'b', 'c']Specify a custom step interval for the range.
import { expandRange } from '@se-oss/braces';
expandRange('1..5..2'); // ['1', '3', '5']Automatically preserves leading zeros in numeric ranges.
import { expandRange } from '@se-oss/braces';
expandRange('01..03'); // ['01', '02', '03']Enforce safety limits on large range expansions to prevent resource exhaustion.
import { BraceLimitError, expandRange } from '@se-oss/braces';
try {
// Try to expand a huge range with a low threshold limit
expandRange('1..10000', 10);
} catch (error) {
if (error instanceof BraceLimitError) {
console.error('Expansion limit exceeded!');
}
}Compile numeric ranges into highly optimized and V8-safe regular expression patterns.
import { compileNumericRange } from '@se-oss/braces';
const pattern = compileNumericRange(1, 250);
// returns: "[1-9]|[1-9]\d|1\d{2}|2[0-4]\d|250"
const regex = new RegExp(`^(?:${pattern})$`);
regex.test('99'); // true
regex.test('250'); // true
regex.test('251'); // falseFor more information, please see the JSDoc comments or check the source code.
Want to contribute? Awesome! To show your support is to star the project, or to raise issues on GitHub.
Thanks again for your support, it is much appreciated! 🙏
MIT © Shahrad Elahi and contributors.