SOURCE

function getBytes(value: string): number {
  return value.replace(/[^\x00-\xff]/g, '--').length
}

function isChinese(char: string): boolean {
  return /[^\x00-\xff]/g.test(char)
}

function getPrefix(value: string, length: number): string {
  let len = length
  let res = ''
  for (const char of value) {
    if (isChinese(char)) {
      len -= 2
    } else {
      len -= 1
    }
    res = res.concat(char)
    if (len <= 0) {
      return res
    }
  }
  return value
}
console.log(getPrefix(
            'te'.slice(0, Math.floor(length / 2)),
            Math.floor(length / 2)
          )
console 命令行工具 X clear

                    
>
console