-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.js
42 lines (34 loc) · 1.02 KB
/
utils.js
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
"use strict";
const lowLetters = 'abcdefghijklmnopqrstuvwxyz';
const upLetters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
const count = upLetters.length;
module.exports.cipher = function cipher (input, key) {
const obj = {};
for(let i = 0; i < count; i++) {
let j = i + key;
if(j >= count) j = j - count;
obj[lowLetters[i]] = lowLetters[j]
obj[upLetters[i]] = upLetters[j]
}
let encodedText = ''
for (let char of input) {
encodedText += obj[char] ? obj[char] : char;
}
console.log('encodedText ', encodedText);
return encodedText;
}
exports.decoder = function decoder(input, key) {
const obj = {};
for(let i = 0; i < count; i++) {
let j = i + key;
if(j >= count) j = j - count;
obj[lowLetters[i]] = lowLetters[j]
obj[upLetters[i]] = upLetters[j]
}
let decodedText = ''
for (let char of input) {
decodedText += obj[char] ? obj[char] : char;
}
console.log('decodedText ', decodedText);
return decodedText;
}