-
Notifications
You must be signed in to change notification settings - Fork 0
/
cipher.js
44 lines (41 loc) · 953 Bytes
/
cipher.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
43
44
class Cipher {
/**
* Ciphers a string using the Caesar cipher technique
*
* @static
* @param {string} plainText Text to cipher
* @returns {string} Caesar ciphered text
* @memberof Cipher
*/
static caesar(plainText) {
return plainText
.split('')
.map(
char => Cipher._getChar(Cipher._getCharCodeOffsetted(char, 1)))
.join('');
}
/**
* Get char code offseted
*
* @private
* @param {string} char
* @param {number} offset
* @returns {number}
* @memberof Cipher
*/
static _getCharCodeOffsetted(char, offset = 1) {
return char.match(/[A-Za-z]/) ? char.charCodeAt(0) + offset : char;
}
/**
* Get char from charcode
*
* @private
* @param {number} charCode
* @returns {string}
* @memberof Cipher
*/
static _getChar(charCode) {
return typeof charCode === 'number' ? String.fromCharCode(charCode) : charCode;
}
}
module.exports = Cipher;