Skip to content

Commit

Permalink
Merge pull request #7 from Dunkelhaiser/dev
Browse files Browse the repository at this point in the history
Moved processing to external method
  • Loading branch information
Dunkelhaiser authored Jan 24, 2024
2 parents 9d65f39 + db5b6aa commit 1ed0c07
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 21 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# @dunkelhaiser/caesar-cipher

## 1.1.1

### Patch Changes

- 8d6fe60: Refactored code

## 1.1.0

### Minor Changes
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@dunkelhaiser/caesar-cipher",
"version": "1.1.0",
"version": "1.1.1",
"description": "Implementation of the Caesar Cipher encryption algorithm in JavaScript. The Caesar Cipher is a classic substitution cipher technique where each letter in the plaintext is shifted a certain number of places down or up the alphabet.",
"private": false,
"keywords": [
Expand Down
39 changes: 19 additions & 20 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,21 @@ export class CaesarCipher {
return char;
}

/**
* Processes a given string.
*
* @param {(string|Array.<string>)} input - text to be encrypted
* @param {number} shift - amount to shift each character by, cannot be negative (will be converted to positive)
* @param {string=} alphabet - alphabet to be used for the cipher (defaults to English alphabet)
* @param {boolean} decrypt - if true, performs decryption by shifting in the opposite direction
* @returns {(string|Array.<string>)} encrypted string
*/
private static processString = (input: string, shift: number, alphabet?: string, decrypt?: boolean): string =>
input
.split("")
.map((char) => this.shiftChar(char, shift, alphabet, decrypt))
.join("");

/**
* Encrypts a given input string.
*
Expand All @@ -48,17 +63,9 @@ export class CaesarCipher {
*/
static encrypt<T extends string | string[]>(input: T, shift: number, alphabet?: string): T {
if (Array.isArray(input)) {
return input.map((str) =>
str
.split("")
.map((char) => this.shiftChar(char, shift, alphabet, false))
.join("")
) as T;
return input.map((str) => this.processString(str, shift, alphabet, false)) as T;
}
return input
.split("")
.map((char) => this.shiftChar(char, shift, alphabet, false))
.join("") as T;
return this.processString(input, shift, alphabet, false) as T;
}

/**
Expand All @@ -71,16 +78,8 @@ export class CaesarCipher {
*/
static decrypt<T extends string | string[]>(input: T, shift: number, alphabet?: string): T {
if (Array.isArray(input)) {
return input.map((str) =>
str
.split("")
.map((char) => this.shiftChar(char, shift, alphabet, true))
.join("")
) as T;
return input.map((str) => this.processString(str, shift, alphabet, true)) as T;
}
return input
.split("")
.map((char) => this.shiftChar(char, shift, alphabet, true))
.join("") as T;
return this.processString(input, shift, alphabet, true) as T;
}
}

0 comments on commit 1ed0c07

Please sign in to comment.