Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
72def41
feat: improved memoize function
fahimfaisaal Mar 2, 2022
8548ef1
docs: modified documentation
fahimfaisaal Mar 2, 2022
ce9d3b9
style: format with standard
fahimfaisaal Mar 2, 2022
1e2fb53
docs: modified stringify doc
fahimfaisaal Mar 2, 2022
2a5c28b
refactor: remove two repetition implementation
fahimfaisaal Mar 2, 2022
c582f58
feat: added validation, test codes
fahimfaisaal Mar 14, 2022
cdff0e3
chore: remove useless words
fahimfaisaal Mar 15, 2022
5727b64
feat: added types for jest
fahimfaisaal Mar 15, 2022
025a8b8
Merge branch 'upgrade-abs' of https://github.com/fahimfaisaal/Javascr…
fahimfaisaal Mar 15, 2022
9efda24
chore: added link box
fahimfaisaal Mar 15, 2022
98ffd1d
feat: added new validation test casses & methods
fahimfaisaal Mar 26, 2022
b8d6e68
style: formated with standard
fahimfaisaal Mar 26, 2022
b7c961c
feat: added parse method & test cases
fahimfaisaal Mar 26, 2022
6b7b715
docs: added js docs
fahimfaisaal Mar 27, 2022
269a3f4
chore: added default import export
fahimfaisaal Mar 27, 2022
5cedaa9
feat: imporved algorithm via replace method
fahimfaisaal Mar 27, 2022
d00a7bf
test: added two test cases
fahimfaisaal Mar 27, 2022
bbcce39
feat: added jest type for suggestions
fahimfaisaal Mar 28, 2022
9be30a8
feat: added `reduceRight` & `trim` method
fahimfaisaal Mar 28, 2022
489544d
chore: added helper variable
fahimfaisaal Mar 28, 2022
68cb5ad
feat: added new rotation option
fahimfaisaal Mar 29, 2022
addf721
Revert "chore: added helper variable"
fahimfaisaal Mar 29, 2022
07a2b17
Merge branch 'TheAlgorithms:master' into master
fahimfaisaal Mar 29, 2022
8c297ae
Merge branch 'upgrade-CaesarsCipher'
fahimfaisaal Mar 29, 2022
605cfb7
Merge branch 'upgrade-reverseWord'
fahimfaisaal Mar 29, 2022
6636af2
Merge branch 'upgrade-packageJson'
fahimfaisaal Mar 29, 2022
3905c5d
Merge branch 'upgrade-LRU'
fahimfaisaal Mar 29, 2022
42b13b8
Merge branch 'upgrade-ciphers'
fahimfaisaal Mar 29, 2022
64981b7
Merge branch 'upgrade-memoize'
fahimfaisaal Mar 29, 2022
e1c6666
Merge branch 'upgrade-abs'
fahimfaisaal Mar 29, 2022
e783547
remove: yarn lock
fahimfaisaal Mar 29, 2022
399e353
chore: fix grammer
fahimfaisaal Mar 29, 2022
189e492
Merge branch 'master' of github.com:fahimfaisaal/Javascript
fahimfaisaal Mar 29, 2022
a946220
Merge branch 'TheAlgorithms:master' into master
fahimfaisaal Mar 29, 2022
48329bf
feat: remove revert
fahimfaisaal Mar 29, 2022
08955a7
chore: added new line
fahimfaisaal Mar 29, 2022
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 20 additions & 13 deletions Ciphers/ROT13.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,24 @@
/**
* Transcipher a ROT13 cipher
* @param {String} text - string to be encrypted
* @return {String} - decrypted string
* @function ROT13
* @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.
* @see - [wiki](https://en.wikipedia.org/wiki/ROT13)
* @param {String} str - string to be decrypted
* @return {String} decrypted string
*/
const ROT13 = (text) => {
const originalCharacterList = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'
const toBeMappedCharaterList = 'NOPQRSTUVWXYZABCDEFGHIJKLMnopqrstuvwxyzabcdefghijklm'
const index = x => originalCharacterList.indexOf(x)
const replace = x => index(x) > -1 ? toBeMappedCharaterList[index(x)] : x
return text.split('').map(replace).join('')
}
function ROT13 (str) {
if (typeof str !== 'string') {
throw new TypeError('Argument should be string')
}

return str.replace(/[a-z]/gi, (char) => {
const charCode = char.charCodeAt()

export { ROT13 }
if (/[n-z]/i.test(char)) {
return String.fromCharCode(charCode - 13)
}

return String.fromCharCode(charCode + 13)
})
}

// > ROT13('The quick brown fox jumps over the lazy dog')
// 'Gur dhvpx oebja sbk whzcf bire gur ynml qbt'
export default ROT13
18 changes: 18 additions & 0 deletions Ciphers/test/ROT13.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import ROT13 from '../ROT13'

describe('Testing ROT13 function', () => {
it('Test - 1, passing a non-string as an argument', () => {
expect(() => ROT13(0x345)).toThrow()
expect(() => ROT13(123)).toThrow()
expect(() => ROT13(123n)).toThrow()
expect(() => ROT13(false)).toThrow()
expect(() => ROT13({})).toThrow()
expect(() => ROT13([])).toThrow()
})

it('Test - 2, passing a string as an argument', () => {
expect(ROT13('Uryyb Jbeyq')).toBe('Hello World')
expect(ROT13('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz')).toBe('NOPQRSTUVWXYZABCDEFGHIJKLMnopqrstuvwxyzabcdefghijklm')
expect(ROT13('The quick brown fox jumps over the lazy dog')).toBe('Gur dhvpx oebja sbk whzcf bire gur ynml qbt')
})
})