Skip to content

Caesar cipher [Fixed #40] [Fixed #44] #57

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 12 commits into from
67 changes: 67 additions & 0 deletions src/_Classics_/caesar_cipher/caesarCipher.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/* eslint-disable linebreak-style */
/* eslint-disable no-else-return */
const caesarCipher = require('./caeserCipher');

describe('Caesar Cipher tests', () => {
it('Shift not passed', () => {
const ocString = 'Hello World';
const enCiphered = caesarCipher(ocString);
expect(ocString).toBe(enCiphered);
});

it('Encipher equals decipher', () => {
const ocString = 'Hello World';
const enCiphered = caesarCipher(ocString, 1);
const deCiphered = caesarCipher(enCiphered, -1);
expect(ocString).toBe(deCiphered);
});

it('Shift lower than string length works', () => {
const ocString = 'Hello World';
const enCiphered = caesarCipher(ocString, 1);
const idealResult = 'Ifmmp Xpsme';
expect(enCiphered).toBe(idealResult);
});

it('Shift Higher than string length works', () => {
const ocString = 'Hello World';
const enCiphered = caesarCipher(ocString, 63);
const idealResult = caesarCipher(ocString, 1);
expect(enCiphered).toBe(idealResult);
});

it('Negative shift Works', () => {
const ocString = 'Ifmmp Xpsme';
const enCiphered = caesarCipher(ocString, -1);
const idealResult = 'Hello World';
expect(enCiphered).toBe(idealResult);
});

it('Shift is a numerical string', () => {
const ocString = 'Hello World';
const enCiphered = caesarCipher(ocString, '1');
const idealResult = 'Ifmmp Xpsme';
expect(enCiphered).toBe(idealResult);
});

it('Shift is not a numbered string', () => {
function test() {
caesarCipher('Hello World', 'abc');
}
expect(test).toThrow('Invalid Shift Provided');
});

it('toEncipher is NaN', () => {
function test() {
caesarCipher(NaN, 1);
}
expect(test).toThrowError('Invalid string provided');
});

it('toEncipher is undefined', () => {
function test() {
caesarCipher(undefined, 1);
}
expect(test).toThrowError('Invalid string provided');
});
});
46 changes: 46 additions & 0 deletions src/_Classics_/caesar_cipher/caeserCipher.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/**
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Creating a completely new file is not acceptable. I am marking this PR as Invalid and closing it. However, we can go ahead and keep your Unit Tests somehow

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey give me sometime as I have university exams going on, I'll try to update the old code itself asap.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But I am not clear as to why a new code (that solves previous issues and have some new features) is not acceptable. Would you not consider this?

* Most simplest encryption scheme. Read more: [http://practicalcryptography.com/ciphers/caesar-cipher/]
**/
function caesarCipher(toEncipher, shift = 0) {
// If required for very strict shift checking then remove '=0'
if (Number.isNaN(Number(shift)) === true) {
throw new Error('Invalid Shift Provided');
} else {
shift = parseInt(Number(shift), 10);
}

if (typeof (toEncipher) === 'string' || (typeof (toEncipher) === 'number' && Number.isNaN(toEncipher) === false)) {
toEncipher = String(toEncipher);
} else {
throw new Error('Invalid string provided');
}

// These are the valid entries aacepted, you can change it according to requirements
const validEntries = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';

shift %= validEntries.length;

let output = '';
for (const char of toEncipher) {
if (char === ' ') {
output += ' ';
continue;
// ------- If you donot want to accept invalid entries then uncomment below block
/*
========================================================================================================
} else if (validEntries.indexOf(char) === -1) {return Error ('invalid character' + char)}
========================================================================================================
and comment out the below block
*/
//= ======================================================================================================
} else if (validEntries.indexOf(char) === -1) {
output += char;
continue;
}
//= ======================================================================================================
output += (validEntries.indexOf(char) + shift <= validEntries.length) ? validEntries[validEntries.indexOf(char) + shift] : validEntries[(validEntries.indexOf(char) + shift) % validEntries.length];
}
return output;
}

module.exports = caesarCipher;
61 changes: 0 additions & 61 deletions src/_Classics_/caeser_cipher/index.js

This file was deleted.