-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrot8DecodeStream.js
More file actions
37 lines (30 loc) · 871 Bytes
/
Copy pathrot8DecodeStream.js
File metadata and controls
37 lines (30 loc) · 871 Bytes
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
const isEnglishLetter = require('./isEnglishLetter');
const stream = require('stream');
class Rot8DecodeStream extends stream.Transform {
constructor() {
super();
}
_transform(chunk, encoding, callback) {
const str = chunk.toString().trim();
const cipher = str
.split('')
.map((x) => this.#rot8CipherDecode(x))
.join('');
this.push(cipher);
callback();
}
#rot8CipherDecode(char) {
if (isEnglishLetter(char)) {
const charCode = char.charCodeAt(0);
let newCharCode = charCode - 8;
if (56 < newCharCode && newCharCode < 65) {
newCharCode = 91 - (65 - newCharCode);
} else if (88 < newCharCode && newCharCode < 97) {
newCharCode = 123 - (97 - newCharCode);
}
return String.fromCharCode(newCharCode);
}
return char;
}
}
module.exports = Rot8DecodeStream;