-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.ts
More file actions
51 lines (50 loc) · 2.19 KB
/
Copy pathapp.ts
File metadata and controls
51 lines (50 loc) · 2.19 KB
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
const aCodeAscii: number = 'a'.charCodeAt(0);
const zCodeAscii: number = 'z'.charCodeAt(0);
const nEnglishLetters: number = zCodeAscii - aCodeAscii + 1;
function shiftCipher(str: string, shift: number = 1): string {
return cipherDecipher(str, shift, mapperCipher);
}
function shiftDecipher(str: string, shift: number = 1) : string {
return cipherDecipher(str, shift, mapperDecipher);
}
type MapperFunction = (symb: string, shift: number) => string;
function cipherDecipher(str: string, shift: number,
mapperFun: MapperFunction): string {
//const arStr: string[] = Array.from(str);
const arStr: Array<string> = Array.from(str);
const arRes: Array<string> = arStr.map(symb => {
let res: string = symb;
if (symb <= 'z' && symb >= 'a') {
res = mapperFun(symb, shift);
}
return res;
})
return arRes.join('');
}
function mapperCipher(symb: string, shift: number) : string {
const actualShift: number = (symb.charCodeAt(0) - aCodeAscii + shift) % nEnglishLetters;
return String.fromCharCode(aCodeAscii + actualShift);
}
function mapperDecipher(symb: string, shift: number): string {
const actualShift: number = (zCodeAscii - symb.charCodeAt(0) + shift) % nEnglishLetters;
return String.fromCharCode(zCodeAscii - actualShift);
}
type TestObj = {
str: string,
shift?: number
}
function testCipherDecipher(data: Array<TestObj>,
testName: string): void{
console.log(`${"*".repeat(10)}${testName}${"*".repeat(10)}`)
const funForTest: MapperFunction
= testName === "cipherTest" ? shiftCipher : shiftDecipher;
data.forEach((obj => console.log(`str=${obj.str}, shift=${obj.shift || 1} => ${funForTest(obj.str, obj.shift)}`)))
}
const dataForCipherTest: Array<TestObj> = [
{str: "abc"}, {str: "abz", shift: 1000}
];
testCipherDecipher(dataForCipherTest, "cipherTest");
const dataForDecipherTest: Array<TestObj> = [
{str: "bcd"}, {str: "mnl", shift: 1000}
];
testCipherDecipher(dataForDecipherTest, "decipherTest");