11/*
2- Problem statement and Explanation : https://www.codeproject.com/Tips/162540/Letter-Case-Conversion-Algorithms-Title-Case-Toggl
2+ Problem statement and Explanation : https://www.codeproject.com/Tips/162540/Letter-Case-Conversion-Algorithms-Title-Case-Toggl.
3+ [Title case](https://en.wikipedia.org/wiki/Title_case) is a style where all words are capitalized. Officially, title case
4+ does not capitalize some words, such as very short words like "a" or "is", but for the purposes of this function, a general approach
5+ is taken where all words are capitalized regarless of length.
36*/
47
58/**
6- * The TitleCaseConversion converts a string into a title case string.
7- * @param {String } inputString input string
8- * @returns {String }
9+ * The titleCaseConversion function converts a string into a title case string.
10+ * @param {string } inputString The input string which can have any types of letter casing.
11+ * @returns {string } A string that is in title case.
912 */
10- const TitleCaseConversion = ( inputString ) => {
13+ const titleCaseConversion = ( inputString ) => {
14+ if ( inputString === '' ) return ''
1115 // Extact all space seprated string.
1216 const stringCollections = inputString . split ( ' ' ) . map ( word => {
1317 let firstChar = ''
14- // Get a character code by the use charCodeAt method.
18+ // Get the [ASCII](https://en.wikipedia.org/wiki/ASCII) character code by the use charCodeAt method.
1519 const firstCharCode = word [ 0 ] . charCodeAt ( )
16- // If the character code lies between 97 to 122 it means they are in the lower case so convert it.
20+ // If the ASCII character code lies between 97 to 122 it means they are in the lowercase so convert it.
1721 if ( firstCharCode >= 97 && firstCharCode <= 122 ) {
1822 // Convert the case by use of the above explanation.
1923 firstChar += String . fromCharCode ( firstCharCode - 32 )
@@ -22,21 +26,21 @@ const TitleCaseConversion = (inputString) => {
2226 firstChar += word [ 0 ]
2327 }
2428 const newWordChar = word . slice ( 1 ) . split ( '' ) . map ( char => {
25- // Get a character code by the use charCodeAt method.
29+ // Get the ASCII character code by the use charCodeAt method.
2630 const presentCharCode = char . charCodeAt ( )
27- // If the character code lies between 65 to 90 it means they are in the upper case so convert it.
31+ // If the ASCII character code lies between 65 to 90, it means they are in the uppercase so convert it.
2832 if ( presentCharCode >= 65 && presentCharCode <= 90 ) {
2933 // Convert the case by use of the above explanation.
3034 return String . fromCharCode ( presentCharCode + 32 )
3135 }
3236 // Else return the characters without any modification.
3337 return char
3438 } )
35- // return the first converted character and remaining character string.
39+ // Return the first converted character and remaining character string.
3640 return firstChar + newWordChar . join ( '' )
3741 } )
38- // convert all words in a string and return it.
42+ // Convert all words in a string and return it.
3943 return stringCollections . join ( ' ' )
4044}
4145
42- module . exports = TitleCaseConversion
46+ export { titleCaseConversion }
0 commit comments