JavaScript
JavaScript copied to clipboard
Enhancements to Caesar Cipher Implementation - CaesarCipher.js
Describe your change:
Improves the existing Caesar Cipher implementation to make it more robust, future-proof, and flexible. Key improvements include handling edge cases, optimizing performance, and adding support for custom alphabets. These changes enhance both the functionality and maintainability of the code.
- The function now accepts an optional alphabet parameter, allowing encryption with custom alphabets (e.g., different languages).
- Rotations are now optimized to handle values greater than the alphabet length by using rotation % alphabetLength.
- Non-alphabetic characters (numbers, symbols, etc.) are explicitly left unchanged in the encrypted output.
- The function throws appropriate errors for invalid input types (e.g., non-string, negative rotations) and gracefully handles empty strings.
- The function maintains the case of letters, ensuring that uppercase letters remain uppercase after encryption.
- The cipher map is now generated only once per function call, reducing overhead for large strings.
Additional Changes:
- Improved code readability with clearer logic and added comments for better maintainability.
- Added input validation to ensure rotation is a non-negative integer.
No Issue related to PR at the time of creation. Comment or contact if wrongful.
Checklist:
- [x] I have read CONTRIBUTING.md.
- [x] This pull request is all my own work -- I have not plagiarized.
- [x] I know that pull requests will not be merged if they fail the automated tests.
- [x] This PR only changes one algorithm file. To ease review, please open separate PRs for separate algorithms.
- [x] All new JavaScript files are placed inside an existing directory.
- [x] All filenames should use the UpperCamelCase (PascalCase) style. There should be no spaces in filenames.
Example:
UserProfile.jsis allowed butuserprofile.js,Userprofile.js,user-Profile.js,userProfile.jsare not - [x] All new algorithms have a URL in their comments that points to Wikipedia or another similar explanation.
- [x] If this pull request resolves one or more open issues then the commit message contains
Fixes: #{$ISSUE_NO}.