From 7a3d24a382940502a55a47c4981ad160ee6175e4 Mon Sep 17 00:00:00 2001 From: vbetsch Date: Thu, 9 Apr 2026 20:45:53 +0200 Subject: [PATCH 1/3] test: should encrypt hello or world --- tests/cypher.test.ts | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/tests/cypher.test.ts b/tests/cypher.test.ts index e7abc60..0951aa1 100644 --- a/tests/cypher.test.ts +++ b/tests/cypher.test.ts @@ -14,4 +14,12 @@ describe('Cypher', () => { const cypher: Cypher = new Cypher(); expect(cypher.decrypt('ldga(')).toBe('world'); }); + it('should encrypt hello', () => { + const cypher: Cypher = new Cypher(); + expect(cypher.encrypt('hello')).toBe('&£aad'); + }); + it('should encrypt world', () => { + const cypher: Cypher = new Cypher(); + expect(cypher.encrypt('world')).toBe('ldga('); + }); }); From adf250afe49d1b1955381d773fa5fa2b4ecbd14a Mon Sep 17 00:00:00 2001 From: vbetsch Date: Thu, 9 Apr 2026 20:46:43 +0200 Subject: [PATCH 2/3] feat: add encrypt function --- src/cypher.ts | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/cypher.ts b/src/cypher.ts index 9edd931..4a5a722 100644 --- a/src/cypher.ts +++ b/src/cypher.ts @@ -65,4 +65,14 @@ export class Cypher { } return result; } + + public encrypt(message: string): string { + let result: string = ''; + for (let i = 0; i < message.length; i++) { + const char: string = message.charAt(i); + const index: number = this._alphabet.indexOf(char); + result += this._decryptionKey[index]; + } + return result; + } } From 3cd7e7f3f8be0e350b03a6e182189111e7371c15 Mon Sep 17 00:00:00 2001 From: vbetsch Date: Thu, 9 Apr 2026 20:49:41 +0200 Subject: [PATCH 3/3] refactor: create translate function --- src/cypher.ts | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/src/cypher.ts b/src/cypher.ts index 4a5a722..eec8389 100644 --- a/src/cypher.ts +++ b/src/cypher.ts @@ -56,23 +56,21 @@ export class Cypher { 'o', ]; - public decrypt(message: string): string { + private _translate(dictSource: string[], dictTarget: string[], message: string): string { let result: string = ''; for (let i = 0; i < message.length; i++) { const char: string = message.charAt(i); - const index: number = this._decryptionKey.indexOf(char); - result += this._alphabet[index]; + const index: number = dictSource.indexOf(char); + result += dictTarget[index]; } return result; } + public decrypt(message: string): string { + return this._translate(this._decryptionKey, this._alphabet, message); + } + public encrypt(message: string): string { - let result: string = ''; - for (let i = 0; i < message.length; i++) { - const char: string = message.charAt(i); - const index: number = this._alphabet.indexOf(char); - result += this._decryptionKey[index]; - } - return result; + return this._translate(this._alphabet, this._decryptionKey, message); } }