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 string = string.replace(emojiRegex(), ' ');
9 if (typeof string !== 'string' || string.length === 0) {
13 string = stripAnsi(string);
17 for (let i = 0; i < string.length; i++) {
18 const code = string.codePointAt(i);
20 // Ignore control characters
21 if (code <= 0x1F || (code >= 0x7F && code <= 0x9F)) {
25 // Ignore combining characters
26 if (code >= 0x300 && code <= 0x36F) {
35 width += isFullwidthCodePoint(code) ? 2 : 1;
41 module.exports = stringWidth;
42 // TODO: remove this in the next major version
43 module.exports.default = stringWidth;