From d36cede0c7706c90fa579991022029946779d589 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Karsten=20Silkenb=C3=A4umer?= Date: Sat, 2 Mar 2019 17:55:03 +0100 Subject: [PATCH] Use better names for the alphabet selection --- src/core/lib/Bacon.mjs | 13 +++++++++---- src/core/operations/BaconCipherDecode.mjs | 9 +++++---- src/core/operations/BaconCipherEncode.mjs | 10 +++++----- tests/operations/tests/BaconCipher.mjs | 4 ++-- 4 files changed, 21 insertions(+), 15 deletions(-) diff --git a/src/core/lib/Bacon.mjs b/src/core/lib/Bacon.mjs index e45c3b74..3ed39336 100644 --- a/src/core/lib/Bacon.mjs +++ b/src/core/lib/Bacon.mjs @@ -9,10 +9,15 @@ /** * Bacon definitions. */ - -export const BACON_ALPHABET_REDUCED = "ABCDEFGHIKLMNOPQRSTUWXYZ"; -export const BACON_ALPHABET_COMPLETE = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; -export const BACON_CODES_REDUCED = [0, 1, 2, 3, 4, 5, 6, 7, 8, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 19, 20, 21, 22, 23]; +export const BACON_ALPHABETS = { + "Standard (I=J and U=V)": { + alphabet: "ABCDEFGHIKLMNOPQRSTUWXYZ", + codes: [0, 1, 2, 3, 4, 5, 6, 7, 8, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 19, 20, 21, 22, 23] + }, + "Complete": { + alphabet: "ABCDEFGHIJKLMNOPQRSTUVWXYZ" + } +}; export const BACON_TRANSLATION_01 = "0/1"; export const BACON_TRANSLATION_AB = "A/B"; export const BACON_TRANSLATION_CASE = "Case"; diff --git a/src/core/operations/BaconCipherDecode.mjs b/src/core/operations/BaconCipherDecode.mjs index 6aa5aac8..ecd1bc92 100644 --- a/src/core/operations/BaconCipherDecode.mjs +++ b/src/core/operations/BaconCipherDecode.mjs @@ -8,7 +8,7 @@ import Operation from "../Operation"; import { - BACON_ALPHABET_REDUCED, BACON_ALPHABET_COMPLETE, + BACON_ALPHABETS, BACON_TRANSLATION_CASE, BACON_TRANSLATION_AMNZ, BACON_TRANSLATIONS, BACON_CLEARER_MAP, BACON_NORMALIZE_MAP, swapZeroAndOne } from "../lib/Bacon"; @@ -33,7 +33,7 @@ class BaconCipherDecode extends Operation { { "name": "Alphabet", "type": "option", - "value": [BACON_ALPHABET_REDUCED, BACON_ALPHABET_COMPLETE] + "value": Object.keys(BACON_ALPHABETS) }, { "name": "Translation", @@ -55,10 +55,11 @@ class BaconCipherDecode extends Operation { */ run(input, args) { const [alphabet, translation, invert] = args; - // split text into groups of 5 characters + const alphabetObject = BACON_ALPHABETS[alphabet]; // remove invalid characters input = input.replace(BACON_CLEARER_MAP[translation], ""); + // normalize to unique alphabet if (BACON_NORMALIZE_MAP[translation] !== undefined) { input = input.replace(/./g, function (c) { @@ -95,7 +96,7 @@ class BaconCipherDecode extends Operation { for (let index = 0; index < inputArray.length; index++) { const code = inputArray[index]; const number = parseInt(code, 2); - output += number < alphabet.length ? alphabet[number] : "?"; + output += number < alphabetObject.alphabet.length ? alphabetObject.alphabet[number] : "?"; } return output; } diff --git a/src/core/operations/BaconCipherEncode.mjs b/src/core/operations/BaconCipherEncode.mjs index 4761df46..e163792e 100644 --- a/src/core/operations/BaconCipherEncode.mjs +++ b/src/core/operations/BaconCipherEncode.mjs @@ -8,11 +8,10 @@ import Operation from "../Operation"; import { - BACON_ALPHABET_REDUCED, BACON_ALPHABET_COMPLETE, + BACON_ALPHABETS, BACON_TRANSLATIONS_FOR_ENCODING, BACON_TRANSLATION_AB, swapZeroAndOne } from "../lib/Bacon"; -import { BACON_CODES_REDUCED } from "../lib/Bacon.mjs"; /** * BaconCipherEncode operation @@ -34,7 +33,7 @@ class BaconCipherEncode extends Operation { { "name": "Alphabet", "type": "option", - "value": [BACON_ALPHABET_REDUCED, BACON_ALPHABET_COMPLETE] + "value": Object.keys(BACON_ALPHABETS) }, { "name": "Translation", @@ -62,6 +61,7 @@ class BaconCipherEncode extends Operation { run(input, args) { const [alphabet, translation, keep, invert] = args; + const alphabetObject = BACON_ALPHABETS[alphabet]; const charCodeA = "A".charCodeAt(0); const charCodeZ = "Z".charCodeAt(0); @@ -69,8 +69,8 @@ class BaconCipherEncode extends Operation { const charCode = c.toUpperCase().charCodeAt(0); if (charCode >= charCodeA && charCode <= charCodeZ) { let code = charCode - charCodeA; - if (alphabet === BACON_ALPHABET_REDUCED) { - code = BACON_CODES_REDUCED[code]; + if (alphabetObject.codes !== undefined) { + code = alphabetObject.codes[code]; } const bacon = ("00000" + code.toString(2)).substr(-5, 5); return bacon; diff --git a/tests/operations/tests/BaconCipher.mjs b/tests/operations/tests/BaconCipher.mjs index dce51659..16f4bac1 100644 --- a/tests/operations/tests/BaconCipher.mjs +++ b/tests/operations/tests/BaconCipher.mjs @@ -6,9 +6,9 @@ * @license Apache-2.0 */ import TestRegister from "../TestRegister"; -import { BACON_ALPHABET_REDUCED, BACON_ALPHABET_COMPLETE, BACON_TRANSLATIONS } from "../../../src/core/lib/Bacon"; +import { BACON_ALPHABETS, BACON_TRANSLATIONS } from "../../../src/core/lib/Bacon"; -const alphabets = [BACON_ALPHABET_REDUCED, BACON_ALPHABET_COMPLETE]; +const alphabets = Object.keys(BACON_ALPHABETS); const translations = BACON_TRANSLATIONS; TestRegister.addTests([