2 const stripAnsi = require('strip-ansi');
3 const isFullwidthCodePoint = require('is-fullwidth-code-point');
4 const emojiRegex = require('emoji-regex');
6 const stringWidth = string => {
7 if (typeof string !== 'string' || string.length === 0) {
11 string = stripAnsi(string);
13 if (string.length === 0) {
17 string = string.replace(emojiRegex(), ' ');
21 for (let i = 0; i < string.length; i++) {
22 const code = string.codePointAt(i);
24 // Ignore control characters
25 if (code <= 0x1F || (code >= 0x7F && code <= 0x9F)) {
29 // Ignore combining characters
30 if (code >= 0x300 && code <= 0x36F) {
39 width += isFullwidthCodePoint(code) ? 2 : 1;
45 module.exports = stringWidth;
46 // TODO: remove this in the next major version
47 module.exports.default = stringWidth;