Skip to content

Commit

Permalink
add strip_ansi string util
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanatkn committed Nov 8, 2024
1 parent 3bcd9ff commit ffb7587
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 18 deletions.
5 changes: 5 additions & 0 deletions .changeset/shiny-bugs-flow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@ryanatkn/belt": patch
---

add `strip_ansi` string util
24 changes: 12 additions & 12 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,16 +59,16 @@
"@ryanatkn/gro": "^0.146.2",
"@ryanatkn/moss": "^0.20.2",
"@sveltejs/adapter-static": "^3.0.6",
"@sveltejs/kit": "^2.7.5",
"@sveltejs/kit": "^2.8.0",
"@sveltejs/package": "^2.3.7",
"@sveltejs/vite-plugin-svelte": "^4.0.0",
"@types/node": "^22.9.0",
"eslint": "^9.14.0",
"eslint-plugin-svelte": "^2.46.0",
"prettier": "^3.3.3",
"prettier-plugin-svelte": "^3.2.7",
"svelte": "^5.1.9",
"svelte-check": "^4.0.5",
"svelte": "^5.1.12",
"svelte-check": "^4.0.6",
"tinybench": "^3.0.3",
"tslib": "^2.8.1",
"typescript": "^5.6.3",
Expand Down
13 changes: 13 additions & 0 deletions src/lib/string.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
ensure_end,
deindent,
count_graphemes,
strip_ansi,
} from '$lib/string.js';

/* test__truncate */
Expand Down Expand Up @@ -366,3 +367,15 @@ test__count_graphemes('counts graphemes of a string, where compound emoji are on

test__count_graphemes.run();
/* test__count_graphemes */

/* test__strip_ansi */
const test__strip_ansi = suite('strip_ansi');

test__strip_ansi('counts graphemes of a string, where compound emoji are one grapheme', () => {
assert.is(strip_ansi('\x1B[31mred text\x1B[0m'), 'red text');
assert.is(strip_ansi(' \x1B[1;33;40m Yellow on black \x1B[0m '), ' Yellow on black ');
assert.is(strip_ansi('/src/'), '/src/');
});

test__strip_ansi.run();
/* test__strip_ansi */
5 changes: 5 additions & 0 deletions src/lib/string.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,8 @@ export const plural = (count: number | undefined | null, suffix = 's'): string =
*/
export const count_graphemes = (str: string): number =>
count_iterator(new Intl.Segmenter().segment(str));

/**
* Strips ANSI escape sequences from a string
*/
export const strip_ansi = (str: string): string => str.replaceAll(/\x1B\[[0-9;]*[a-zA-Z]/g, ''); // eslint-disable-line no-control-regex
7 changes: 4 additions & 3 deletions src/routes/package.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,16 @@ export const package_json = {
'@ryanatkn/gro': '^0.146.2',
'@ryanatkn/moss': '^0.20.2',
'@sveltejs/adapter-static': '^3.0.6',
'@sveltejs/kit': '^2.7.5',
'@sveltejs/kit': '^2.8.0',
'@sveltejs/package': '^2.3.7',
'@sveltejs/vite-plugin-svelte': '^4.0.0',
'@types/node': '^22.9.0',
eslint: '^9.14.0',
'eslint-plugin-svelte': '^2.46.0',
prettier: '^3.3.3',
'prettier-plugin-svelte': '^3.2.7',
svelte: '^5.1.9',
'svelte-check': '^4.0.5',
svelte: '^5.1.12',
'svelte-check': '^4.0.6',
tinybench: '^3.0.3',
tslib: '^2.8.1',
typescript: '^5.6.3',
Expand Down Expand Up @@ -378,6 +378,7 @@ export const src_json = {
{name: 'deindent', kind: 'function'},
{name: 'plural', kind: 'function'},
{name: 'count_graphemes', kind: 'function'},
{name: 'strip_ansi', kind: 'function'},
],
},
'./throttle.js': {
Expand Down

0 comments on commit ffb7587

Please sign in to comment.