Skip to content

Commit 64981b7

Browse files
committed
Merge branch 'upgrade-memoize'
2 parents 42b13b8 + 2a5c28b commit 64981b7

3 files changed

Lines changed: 5511 additions & 13 deletions

File tree

Ciphers/ROT13.js

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,24 @@
11
/**
2-
* Transcipher a ROT13 cipher
3-
* @param {String} text - string to be encrypted
4-
* @return {String} - decrypted string
2+
* @function ROT13
3+
* @description - ROT13 ("rotate by 13 places", sometimes hyphenated ROT-13) is a simple letter substitution cipher that replaces a letter with the 13th letter after it in the alphabet. ROT13 is a special case of the Caesar cipher which was developed in ancient Rome. Because there are 26 letters (2×13) in the basic Latin alphabet, ROT13 is its own inverse; that is, to undo ROT13, the same algorithm is applied, so the same action can be used for encoding and decoding. The algorithm provides virtually no cryptographic security, and is often cited as a canonical example of weak encryption.
4+
* @see - [wiki](https://en.wikipedia.org/wiki/ROT13)
5+
* @param {String} str - string to be decrypted
6+
* @return {String} decrypted string
57
*/
6-
const ROT13 = (text) => {
7-
const originalCharacterList = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'
8-
const toBeMappedCharaterList = 'NOPQRSTUVWXYZABCDEFGHIJKLMnopqrstuvwxyzabcdefghijklm'
9-
const index = x => originalCharacterList.indexOf(x)
10-
const replace = x => index(x) > -1 ? toBeMappedCharaterList[index(x)] : x
11-
return text.split('').map(replace).join('')
12-
}
8+
function ROT13 (str) {
9+
if (typeof str !== 'string') {
10+
throw new TypeError('Argument should be string')
11+
}
12+
13+
return str.replace(/[a-z]/gi, (char) => {
14+
const charCode = char.charCodeAt()
1315

14-
export { ROT13 }
16+
if (/[n-z]/i.test(char)) {
17+
return String.fromCharCode(charCode - 13)
18+
}
19+
20+
return String.fromCharCode(charCode + 13)
21+
})
22+
}
1523

16-
// > ROT13('The quick brown fox jumps over the lazy dog')
17-
// 'Gur dhvpx oebja sbk whzcf bire gur ynml qbt'
24+
export default ROT13

Ciphers/test/ROT13.test.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import ROT13 from '../ROT13'
2+
3+
describe('Testing ROT13 function', () => {
4+
it('Test - 1, passing a non-string as an argument', () => {
5+
expect(() => ROT13(0x345)).toThrow()
6+
expect(() => ROT13(123)).toThrow()
7+
expect(() => ROT13(123n)).toThrow()
8+
expect(() => ROT13(false)).toThrow()
9+
expect(() => ROT13({})).toThrow()
10+
expect(() => ROT13([])).toThrow()
11+
})
12+
13+
it('Test - 2, passing a string as an argument', () => {
14+
expect(ROT13('Uryyb Jbeyq')).toBe('Hello World')
15+
expect(ROT13('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz')).toBe('NOPQRSTUVWXYZABCDEFGHIJKLMnopqrstuvwxyzabcdefghijklm')
16+
expect(ROT13('The quick brown fox jumps over the lazy dog')).toBe('Gur dhvpx oebja sbk whzcf bire gur ynml qbt')
17+
})
18+
})

0 commit comments

Comments
 (0)