-
Notifications
You must be signed in to change notification settings - Fork 3.5k
/
Copy pathManchesterEncode.mjs
58 lines (48 loc) · 1.5 KB
/
ManchesterEncode.mjs
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
52
53
54
55
56
57
58
/**
* @author mt3571 [[email protected]]
* @copyright Crown Copyright 2020
* @license Apache-2.0
*/
import Operation from "../Operation.mjs";
import OperationError from "../errors/OperationError.mjs";
/**
* Manchester encoding operation
*/
class ManchesterEncode extends Operation {
/**
* ManchesterEncode constructor
*/
constructor() {
super();
this.name = "Manchester Encode";
this.module = "Encodings";
this.description = "Performs the Manchester encoding on the data (also known as phase encoding). A <code>1</code> is converted to <code>01</code> and a <code>0</code> is converted to <code>10</code>. ";
this.infoURL = "https://en.wikipedia.org/wiki/Manchester_code";
this.inputType = "string";
this.outputType = "string";
this.args = [];
}
/**
* @param {string} input
* @param {Object[]} args
* @returns {string}
*/
run(input, args) {
const encoding = [];
for (let i = 0; i < input.length; i ++){
const bit = input[i];
if (bit == 0){
encoding.push(1);
encoding.push(0);
} else if (bit == 1){
encoding.push(0);
encoding.push(1);
} else {
throw new OperationError(`Invalid input character ${bit}. Input should be in binary.`);
}
}
const output = encoding.join("");
return output;
}
}
export default ManchesterEncode;