string width and ansi stripping: working versions (tested 2026-10-16)
Measure the visual width of ASCII, CJK, and emoji strings and strip ANSI escape codes with string-width and strip-ansi, asserting widths and cleaned output offline.
passed6 passed · 0 failed2026-10-16 19:54 UTC- Status
- passed
- Last passed
- 2026-10-16 19:54 UTC
- Tests
- 6 / 6
- Language
- TypeScript
- Runtime
- node v22.23.3
pinned versions4
string-width@8.3.0strip-ansi@7.2.0@types/node@26.6.5tsx@4.23.15
run it
npm install --no-audit --no-fund --ignore-scripts=false npm test
test environment
node v22.23.3 linux 6.17.0-1022-azure x64 runner: github-actions 38958328921
The code
Exactly the files that passed. Your agent can pull them with get_recipe("string-width-strip-ansi") (install), or see them on GitHub.
package.json
{
"name": "string-width-strip-ansi",
"private": true,
"type": "module",
"scripts": {
"test": "node --import tsx --test test/*.test.ts"
},
"dependencies": {
"string-width": "8.3.0",
"strip-ansi": "7.2.0"
},
"devDependencies": {
"@types/node": "26.6.5",
"tsx": "4.23.15"
}
}
src/width.ts
import stringWidth from "string-width";
import stripAnsi from "strip-ansi";
export function visibleWidth(input: string): number {
return stringWidth(input);
}
export function strip(input: string): string {
return stripAnsi(input);
}
export const RED = "\u001b[31m";
export const BOLD = "\u001b[1m";
export const RESET = "\u001b[0m";
export function colorize(text: string, codes: string[]): string {
return codes.join("") + text + RESET;
}
test/width.test.ts
import { test } from "node:test";
import assert from "node:assert/strict";
import stringWidth from "string-width";
import stripAnsi from "strip-ansi";
import { RED, BOLD, RESET, visibleWidth, strip, colorize } from "../src/width.ts";
test("measures ASCII width as character count", () => {
assert.equal(visibleWidth("hello"), 5);
assert.equal(visibleWidth(""), 0);
});
test("measures CJK wide characters as two columns each", () => {
assert.equal(visibleWidth("你好"), 4);
assert.equal(visibleWidth("こんにちは"), 10);
});
test("measures emoji as two columns", () => {
assert.equal(visibleWidth("👍"), 2);
assert.equal(visibleWidth("a👍b"), 4);
});
test("ignores ANSI escape codes when measuring width", () => {
const colored = colorize("hello", [RED]);
assert.equal(visibleWidth(colored), 5);
assert.equal(stringWidth(colored), 5);
});
test("strips ANSI codes to plain text", () => {
const colored = colorize("hello", [BOLD, RED]);
assert.equal(colored, `${BOLD}${RED}hello${RESET}`);
assert.equal(strip(colored), "hello");
assert.equal(stripAnsi(colored), "hello");
});
test("strip keeps wide characters and emoji intact", () => {
const mixed = colorize("你好👍", [RED]);
assert.equal(strip(mixed), "你好👍");
assert.equal(visibleWidth(strip(mixed)), 6);
});
Connect to ProvenPin
Add the free MCP server so your agent can pull this recipe and others like it. Same setup as the docs.
terminal
claude mcp add --transport http provenpin https://mcp.provenpin.com/mcp
~/.cursor/mcp.json
{ "mcpServers": { "provenpin": { "url": "https://mcp.provenpin.com/mcp" } } }.vscode/mcp.json
{ "servers": { "provenpin": { "type": "http", "url": "https://mcp.provenpin.com/mcp" } } }~/.codeium/windsurf/mcp_config.json
{ "mcpServers": { "provenpin": { "serverUrl": "https://mcp.provenpin.com/mcp" } } }MCP settings
{ "mcpServers": { "provenpin": { "type": "streamableHttp", "url": "https://mcp.provenpin.com/mcp" } } }Any other MCP client: Streamable HTTP at https://mcp.provenpin.com/mcp. No API key, no signup.