Files
bun.sh/test/js/node/readline/getStringWidth.test.ts
Jarred Sumner 1d684e0d4f Tweak formatting of console.table (#7927)
* Tweak alignment of console.table

* " " empty string instead of "#"

* Fix assertion failure

* Only get the length when necessary

* Avoid stale copies

* Add `asUTF8` helper to `bun.String`

* Cautiously handle recursion

* Further tweaks to output

* output

* Add native implementation of `getStringWidth`

* [autofix.ci] apply automated fixes

* If its not a string then quote it by default

* Add snapshot for headers

* Make it easier to debug when a builtin throws an exception

* This must be hoisted

* Fix bugs in `getStringWidth`

* Update getStringWidth.test.ts

* Eager load it

---------

Co-authored-by: Jarred Sumner <709451+Jarred-Sumner@users.noreply.github.com>
Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
2023-12-31 02:38:52 -08:00

38 lines
1.1 KiB
TypeScript

import readline from "node:readline";
var {
utils: { getStringWidth },
// @ts-ignore
} = readline[Symbol.for("__BUN_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED__")];
it("handles invisible ASCII character at any position", () => {
const visible = "a";
const invisible = String.fromCharCode(3);
for (let i = 0; i < 48; i++) {
const str = visible.repeat(i) + invisible + visible.repeat(48 - i);
expect(getStringWidth(str)).toBe(48);
}
});
it("handles visible ASCII character at any position", () => {
const visible = "a";
const invisible = String.fromCharCode(3);
for (let i = 0; i < 48; i++) {
const str = invisible.repeat(i) + visible + invisible.repeat(48 - i);
expect(getStringWidth(str)).toBe(1);
}
});
it("handles alternating characters", () => {
// In node, this is `process.binding("icu").getStringWidth`
expect(getStringWidth("あ")).toBe(2);
expect(getStringWidth("'あ")).toBe(3);
expect(getStringWidth("ああ")).toBe(4);
expect(getStringWidth("あああ")).toBe(6);
expect(getStringWidth("'あああ")).toBe(7);
expect(getStringWidth('"あああ')).toBe(7);
expect(getStringWidth('"あああ"')).toBe(8);
});