* @function ROT13 * @description - ROT13 ("rotate by 13 places", sometimes hyphenated ROT-13) is a simple letter substitution cipher that replaces a letter with the 13th letter after it in the alphabet. ROT13 is a special case of the Caesar cipher which was developed in ancient Rome. Because there
(str)
| 6 | * @return {String} decrypted string |
| 7 | */ |
| 8 | function ROT13(str) { |
| 9 | if (typeof str !== 'string') { |
| 10 | throw new TypeError('Argument should be string') |
| 11 | } |
| 12 | |
| 13 | return str.replace(/[a-z]/gi, (char) => { |
| 14 | const charCode = char.charCodeAt() |
| 15 | |
| 16 | if (/[n-z]/i.test(char)) { |
| 17 | return String.fromCharCode(charCode - 13) |
| 18 | } |
| 19 | |
| 20 | return String.fromCharCode(charCode + 13) |
| 21 | }) |
| 22 | } |
| 23 | |
| 24 | export default ROT13 |