display-width
Advanced tools
Comparing version 1.0.0 to 1.1.0
13
index.js
@@ -13,3 +13,3 @@ import { table, wtable, embtable, embtableLen } from "./tables.js"; | ||
} | ||
return _wcwidth(uwc.codePointAt(0), table, wtable); | ||
return _wcwidth(uwc.codePointAt(0)); | ||
} | ||
@@ -68,3 +68,3 @@ | ||
} | ||
w = isEmojiModifier(wc_last, wc) ? 2 - w : _wcwidth(wc, table, wtable); // Emoji Modifier promotes width to 2 | ||
w = isEmojiModifier(wc_last, wc) ? 2 - w : _wcwidth(wc); // Emoji Modifier promotes width to 2 | ||
if (w === -1) { | ||
@@ -92,3 +92,3 @@ return -1; | ||
// Identify emoji modifier base per UTS #51 using the _EMBTABLE bitmap | ||
// Identify emoji modifier base per UTS #51 using the embtable bitmap | ||
function isEmojiModifier(wcLast, wc) { | ||
@@ -117,2 +117,4 @@ if (wc < 0x1f3fb || wc > 0x1f3ff) { | ||
* | ||
* derived from musl: https://github.com/kraj/musl/blob/ffb23aef/src/ctype/wcwidth.c#L11-L29 | ||
* | ||
* @param {number} uwc | ||
@@ -138,7 +140,8 @@ * @return {number} | ||
if (wc - 0x20000 < 0x20000) { | ||
if (wc >= 0x20000 && wc < 0x40000) { | ||
return 2; | ||
} | ||
if (wc === 0xe0001 || wc - 0xe0020 <= 0x5f || wc - 0xe0100 < 0xef) { | ||
if (wc === 0xe0001 || (wc >= 0xe0020 && wc <= 0xe007f) | ||
|| (wc >= 0xe0100 && wc < 0xe01ef)) { | ||
return 0; | ||
@@ -145,0 +148,0 @@ } |
{ | ||
"description": "A node.js port of [uwcwidth](https://github.com/Z4JC/uwcwidth), a fast function for determining a string's display width", | ||
"homepage": "https://github.com/llimllib/display-width", | ||
"keywords": [ | ||
"string", | ||
"character", | ||
"unicode", | ||
"width", | ||
"visual", | ||
"column", | ||
"columns", | ||
"fullwidth", | ||
"full-width", | ||
"full", | ||
"ansi", | ||
"escape", | ||
"codes", | ||
"terminal", | ||
"console", | ||
"cjk", | ||
"chinese", | ||
"japanese", | ||
"korean", | ||
"fixed-width", | ||
"string-width" | ||
], | ||
"license": "MIT", | ||
@@ -10,3 +34,3 @@ "main": "index.js", | ||
"type": "module", | ||
"version": "1.0.0" | ||
"version": "1.1.0" | ||
} |
@@ -5,2 +5,4 @@ # display-width | ||
That code is a port of POSIX [wcswidth](https://pubs.opengroup.org/onlinepubs/9699919799.2016edition/functions/wcswidth.html), a standard function for counting the number of columns occupied by a string, and is derived from [musl libc](https://github.com/kraj/musl/blob/ffb23aef/src/ctype/wcwidth.c#L11-L29) | ||
## How to use it | ||
@@ -33,4 +35,15 @@ | ||
yes! | ||
yes! Compared to [string-width](https://www.npmjs.com/package/string-width), it's about **33x** faster | ||
``` | ||
┌─────────┬─────────────────┬───────────┬────────────────────┬───────────┬─────────┐ | ||
│ (index) │ Task Name │ ops/sec │ Average Time (ns) │ Margin │ Samples │ | ||
├─────────┼─────────────────┼───────────┼────────────────────┼───────────┼─────────┤ | ||
│ 0 │ 'display-width' │ '188,426' │ 5307.115321339439 │ '±1.55%' │ 18843 │ | ||
│ 1 │ 'string-width' │ '5,586' │ 179008.73345259373 │ '±11.73%' │ 559 │ | ||
└─────────┴─────────────────┴───────────┴────────────────────┴───────────┴─────────┘ | ||
``` | ||
see the [bench directory](https://github.com/llimllib/display-width/tree/main/bench) for details | ||
## Is it large? | ||
@@ -37,0 +50,0 @@ |
56
test.js
@@ -44,2 +44,18 @@ import { before, after, describe, it } from "node:test"; | ||
emFitzpatrickType5: ["\u{1f3fe}", 2], | ||
// CJK ideographs are 20000-3ffff | ||
qiang: ["\u{2b017}", 2], | ||
// TIP, seal script | ||
seal_32477: ["\u{32477}", 2], | ||
// unassigned codepoints should default to 1, | ||
// likely for forward compatibility | ||
unassigned_plane_4: ["\u{40000}", 1], | ||
unassigned_plane_d: ["\u{d0000}", 1], | ||
unassigned_plane_e: ["\u{e0002}", 1], | ||
// musl code insists on doing this even in the tags plane for unassigned: | ||
// (should we just default to everything in 0xe0000-0xeffff as invisible?) | ||
tag_unassigned: ["\u{e0080}", 1], | ||
// but assigned codepoints in the tags code block should be invisible | ||
tag_begin: ["\u{e0001}", 0], | ||
tag_space: ["\u{e0020}", 0], | ||
tag_end: ["\u{e007f}", 0] | ||
}; | ||
@@ -87,4 +103,10 @@ | ||
// non-printables | ||
tab: ["Hi\tthere", -1], // wut why | ||
esc: ["There is an \x1b", -1], // why | ||
// | ||
// > The wcswidth() function shall return -1 if any of the first n | ||
// wide-character codes in the wide-character string is not a printable | ||
// wide-character code. | ||
// | ||
// https://pubs.opengroup.org/onlinepubs/9699919799.2016edition/functions/wcswidth.html | ||
tab: ["Hi\tthere", -1], | ||
esc: ["There is an \x1b", -1], | ||
one: ["\x01", -1], | ||
@@ -190,2 +212,32 @@ zero: ["\x00", 0], | ||
], | ||
// from string-width | ||
// sindorhesus has this as two, but I believe one is more correct. How to be sure? | ||
heavyCircleWithStrokeAndTwoDots: ["\u25e3", 1], | ||
simple: ["abcde", 5], | ||
cjk1: ["古池や", 6], | ||
cjk2: ["あいうabc", 9], | ||
cjk3: ["あいう★", 7], | ||
plusminus: ["±", 1], | ||
katakana: ["ノード.js", 9], | ||
chinese: ["你好", 4], | ||
korean: ["안녕하세요", 10], | ||
surrogate: ["A\uD83C\uDE00BC", 5], | ||
escapes: ["\u001B[31m\u001B[39m", -1], | ||
emojiPresentationCharacter: ["\u{231A}", 2], | ||
emojiEitherWay: ["\u{2194}\u{FE0F}", 2], | ||
emojiModifierBase: ["\u{1F469}", 2], | ||
emojiModifierBaseAndModifier: ["\u{1F469}\u{1F3FF}", 2], | ||
variationSelectors: ["\u{845B}\u{E0100}", 2], | ||
thai: ["ปฏัก", 3], | ||
thaiCombiningChar: ["_\u0E34", 1], | ||
// sindorhesus has this as two for no reason I can tell? | ||
fancyQuote: ["“", 1], | ||
// Mitchell seems to say that this character (🧑🌾) shows a place | ||
// where wcswidth isn't correct and gives 4 instead of 2; however we return | ||
// 2. I don't quite understand what's going on here | ||
// | ||
// I even wrote a C program to use my systems' wcswidth and it returned 2 | ||
// | ||
// https://mitchellh.com/writing/grapheme-clusters-in-terminals | ||
farmer: ["\u{1f9d1}\u200d\u{1f33e}", 2], | ||
}; | ||
@@ -192,0 +244,0 @@ |
Native code
Supply chain riskContains native code (e.g., compiled binaries or shared libraries). Including native code can obscure malicious behavior.
Found 1 instance in 1 package
Mixed license
License(Experimental) Package contains multiple licenses.
Found 1 instance in 1 package
No website
QualityPackage does not have a website.
Found 1 instance in 1 package
2252970
16
859
0
60
1
1