ran the linter

This commit is contained in:
j83305 2020-06-02 11:27:29 +01:00
parent 44cf916f43
commit b842a38c84
2 changed files with 34 additions and 20 deletions

View file

@ -9,33 +9,42 @@
import OperationError from "../errors/OperationError.mjs"; import OperationError from "../errors/OperationError.mjs";
/**
* Function to take the user input and encode using the given arguments
* @param input string of binary
* @param args array
*/
export function calculateParityBit(input, args) { export function calculateParityBit(input, args) {
let count1s = 0; let count1s = 0;
for (let i = 0; i < input.length; i++){ for (let i = 0; i < input.length; i++) {
const character = input.charAt(i); const character = input.charAt(i);
if (character === "1"){ if (character === "1") {
count1s++; count1s++;
} else if (character !== args[3] && character !== "0" && character !== " "){ } else if (character !== args[3] && character !== "0" && character !== " ") {
throw new OperationError("unexpected character encountered: \"" + character + "\""); throw new OperationError("unexpected character encountered: \"" + character + "\"");
} }
} }
let parityBit = "1"; let parityBit = "1";
const flipflop = args[0] === "Even Parity" ? 0 : 1; const flipflop = args[0] === "Even Parity" ? 0 : 1;
if (count1s % 2 === flipflop){ if (count1s % 2 === flipflop) {
parityBit = "0"; parityBit = "0";
} }
if (args[1] === "End"){ if (args[1] === "End") {
return input + parityBit; return input + parityBit;
}else{ } else {
return parityBit + input; return parityBit + input;
} }
} }
// just removes the parity bit to return the original data /**
* just removes the parity bit to return the original data
* @param input string of binary, encoded
* @param args array
*/
export function decodeParityBit(input, args) { export function decodeParityBit(input, args) {
if (args[1] === "End"){ if (args[1] === "End") {
return input.slice(0, -1); return input.slice(0, -1);
}else{ } else {
return input.slice(1); return input.slice(1);
} }
} }

View file

@ -63,16 +63,21 @@ class ParityBit extends Operation {
* @returns {string} * @returns {string}
*/ */
run(input, args) { run(input, args) {
if (input.length === 0){ if (input.length === 0) {
return input; return input;
} }
/**
* determines weather to use the encode or decode method based off args[2]
* @param input input to be encoded or decoded
* @param args array
*/
const method = (input, args) => args[2] === "Encode" ? calculateParityBit(input, args) : decodeParityBit(input, args); const method = (input, args) => args[2] === "Encode" ? calculateParityBit(input, args) : decodeParityBit(input, args);
if (args[3].length > 0){ if (args[3].length > 0) {
let byteStrings = input.split(args[3]); const byteStrings = input.split(args[3]);
for (let byteStringsArrayIndex = 0; byteStringsArrayIndex < byteStrings.length; byteStringsArrayIndex++){ for (let byteStringsArrayIndex = 0; byteStringsArrayIndex < byteStrings.length; byteStringsArrayIndex++) {
byteStrings[byteStringsArrayIndex] = method(byteStrings[byteStringsArrayIndex], args); byteStrings[byteStringsArrayIndex] = method(byteStrings[byteStringsArrayIndex], args);
} }
return byteStrings.join(args[3]) return byteStrings.join(args[3]);
} }
return method(input, args); return method(input, args);
} }
@ -87,8 +92,8 @@ class ParityBit extends Operation {
* @returns {Object[]} pos * @returns {Object[]} pos
*/ */
highlight(pos, args) { highlight(pos, args) {
if (args[3].length === 0){ if (args[3].length === 0) {
if (args[1] === "Prepend"){ if (args[1] === "Prepend") {
pos[0].start += 1; pos[0].start += 1;
pos[0].end += 1; pos[0].end += 1;
} }
@ -107,9 +112,9 @@ class ParityBit extends Operation {
* @returns {Object[]} pos * @returns {Object[]} pos
*/ */
highlightReverse(pos, args) { highlightReverse(pos, args) {
if (args[3].length === 0){ if (args[3].length === 0) {
if (args[1] === "Prepend"){ if (args[1] === "Prepend") {
if (pos[0].start > 0){ if (pos[0].start > 0) {
pos[0].start -= 1; pos[0].start -= 1;
} }
pos[0].end -= 1; pos[0].end -= 1;