diff --git a/src/cypher.ts b/src/cypher.ts index eec8389..dc4d960 100644 --- a/src/cypher.ts +++ b/src/cypher.ts @@ -61,6 +61,7 @@ export class Cypher { for (let i = 0; i < message.length; i++) { const char: string = message.charAt(i); const index: number = dictSource.indexOf(char); + if(!dictTarget[index]) continue; result += dictTarget[index]; } return result; @@ -71,6 +72,6 @@ export class Cypher { } public encrypt(message: string): string { - return this._translate(this._alphabet, this._decryptionKey, message); + return this._translate(this._alphabet, this._decryptionKey, message.toLowerCase()); } } diff --git a/src/index.ts b/src/index.ts index a420803..4121ad7 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1 +1,5 @@ +import {Cypher} from "@src/cypher"; + console.log('Hello World!'); +const cypher: Cypher = new Cypher(); +console.log(cypher.encrypt('Hello World!')); diff --git a/tests/cypher.test.ts b/tests/cypher.test.ts index 0951aa1..bf21c57 100644 --- a/tests/cypher.test.ts +++ b/tests/cypher.test.ts @@ -1,4 +1,4 @@ -import { Cypher } from '@src/cypher'; +import {Cypher} from '@src/cypher'; describe('Cypher', () => { it('should be OK', () => { @@ -6,6 +6,14 @@ describe('Cypher', () => { expect(cypher).toBeDefined(); expect(cypher).toBeInstanceOf(Cypher); }); + it('should decrypt h', () => { + const cypher: Cypher = new Cypher(); + expect(cypher.decrypt('&')).toBe('h'); + }); + it('should decrypt he', () => { + const cypher: Cypher = new Cypher(); + expect(cypher.decrypt('&£')).toBe('he'); + }); it('should decrypt hello', () => { const cypher: Cypher = new Cypher(); expect(cypher.decrypt('&£aad')).toBe('hello'); @@ -14,6 +22,14 @@ describe('Cypher', () => { const cypher: Cypher = new Cypher(); expect(cypher.decrypt('ldga(')).toBe('world'); }); + it('should encrypt h', () => { + const cypher: Cypher = new Cypher(); + expect(cypher.encrypt('h')).toBe('&'); + }); + it('should encrypt he', () => { + const cypher: Cypher = new Cypher(); + expect(cypher.encrypt('he')).toBe('&£'); + }); it('should encrypt hello', () => { const cypher: Cypher = new Cypher(); expect(cypher.encrypt('hello')).toBe('&£aad'); @@ -22,4 +38,12 @@ describe('Cypher', () => { const cypher: Cypher = new Cypher(); expect(cypher.encrypt('world')).toBe('ldga('); }); + it('should handle cases', () => { + const cypher: Cypher = new Cypher(); + expect(cypher.encrypt('Hello')).toBe('&£aad'); + }); + it('should handle unknown characters', () => { + const cypher: Cypher = new Cypher(); + expect(cypher.encrypt('Hello !')).toBe('&£aad'); + }); });