fast-slice-ansi
Version:
A tiny and fast text slicing library which takes ANSI escapes into account.
40 lines (24 loc) • 759 B
Markdown
> A fast `String
Inspired by `slice-ansi`, this comes with various performance improvements.
```bash
npm i -S fast-slice-ansi
```
```ts
import {sliceAnsi} from 'fast-slice-ansi';
import {styleText} from 'node:util';
const input = `Hello ${styleText('blue', 'blue')} world`;
const result = sliceAnsi(input, 6);
// result: '\u001b[34mblue\u001b[39m world'
```
An options object can be passed which has the following properties:
- `visual`: If `true` (default), slices operate against the visual representation of the string (e.g. a surrogate pair is counted as one character)
Example:
```ts
sliceAnsi(input, 0, 3, {visual: false});
```
MIT