Skip to content

Commit

Permalink
simplify the code by using a ternary operator to choose between decry…
Browse files Browse the repository at this point in the history
…ption and encryption of the text
  • Loading branch information
sergeiown committed Jul 2, 2024
1 parent 7869716 commit 41f68e4
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 19 deletions.
13 changes: 4 additions & 9 deletions base64.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,10 @@ function processText() {

const isEncoded = isBase64(text);

if (isEncoded) {
const decodedText = decodeText(text);
console.log('Decrypted text:', `${decodedText}`);
writeToOutputFile(`${decodedText}${os.EOL}`);
} else {
const encodedText = Buffer.from(text, 'utf8').toString('base64');
console.log('Encrypted text:', `${encodedText}`);
writeToOutputFile(`${encodedText}${os.EOL}`);
}
const resultText = isEncoded ? decodeText(text) : Buffer.from(text, 'utf8').toString('base64');

console.log(isEncoded ? 'Decrypted text:' : 'Encrypted text:', `${resultText}`);
writeToOutputFile(`${resultText}${os.EOL}`);

processText();
});
Expand Down
13 changes: 3 additions & 10 deletions xor.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,10 @@ function processText() {
writeToOutputFile(`${os.EOL}`);
writeToOutputFile(`${text}${os.EOL}`);

const isEncoded = isXOREncoded(text);
const result = isXOREncoded(text) ? decodeText(text, encryptionKey) : encodeText(text, encryptionKey);

if (isEncoded) {
const decodedText = decodeText(text, encryptionKey);
console.log('Decrypted text:', `${decodedText}`);
writeToOutputFile(`${decodedText}${os.EOL}`);
} else {
const encodedText = encodeText(text, encryptionKey);
console.log('Encrypted text:', `${encodedText}`);
writeToOutputFile(`${encodedText}${os.EOL}`);
}
console.log(isXOREncoded(text) ? 'Decrypted text:' : 'Encrypted text:', result);
writeToOutputFile(`${result}${os.EOL}`);

processText();
});
Expand Down

0 comments on commit 41f68e4

Please sign in to comment.