@nxmix/string-visual-width
Version:
Get the visual width of the string
24 lines • 835 B
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
// Packages
const emoji_seq_match_1 = require("@nxmix/emoji-seq-match");
const is_full_width_1 = require("@nxmix/is-full-width");
const strip_ansi_1 = require("strip-ansi");
const stringWidth = (str) => {
let visuelWidth = 0;
const arr = [...strip_ansi_1.default(str).normalize()];
for (let i = 0; i < arr.length; i++) {
const matchedLength = emoji_seq_match_1.default(arr, i);
if (matchedLength > 0) {
i += matchedLength - 1;
// Jonied emojis are counted as 2
visuelWidth += 2;
}
else {
visuelWidth += is_full_width_1.default(arr[i].codePointAt(0)) ? 2 : 1;
}
}
return visuelWidth;
};
exports.default = stringWidth;
//# sourceMappingURL=index.js.map
;