From ffb7587d1dd845795d79ad315cf8e9f4cfe12bde Mon Sep 17 00:00:00 2001 From: Ryan Atkinson Date: Fri, 8 Nov 2024 11:59:41 -0700 Subject: [PATCH] add `strip_ansi` string util --- .changeset/shiny-bugs-flow.md | 5 +++++ package-lock.json | 24 ++++++++++++------------ package.json | 6 +++--- src/lib/string.test.ts | 13 +++++++++++++ src/lib/string.ts | 5 +++++ src/routes/package.ts | 7 ++++--- 6 files changed, 42 insertions(+), 18 deletions(-) create mode 100644 .changeset/shiny-bugs-flow.md diff --git a/.changeset/shiny-bugs-flow.md b/.changeset/shiny-bugs-flow.md new file mode 100644 index 00000000..b2943586 --- /dev/null +++ b/.changeset/shiny-bugs-flow.md @@ -0,0 +1,5 @@ +--- +"@ryanatkn/belt": patch +--- + +add `strip_ansi` string util diff --git a/package-lock.json b/package-lock.json index b6074c07..8cb9cc0b 100644 --- a/package-lock.json +++ b/package-lock.json @@ -15,7 +15,7 @@ "@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", @@ -23,8 +23,8 @@ "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", @@ -579,9 +579,9 @@ } }, "node_modules/@sveltejs/kit": { - "version": "2.7.5", - "resolved": "https://registry.npmjs.org/@sveltejs/kit/-/kit-2.7.5.tgz", - "integrity": "sha512-8WIrVch2Ze2Rq3eIMPTqIIRFPM2zGQcGKHim2z43KVRdgdtYWBugAQ7nemH9ATnzlvbgztk6hwhEZOi8A8ZOPg==", + "version": "2.8.0", + "resolved": "https://registry.npmjs.org/@sveltejs/kit/-/kit-2.8.0.tgz", + "integrity": "sha512-HCiWupCuKJQ3aPaC4Xc6lpPdjOOnoGzEiYjOqMqppdtfGtY2ABrx932Vw66ZwS2RGXc0BmZvFvNq5SzqlmDVLg==", "dev": true, "hasInstallScript": true, "license": "MIT", @@ -2644,9 +2644,9 @@ } }, "node_modules/svelte": { - "version": "5.1.9", - "resolved": "https://registry.npmjs.org/svelte/-/svelte-5.1.9.tgz", - "integrity": "sha512-nzq+PPKGS2PoEWDjAcXSrKSbXmmmOAxd6dAz1IhRusUpVkFS6DMELWPyBPGwu6TpO/gsgtFXwX0M4+pAR5gzKw==", + "version": "5.1.12", + "resolved": "https://registry.npmjs.org/svelte/-/svelte-5.1.12.tgz", + "integrity": "sha512-U9BwbSybb9QAKAHg4hl61hVBk97U2QjUKmZa5++QEGoi6Nml6x6cC9KmNT1XObGawToN3DdLpdCs/Z5Yl5IXjQ==", "dev": true, "license": "MIT", "dependencies": { @@ -2669,9 +2669,9 @@ } }, "node_modules/svelte-check": { - "version": "4.0.5", - "resolved": "https://registry.npmjs.org/svelte-check/-/svelte-check-4.0.5.tgz", - "integrity": "sha512-icBTBZ3ibBaywbXUat3cK6hB5Du+Kq9Z8CRuyLmm64XIe2/r+lQcbuBx/IQgsbrC+kT2jQ0weVpZSSRIPwB6jQ==", + "version": "4.0.6", + "resolved": "https://registry.npmjs.org/svelte-check/-/svelte-check-4.0.6.tgz", + "integrity": "sha512-2XwmQNJaKbenJbvu5at+DuRpvF4v73Zu7f0/WkMl1O7WDm/IfF+E13t8D0nnRiRcMsNYm9ufHyLwfeMEnebpdg==", "dev": true, "license": "MIT", "dependencies": { diff --git a/package.json b/package.json index 908faa5b..9540b82c 100644 --- a/package.json +++ b/package.json @@ -59,7 +59,7 @@ "@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", @@ -67,8 +67,8 @@ "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", diff --git a/src/lib/string.test.ts b/src/lib/string.test.ts index 4cc8703e..4bd7ddce 100644 --- a/src/lib/string.test.ts +++ b/src/lib/string.test.ts @@ -12,6 +12,7 @@ import { ensure_end, deindent, count_graphemes, + strip_ansi, } from '$lib/string.js'; /* test__truncate */ @@ -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 */ diff --git a/src/lib/string.ts b/src/lib/string.ts index bbaf53e8..673792f6 100644 --- a/src/lib/string.ts +++ b/src/lib/string.ts @@ -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 diff --git a/src/routes/package.ts b/src/routes/package.ts index da654692..818c90b9 100644 --- a/src/routes/package.ts +++ b/src/routes/package.ts @@ -40,7 +40,7 @@ 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', @@ -48,8 +48,8 @@ export const package_json = { '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', @@ -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': {