-
Notifications
You must be signed in to change notification settings - Fork 180
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12 from Marceldts/proposal--caesar-cypher
feat: remade caesar cypher and tests
- Loading branch information
Showing
3 changed files
with
45 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,33 @@ | ||
import { describe, it, expect } from 'vitest' | ||
import { cypher } from './caesar-cypher' | ||
|
||
describe('cypher', () => { | ||
it('should shift by one position', () => { | ||
const given = 'abc' | ||
|
||
const actual = cypher(given, 1) | ||
|
||
expect(actual).toBe('bdf') | ||
describe('caesarCypher', () => { | ||
it('should return an empty string when given an empty string', () => { | ||
expect(cypher('', 1)).toBe('') | ||
}) | ||
it('should shift the letters of the alphabet by the given number', () => { | ||
expect(cypher('abcd', 1)).toBe('bdfh') | ||
}) | ||
|
||
it('should shift by two positions initially', () => { | ||
const given = 'abc' | ||
|
||
const actual = cypher(given, 2) | ||
it('should ignore the blank spaces to increase the shift', () => { | ||
expect(cypher('a b c d', 1)).toBe('b d f h') | ||
}) | ||
|
||
expect(actual).toBe('ceg') | ||
it('should work with negative shifts', () => { | ||
expect(cypher('abcd', -1)).toBe('zbdf') | ||
}) | ||
|
||
it('should handle overflow of the alphabet', () => { | ||
const given = 'xyz' | ||
it('should work with large shifts', () => { | ||
expect(cypher('abcd', 27)).toBe('bdfh') | ||
}) | ||
|
||
const actual = cypher(given, 1) | ||
it('should work with large negative shifts', () => { | ||
expect(cypher('abcd', -27)).toBe('zbdf') | ||
}) | ||
|
||
expect(actual).toBe('yac') | ||
it('should work with the example from the README', () => { | ||
expect(cypher('I should have known that you would have a perfect answer for me!!!', 1)).toBe( | ||
'J ukszrk pjfp wacld kztn tkr unumf keak h xnbqqph pdjoxl ako kd!!!' | ||
) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,26 @@ | ||
export function cypher(message: string, shiftedBy: number): string { | ||
const alphabet = 'abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz' | ||
let cypheredMessage = '' | ||
const _ALPHABET = 'abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz' | ||
const _HIGH_ALPHABET = 'ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZ' | ||
|
||
for (let i = 0; i < message.length; i++) { | ||
const index = alphabet.indexOf(message[i]) + i + shiftedBy | ||
const nextLetter = alphabet[index] | ||
cypheredMessage += nextLetter | ||
export const cypher = (str: string, shift: number): string => { | ||
if (str.length === 0) return '' | ||
let decypheredString = '' | ||
for (const char of str) { | ||
const index: number = _ALPHABET.indexOf(char) | ||
const upperIndex: number = _HIGH_ALPHABET.indexOf(char) | ||
if (index !== -1 || upperIndex !== -1) { | ||
const isLowerCase: boolean = index !== -1 | ||
const auxIndex = isLowerCase ? index : upperIndex | ||
let currentIndex = auxIndex + shift | ||
while (currentIndex < 0) { | ||
currentIndex += 26 | ||
} | ||
decypheredString += isLowerCase | ||
? _ALPHABET[currentIndex % 26] | ||
: _HIGH_ALPHABET[currentIndex % 26] | ||
shift++ | ||
} else { | ||
decypheredString += char | ||
} | ||
} | ||
|
||
return cypheredMessage | ||
return decypheredString | ||
} |