'use strict';const stripAnsi = require('strip-ansi');const isFullwidthCodePoint = require('is-fullwidth-code-point');const emojiRegex = require('emoji-regex');const stringWidth = string => {if (typeof string !== 'string' || string.length === 0) {return 0;}string = stripAnsi(string);if (string.length === 0) {return 0;}string = string.replace(emojiRegex(), '');let width = 0;for (let i = 0; i < string.length; i++) {const code = string.codePointAt(i);// Ignore control charactersif (code <= 0x1F || (code >= 0x7F && code <= 0x9F)) {continue;}// Ignore combining charactersif (code >= 0x300 && code <= 0x36F) {continue;}// Surrogatesif (code > 0xFFFF) {i++;}width += isFullwidthCodePoint(code) ? 2 : 1;}return width;};module.exports = stringWidth;// TODO: remove this in the next major versionmodule.exports.default = stringWidth;
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。