All files / utils toHalfWidthString.ts

100% Statements 10/10
100% Branches 6/6
100% Functions 1/1
100% Lines 9/9

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21              6x 6x 59x 59x 47x 12x 1x   11x     6x    
/**
 * 转换为半角字符串。
 *
 * @param value 要转换的字符串
 * @returns 返回转换后的半角字符串
 */
export function toHalfWidthString(value: string): string {
  let result = ''
  for (let i = 0; i < value.length; i++) {
    const charCode = value.charCodeAt(i)
    if (charCode >= 0xff01 && charCode <= 0xff5e) {
      result += String.fromCharCode(charCode - 0xfee0)
    } else if (0x3000 === charCode) {
      result += String.fromCharCode(0x0020)
    } else {
      result += value[i]
    }
  }
  return result
}