From b842a38c84d560b5b37c16f241ed556683fe61ac Mon Sep 17 00:00:00 2001 From: j83305 Date: Tue, 2 Jun 2020 11:27:29 +0100 Subject: [PATCH] ran the linter --- src/core/lib/ParityBit.mjs | 29 +++++++++++++++++++---------- src/core/operations/ParityBit.mjs | 25 +++++++++++++++---------- 2 files changed, 34 insertions(+), 20 deletions(-) diff --git a/src/core/lib/ParityBit.mjs b/src/core/lib/ParityBit.mjs index 342631a1..9675b46c 100644 --- a/src/core/lib/ParityBit.mjs +++ b/src/core/lib/ParityBit.mjs @@ -9,33 +9,42 @@ 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) { let count1s = 0; - for (let i = 0; i < input.length; i++){ + for (let i = 0; i < input.length; i++) { const character = input.charAt(i); - if (character === "1"){ + if (character === "1") { count1s++; - } else if (character !== args[3] && character !== "0" && character !== " "){ + } else if (character !== args[3] && character !== "0" && character !== " ") { throw new OperationError("unexpected character encountered: \"" + character + "\""); } } let parityBit = "1"; const flipflop = args[0] === "Even Parity" ? 0 : 1; - if (count1s % 2 === flipflop){ + if (count1s % 2 === flipflop) { parityBit = "0"; } - if (args[1] === "End"){ + if (args[1] === "End") { return input + parityBit; - }else{ + } else { 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) { - if (args[1] === "End"){ + if (args[1] === "End") { return input.slice(0, -1); - }else{ + } else { return input.slice(1); } -} \ No newline at end of file +} diff --git a/src/core/operations/ParityBit.mjs b/src/core/operations/ParityBit.mjs index a27574f2..c5ac1d1e 100644 --- a/src/core/operations/ParityBit.mjs +++ b/src/core/operations/ParityBit.mjs @@ -63,16 +63,21 @@ class ParityBit extends Operation { * @returns {string} */ run(input, args) { - if (input.length === 0){ + if (input.length === 0) { 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); - if (args[3].length > 0){ - let byteStrings = input.split(args[3]); - for (let byteStringsArrayIndex = 0; byteStringsArrayIndex < byteStrings.length; byteStringsArrayIndex++){ + if (args[3].length > 0) { + const byteStrings = input.split(args[3]); + for (let byteStringsArrayIndex = 0; byteStringsArrayIndex < byteStrings.length; byteStringsArrayIndex++) { byteStrings[byteStringsArrayIndex] = method(byteStrings[byteStringsArrayIndex], args); } - return byteStrings.join(args[3]) + return byteStrings.join(args[3]); } return method(input, args); } @@ -87,8 +92,8 @@ class ParityBit extends Operation { * @returns {Object[]} pos */ highlight(pos, args) { - if (args[3].length === 0){ - if (args[1] === "Prepend"){ + if (args[3].length === 0) { + if (args[1] === "Prepend") { pos[0].start += 1; pos[0].end += 1; } @@ -107,9 +112,9 @@ class ParityBit extends Operation { * @returns {Object[]} pos */ highlightReverse(pos, args) { - if (args[3].length === 0){ - if (args[1] === "Prepend"){ - if (pos[0].start > 0){ + if (args[3].length === 0) { + if (args[1] === "Prepend") { + if (pos[0].start > 0) { pos[0].start -= 1; } pos[0].end -= 1;