This repository has been archived by the owner on May 19, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[refactor] improve string capitalize functions
- Loading branch information
Showing
1 changed file
with
17 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,30 @@ | ||
/** | ||
* Return the word with the first letter capitalized. | ||
* @param word | ||
* Return the string with the first letter capitalized. | ||
* @param string | ||
* @returns {string} | ||
*/ | ||
function capitalizeWord (word) { | ||
return word.charAt(0).toUpperCase() + word.toLowerCase().slice(1) | ||
} | ||
function capitalize (string) { | ||
if (typeof string !== 'string') { | ||
throw TypeError('Parameter must be a string.') | ||
} | ||
|
||
/** | ||
* Return the words with the first letter capitalized. | ||
* @param words | ||
* @returns {string} | ||
*/ | ||
function capitalizeWords (words) { | ||
return words.split(' ').map(capitalizeWord).join(' ') | ||
return string.charAt(0).toUpperCase() + string.toLowerCase().slice(1) | ||
} | ||
|
||
/** | ||
* Return an array of word with first letter capitalized. | ||
* @param wordList | ||
* Return an array of strings with first letter capitalized. | ||
* @param list | ||
* @returns {string[]} | ||
*/ | ||
function capitalizeWordList (wordList) { | ||
return wordList.map(capitalizeWords) | ||
function capitalizeList (list) { | ||
if (!Array.isArray(list) || !list.every(string => typeof string === 'string')) { | ||
throw TypeError('Parameter must be an array of strings.') | ||
} | ||
|
||
return list.map(capitalize) | ||
} | ||
|
||
export default { | ||
capitalizeWord, | ||
capitalizeWords, | ||
capitalizeWordList | ||
capitalize, | ||
capitalizeList | ||
} |