Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save lzghzr/edfcb9c66cc631923f3533e19c2940eb to your computer and use it in GitHub Desktop.
Save lzghzr/edfcb9c66cc631923f3533e19c2940eb to your computer and use it in GitHub Desktop.
const userName = data['info'][2][1]
const msg = data['info'][1]
// 为了正确判断长度需要使用正则u模式
const uN = userName.match(/./gu)
// 正常来说应该使用如下代码判断宽字体
// const uW = userName.match(/\p{East_Asian_Width=Wide}/gu)
// 但是由于JavaScript目前不支持此方法, 所以用以下代码代替, 并不完美
const uW = userName.match(/[^\u{0}-\u{2e7f}\u{ff61}-\u{1ffff}]/gu)
// 再次加宽字体长度可以使其变为两倍
const num = (uN === null ? 0 : uN.length) + (uW === null ? 0 : uW.length)
// 3 * 8最多可兼容24个宽度, 一般用户名不会这么长, 当然也有超长的
console.log(userName, ''.padEnd(3 - Math.floor((num + 1) / 8), '\t'), msg)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment