Skip to content

Manchester encoding and decoding #1135

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 68 additions & 0 deletions src/core/operations/ManchesterDecode.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/**
* @author mt3571 [[email protected]]
* @copyright Crown Copyright 2020
* @license Apache-2.0
*/

import Operation from "../Operation.mjs";
import OperationError from "../errors/OperationError.mjs";

/**
* Manchester decoding operation
*/
class ManchesterDecode extends Operation {

/**
* ManchesterDecode constructor
*/
constructor() {
super();

this.name = "Manchester Decode";
this.module = "Encodings";
this.description = "Decodes data that has been encoded using the Manchester Encoding (also known as phase encoding). A <code>01</code> is converted to <code>1</code> and a <code>10</code> is converted to <code>0</code>. <br><br>As every bit is encoded into two bits when using this encoding, inputs must be a multiple of 2 long.";
this.infoURL = "https://en.wikipedia.org/wiki/Manchester_code";
this.inputType = "string";
this.outputType = "string";
this.args = [];
this.checks = [
{
pattern: "(01|10)*",
flags: "",
args: []
}
];
}

/**
* @param {string} input
* @param {Object[]} args
* @returns {string}
*/
run(input, args) {
const decoding = [];

if (input.length % 2 != 0){

Check failure on line 45 in src/core/operations/ManchesterDecode.mjs

View workflow job for this annotation

GitHub Actions / main

Expected '!==' and instead saw '!='

Check failure on line 45 in src/core/operations/ManchesterDecode.mjs

View workflow job for this annotation

GitHub Actions / main

Missing space before opening brace
throw new OperationError(`Length of an input should be a multiple of 2, the input is ${input.length} long.`);
}

for (let i = 0; i < input.length; i +=2){

Check failure on line 49 in src/core/operations/ManchesterDecode.mjs

View workflow job for this annotation

GitHub Actions / main

Missing space before opening brace
const bit1 = input[i];
const bit2 = input[i+1];

if (bit1 == 1 && bit2 == 0){

Check failure on line 53 in src/core/operations/ManchesterDecode.mjs

View workflow job for this annotation

GitHub Actions / main

Expected '===' and instead saw '=='

Check failure on line 53 in src/core/operations/ManchesterDecode.mjs

View workflow job for this annotation

GitHub Actions / main

Expected '===' and instead saw '=='

Check failure on line 53 in src/core/operations/ManchesterDecode.mjs

View workflow job for this annotation

GitHub Actions / main

Missing space before opening brace
decoding.push(0);
} else if (bit1 == 0 && bit2 == 1){

Check failure on line 55 in src/core/operations/ManchesterDecode.mjs

View workflow job for this annotation

GitHub Actions / main

Expected '===' and instead saw '=='

Check failure on line 55 in src/core/operations/ManchesterDecode.mjs

View workflow job for this annotation

GitHub Actions / main

Expected '===' and instead saw '=='

Check failure on line 55 in src/core/operations/ManchesterDecode.mjs

View workflow job for this annotation

GitHub Actions / main

Missing space before opening brace
decoding.push(1);
} else {
throw new OperationError(`Invalid input.`);
}

}
const output = decoding.join("");
return output;
}

}

export default ManchesterDecode;
58 changes: 58 additions & 0 deletions src/core/operations/ManchesterEncode.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,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 ++){

Check failure on line 38 in src/core/operations/ManchesterEncode.mjs

View workflow job for this annotation

GitHub Actions / main

Missing space before opening brace
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;
Loading