-
-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Sindre Sorhus <[email protected]>
- Loading branch information
1 parent
2b8c961
commit 840a5ea
Showing
1 changed file
with
6 additions
and
1 deletion.
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 |
---|---|---|
@@ -1,9 +1,14 @@ | ||
import ansiRegex from 'ansi-regex'; | ||
|
||
const regex = ansiRegex(); | ||
|
||
export default function stripAnsi(string) { | ||
if (typeof string !== 'string') { | ||
throw new TypeError(`Expected a \`string\`, got \`${typeof string}\``); | ||
} | ||
|
||
return string.replace(ansiRegex(), ''); | ||
// Even though the regex is global, we don't need to reset the `.lastIndex` | ||
// because unlike `.exec()` and `.test()`, `.replace()` does it automatically | ||
// and doing it manually has a performance penalty. | ||
return string.replace(regex, ''); | ||
} |