-
Notifications
You must be signed in to change notification settings - Fork 268
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
Closed
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
c99c951
basic caesarCipher and a single test for trial.
Omkaragrawal 7591809
fixed 'toBe'
Omkaragrawal f6b29cc
some condition changes
Omkaragrawal 88646e3
condition for shift reconstructed
Omkaragrawal cdfa983
tests added
Omkaragrawal b685575
trying to resolve throw and return
Omkaragrawal 9759a81
little bug resolved. Test bug resolved too
Omkaragrawal 2a2cdbc
folder Renamed
Omkaragrawal e8e30b7
index.js removed and indentations fixed to eslint.
Omkaragrawal 6b3d07a
all the comparisions in caesarCipher.test repaired
Omkaragrawal dc1453c
caesarCipher.test comparision repair done.
Omkaragrawal c6d32bd
fixed issue with decimal shift values.
Omkaragrawal File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
}); | ||
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
/** | ||
* 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; |
This file was deleted.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?