Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 17 additions & 14 deletions exercises/simple-cipher/example.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,23 @@ function xCode(key, inText, sign) {

const mod = (n, m) => (n % m + m) % m;

export default function (key) {
if (typeof key === 'undefined') {
key = generateKey();
} else if (key.length === 0 || key.match(/[^a-z]/, 'g')) {
throw new Error('Bad key');
export class Cipher {

constructor (key) {
if (typeof key === 'undefined') {
this.key = generateKey();
} else if (key.length === 0 || key.match(/[^a-z]/, 'g')) {
throw new Error('Bad key');
} else {
this.key = key;
}
}

encode(plainText) {
return xCode(this.key, plainText, 1);
}

return {
key,
encode(plainText) {
return xCode(this.key, plainText, 1);
},
decode(encodedText) {
return xCode(this.key, encodedText, -1);
},
};
decode(encodedText) {
return xCode(this.key, encodedText, -1);
}
}
20 changes: 19 additions & 1 deletion exercises/simple-cipher/simple-cipher.spec.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import Cipher from './simple-cipher';
import { Cipher } from './simple-cipher';

describe('Random key generation', () => {
xtest('generates keys at random', () => {
Expand Down Expand Up @@ -56,6 +56,24 @@ describe('Incorrect key cipher', () => {
new Cipher('');
}).toThrow(new Error('Bad key'));
});

xtest('throws an error with a leading space', () => {
expect(() => {
new Cipher(' leadingspace');
}).toThrow(new Error('Bad key'));
});

xtest('throws an error with a punctuation mark', () => {
expect(() => {
new Cipher('hyphened-word');
}).toThrow(new Error('Bad key'));
});

xtest('throws an error with a single capital letter', () => {
expect(() => {
new Cipher('leonardoDavinci');
}).toThrow(new Error('Bad key'));
});
});

describe('Substitution cipher', () => {
Expand Down