All files / utils rot13.ts

100% Statements 12/12
100% Branches 2/2
100% Functions 2/2
100% Lines 10/10

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 22 23 24 25 2624x 24x 24x 1248x 1248x                           17x 17x 277x 277x   17x    
const input = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'.split('')
const output = 'NOPQRSTUVWXYZABCDEFGHIJKLMnopqrstuvwxyzabcdefghijklm'.split('')
const lookup = input.reduce<Record<string, string>>((m, k, i) => {
  m[k] = output[i]
  return m
}, Object.create(null))
 
/**
 * 回转 13 位替换式密码。
 *
 * @param str 原文
 * @see https://zh.wikipedia.org/wiki/ROT13
 * @example
 * ```typescript
 * rot13('hello world') // => 'uryyb jbeyq'
 * ```
 */
export function rot13(str: string): string {
  let res = ''
  for (let i = 0, len = str.length; i < len; i++) {
    const char = str.charAt(i)
    res += lookup[char] || char
  }
  return res
}