diff --git a/exercises/simple-cipher/example.js b/exercises/simple-cipher/example.js index f3769fbc08..8e41b5d406 100644 --- a/exercises/simple-cipher/example.js +++ b/exercises/simple-cipher/example.js @@ -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); + } } diff --git a/exercises/simple-cipher/simple-cipher.spec.js b/exercises/simple-cipher/simple-cipher.spec.js index d3a9e48537..2f27067a17 100644 --- a/exercises/simple-cipher/simple-cipher.spec.js +++ b/exercises/simple-cipher/simple-cipher.spec.js @@ -1,4 +1,4 @@ -import Cipher from './simple-cipher'; +import { Cipher } from './simple-cipher'; describe('Random key generation', () => { xtest('generates keys at random', () => { @@ -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', () => {