-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrot13.ts
30 lines (29 loc) · 1008 Bytes
/
rot13.ts
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
26
27
28
29
30
// https://www.codewars.com/kata/52223df9e8f98c7aa7000062
function rot13(str: string): string {
const shiftCount = 13;
const alphabetLength = 26
const setCodePoints = (baseChar: string) => Array.from(
{ length: alphabetLength },
(_, i) => baseChar.codePointAt(0) as number + i
);
const upperCodePoints = setCodePoints('A');
const lowerCodePoints = setCodePoints('a');
const shiftChar = (codePoint: number, codePoints: number[]) => {
const index = codePoints.indexOf(codePoint);
const newIndex = (index + shiftCount) % alphabetLength;
return String.fromCodePoint(codePoints[newIndex]);
};
return str
.split('')
.map(char => {
const codePoint = char.codePointAt(0) as number;
if (upperCodePoints.includes(codePoint)) {
return shiftChar(codePoint, upperCodePoints);
} else if (lowerCodePoints.includes(codePoint)) {
return shiftChar(codePoint, lowerCodePoints);
} else {
return char;
}
})
.join('');
}