Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 8a11916

Browse files
Merge pull request TheAlgorithms#735 from charliejmoore/title-case-conversion-tests
Title case conversion tests
2 parents 90b447c + ced6c89 commit 8a11916

File tree

2 files changed

+67
-12
lines changed

2 files changed

+67
-12
lines changed

‎Conversions/TitleCaseConversion.js

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,23 @@
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}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import { titleCaseConversion } from '../TitleCaseConversion'
2+
3+
describe(('Tests for the titleCaseConversion function'), () => {
4+
it('should return an empty string when the input is an empty string', () => {
5+
expect(titleCaseConversion('')).toEqual('')
6+
})
7+
8+
it('should return the input string when the input string is a title case string', () => {
9+
expect(titleCaseConversion('A Proper Title Case String')).toEqual('A Proper Title Case String')
10+
})
11+
12+
it('should return a title case string when input is an all-uppercase string', () => {
13+
expect(titleCaseConversion('ALL UPPER CASE')).toEqual('All Upper Case')
14+
})
15+
16+
it('should return a title case string when input is a title case string of with spaces', () => {
17+
expect(titleCaseConversion('ALL UPPERCASE')).toEqual('All Uppercase')
18+
})
19+
20+
it('should return a title case string when input is a title case string of with no spaces', () => {
21+
expect(titleCaseConversion('ALLUPPERCASE')).toEqual('Alluppercase')
22+
})
23+
24+
it('should return a title case string when input is a title case string with punctuation', () => {
25+
expect(titleCaseConversion('All Title Case!')).toEqual('All Title Case!')
26+
})
27+
28+
it('should return a title case string when input is an all-lowercase string with no spaces', () => {
29+
expect(titleCaseConversion('lowercaseinput')).toEqual('Lowercaseinput')
30+
})
31+
32+
it('should return a title case string when input is an all-lowercase string with spaces', () => {
33+
expect(titleCaseConversion('lowercase input')).toEqual('Lowercase Input')
34+
})
35+
36+
it('should return a title case string when input is an all-lowercase string with punctuation', () => {
37+
expect(titleCaseConversion('lower, case, input.')).toEqual('Lower, Case, Input.')
38+
})
39+
40+
it('should return a title case string when input is an mixed-case string', () => {
41+
expect(titleCaseConversion('mixeD CaSe INPuT')).toEqual('Mixed Case Input')
42+
})
43+
44+
it('should return a title case string when input is an mixed-case string with no spaces', () => {
45+
expect(titleCaseConversion('mixeDCaSeINPuT')).toEqual('Mixedcaseinput')
46+
})
47+
48+
it('should return a title case string when input is an mixed-case string with punctuation', () => {
49+
expect(titleCaseConversion('mixeD, CaSe, INPuT!')).toEqual('Mixed, Case, Input!')
50+
})
51+
})

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /