An open API service indexing awesome lists of open source software.

https://github.com/shahradelahi/ts-braces

⚡ High-performance, V8-safe numeric & alphabetical range expander and regex compiler.
https://github.com/shahradelahi/ts-braces

alphabetical-range braces numeric-range range-expansion regex-compiler typescript v8-safe

Last synced: 29 days ago
JSON representation

⚡ High-performance, V8-safe numeric & alphabetical range expander and regex compiler.

Awesome Lists containing this project

README

          


@se-oss/braces


CI
NPM Version
MIT License
npm bundle size
Install Size

_@se-oss/braces_ is a high-performance, V8-safe numeric and alphabetical range expander and regular expression compiler.

---

- [Installation](#-installation)
- [Usage](#-usage)
- [Documentation](#-documentation)
- [Contributing](#-contributing)
- [License](#license)

## 📦 Installation

```bash
pnpm add @se-oss/braces
```

Install using your favorite package manager

**npm**

```bash
npm install @se-oss/braces
```

**yarn**

```bash
yarn add @se-oss/braces
```

## 📖 Usage

### Range Expansion

Expand numeric or alphabetical range patterns with optional stepping.

```ts
import { expandRange } from '@se-oss/braces';

// Numeric ranges
expandRange('1..3'); // ['1', '2', '3']

// Alphabetical ranges
expandRange('a..c'); // ['a', 'b', 'c']
```

### Step Parameters

Specify a custom step interval for the range.

```ts
import { expandRange } from '@se-oss/braces';

expandRange('1..5..2'); // ['1', '3', '5']
```

### Zero-Padding Preservation

Automatically preserves leading zeros in numeric ranges.

```ts
import { expandRange } from '@se-oss/braces';

expandRange('01..03'); // ['01', '02', '03']
```

### Expansion Limit & Errors

Enforce safety limits on large range expansions to prevent resource exhaustion.

```ts
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!');
}
}
```

### V8-Safe Regular Expression Compiler

Compile numeric ranges into highly optimized and V8-safe regular expression patterns.

```ts
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'); // false
```

## 📚 Documentation

For more information, please see the JSDoc comments or check the source code.

## 🤝 Contributing

Want to contribute? Awesome! To show your support is to star the project, or to raise issues on [GitHub](https://github.com/shahradelahi/ts-braces).

Thanks again for your support, it is much appreciated! 🙏

## License

[MIT](/LICENSE) © [Shahrad Elahi](https://github.com/shahradelahi) and [contributors](https://github.com/shahradelahi/ts-braces/graphs/contributors).