Skip to content

Commit

Permalink
feat(strings): add stripAnsi(), lengthAnsi() fns
Browse files Browse the repository at this point in the history
  • Loading branch information
postspectacular committed Jan 8, 2021
1 parent 118f97f commit 86fa81a
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
22 changes: 22 additions & 0 deletions packages/strings/src/ansi.ts
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;
2 changes: 2 additions & 0 deletions packages/strings/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
export * from "./api";

export * from "./ansi";
export * from "./case";
export * from "./center";
export * from "./cursor";
Expand Down

0 comments on commit 86fa81a

Please sign in to comment.