Skip to main content

@std/console@0.215.0

latest
Works with
It is unknown whether this package works with Browsers, Deno, Node.js, Cloudflare Workers, Bun
It is unknown whether this package works with Browsers
It is unknown whether this package works with Deno
It is unknown whether this package works with Node.js
It is unknown whether this package works with Cloudflare Workers
It is unknown whether this package works with Bun
JSR Score70%
Published2 years ago (0.215.0)
function unicodeWidth
unicodeWidth(str: string): number

Get the width of a string's constituent characters in columns in TTY-like environments.

Combine with stripAnsiCode from fmt/colors.ts to get the expected physical width of a string in the console.

Examples

Example 1

import { unicodeWidth } from "@std/console/unicode_width";
import { assertEquals } from "@std/assert/assert_equals";
import { stripAnsiCode } from "@std/fmt/colors";

assertEquals(unicodeWidth("hello world"), 11);
assertEquals(unicodeWidth("天地玄黃宇宙洪荒"), 16);
assertEquals(unicodeWidth("fullwidth"), 18);
assertEquals(unicodeWidth(stripAnsiCode("\x1b[36mголубой\x1b[39m")), 7);
assertEquals(unicodeWidth(stripAnsiCode("\x1b[31m紅色\x1b[39m")), 4);
assertEquals(unicodeWidth(stripAnsiCode("\x1B]8;;https://deno.land\x07🦕\x1B]8;;\x07")), 2);

Parameters

Return Type

Report package

Please provide a reason for reporting this package. We will review your report and take appropriate action.

Please review the JSR usage policy before submitting a report.

Add Package

deno add jsr:@std/console

Import symbol

import { unicodeWidth } from "@std/console/unicode_width";
or

Import directly with a jsr specifier

import { unicodeWidth } from "jsr:@std/console/unicode_width";