Skip to content

Commit 075d35f

Browse files
authored
merge: Improved ROT13 function (TheAlgorithms#964)
* feat: improved memoize function used Map instead of object & used the JSON.stringfy method for generate a valid string as a key * docs: modified documentation * style: format with standard * docs: modified stringify doc * refactor: remove two repetition implementation * feat: added validation, test codes * chore: remove useless words * feat: added types for jest * chore: added link box * feat: added new validation test casses & methods * style: formated with standard * feat: added parse method & test cases * docs: added js docs * chore: added default import export * feat: imporved algorithm via replace method * test: added two test cases * feat: added jest type for suggestions * feat: added `reduceRight` & `trim` method * chore: added helper variable * feat: added new rotation option * Revert "chore: added helper variable" This reverts commit 489544d. * remove: yarn lock * chore: fix grammer * feat: remove revert * chore: added new line
1 parent cdfa264 commit 075d35f

File tree

2 files changed

+38
-13
lines changed

2 files changed

+38
-13
lines changed

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)