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";
/**
* 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);
}
}
}

View File

@ -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;