-
-
Notifications
You must be signed in to change notification settings - Fork 151
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(strings): add stripAnsi(), lengthAnsi() fns
- Loading branch information
1 parent
118f97f
commit 86fa81a
Showing
2 changed files
with
24 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
const RE = /\x1b\[[0-9;]+m/g; | ||
|
||
/** | ||
* Removes all ANSI control sequences from given string. | ||
* | ||
* @example | ||
* ```ts | ||
* stripAnsi("\x1B[32mhello\x1B[0m \x1B[91mworld\x1B[0m!""); | ||
* // 'hello world!' | ||
* ``` | ||
* | ||
* @param x | ||
*/ | ||
export const stripAnsi = (x: string) => x.replace(RE, ""); | ||
|
||
/** | ||
* Returns length of `x` excluding any ANSI control sequences (via | ||
* {@link stripAnsi}). | ||
* | ||
* @param x | ||
*/ | ||
export const lengthAnsi = (x: string) => stripAnsi(x).length; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters