forked from OKEAMAH/prettier
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathget-string-width.js
More file actions
48 lines (38 loc) · 1.32 KB
/
get-string-width.js
File metadata and controls
48 lines (38 loc) · 1.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import emojiRegex from "emoji-regex";
// @ts-expect-error -- Special export for us, https://github.com/sindresorhus/get-east-asian-width/pull/6
import { _isNarrowWidth as isNarrowWidth } from "get-east-asian-width";
const notAsciiRegex = /[^\x20-\x7F]/u;
// Similar to https://github.com/sindresorhus/string-width
// We don't strip ansi, always treat ambiguous width characters as having narrow width.
/**
* @param {string} text
* @returns {number}
*/
function getStringWidth(text) {
if (!text) {
return 0;
}
// shortcut to avoid needless string `RegExp`s, replacements, and allocations
if (!notAsciiRegex.test(text)) {
return text.length;
}
text = text.replace(emojiRegex(), " ");
let width = 0;
// Use `Intl.Segmenter` when we drop support for Node.js v14
// https://github.com/prettier/prettier/pull/14793#discussion_r1185840038
// https://github.com/sindresorhus/string-width/pull/47
for (const character of text) {
const codePoint = character.codePointAt(0);
// Ignore control characters
if (codePoint <= 0x1f || (codePoint >= 0x7f && codePoint <= 0x9f)) {
continue;
}
// Ignore combining characters
if (codePoint >= 0x300 && codePoint <= 0x36f) {
continue;
}
width += isNarrowWidth(codePoint) ? 1 : 2;
}
return width;
}
export default getStringWidth;