diff --git a/Conversions/UpperCaseConversion.js b/Conversions/UpperCaseConversion.js index d3a033815..fb5e810a6 100644 --- a/Conversions/UpperCaseConversion.js +++ b/Conversions/UpperCaseConversion.js @@ -1,26 +1,26 @@ /* - Explanation :- a user gives a String (it can be incomplete lower or - partial lower) and then the program would convert it into a - complete(all characters in upper case) upper case string. The - logic we have used in the following program is: All the lower case - characters (a-z) has ASCII value ranging from 97 to 122 and their - corresponding upper case characters (A-Z) have ASCII values 32 + Explanation :- A user gives a string (it can be incomplete lowercase or + partially in lowercase) and then the program converts it into a + completely (all characters in uppercase) uppercase string. The + logic we have used in the following program is: All the lowercase + characters (a-z) has [ASCII](https://en.wikipedia.org/wiki/ASCII) value ranging from 97 to 122 and their + corresponding uppercase characters (A-Z) have ASCII values 32 lesser than them. For example ‘a‘ has an ASCII value of 97 and ‘A‘ has an ASCII value of 65 (97 - 32). The same applies to other characters. */ /** - * UpperCaseConversion takes any case-style string and converts it to the upper case-style string. - * @param {String} inputString any case style string - * @returns {String} upper case string + * upperCaseConversion takes any case-style string and converts it to the uppercase-style string. + * @param {string} inputString Any case style string + * @returns {string} Uppercase string */ -const UpperCaseConversion = (inputString) => { +const upperCaseConversion = (inputString) => { // Take a string and split it into characters. const newString = inputString.split('').map(char => { // Get a character code by the use charCodeAt method. const presentCharCode = char.charCodeAt() - // If the character code lies between 97 to 122 it means they are in the lower case so convert it. + // If the character code lies between 97 to 122, it means they are in the lowercase so convert it. if (presentCharCode >= 97 && presentCharCode <= 122) { // Convert the case by use of the above explanation. return String.fromCharCode(presentCharCode - 32) @@ -32,4 +32,4 @@ const UpperCaseConversion = (inputString) => { return newString.join('') } -module.exports = UpperCaseConversion +export { upperCaseConversion } diff --git a/Conversions/test/UpperCaseConverstion.test.js b/Conversions/test/UpperCaseConverstion.test.js new file mode 100644 index 000000000..a78ec5b9c --- /dev/null +++ b/Conversions/test/UpperCaseConverstion.test.js @@ -0,0 +1,43 @@ +import { upperCaseConversion } from '../UpperCaseConversion' + +describe(('Test the upperCaseConversion function'), () => { + it('should return an empty string when the input is an empty string', () => { + expect(upperCaseConversion('')).toEqual('') + }) + + it('should return an all-uppercase string when input is an all-uppercase string', () => { + expect(upperCaseConversion('ALLUPPERCASE')).toEqual('ALLUPPERCASE') + }) + + it('should return an all-uppercase string when input is an all-uppercase string with spaces', () => { + expect(upperCaseConversion('ALL UPPERCASE')).toEqual('ALL UPPERCASE') + }) + + it('should return an all-uppercase string when input is an all-uppercase string with punctuation', () => { + expect(upperCaseConversion('ALL UPPER-CASE!')).toEqual('ALL UPPER-CASE!') + }) + + it('should return an all-uppercase string when input is an all-lowercase string', () => { + expect(upperCaseConversion('lowercaseinput')).toEqual('LOWERCASEINPUT') + }) + + it('should return an all-uppercase string when input is an all-lowercase string with spaces', () => { + expect(upperCaseConversion('lowercase input')).toEqual('LOWERCASE INPUT') + }) + + it('should return an all-uppercase string when input is an all-lowercase string with punctuation', () => { + expect(upperCaseConversion('lower-case, input.')).toEqual('LOWER-CASE, INPUT.') + }) + + it('should return an all-uppercase string when input is an mixed-case string', () => { + expect(upperCaseConversion('mixeDCaSeINPuT')).toEqual('MIXEDCASEINPUT') + }) + + it('should return an all-uppercase string when input is an mixed-case string with spaces', () => { + expect(upperCaseConversion('mixeD CaSe INPuT')).toEqual('MIXED CASE INPUT') + }) + + it('should return an all-uppercase string when input is an mixed-case string with punctuation', () => { + expect(upperCaseConversion('mixeD-CaSe INPuT!')).toEqual('MIXED-CASE INPUT!') + }) +})