Skip to content
This repository has been archived by the owner on May 19, 2021. It is now read-only.

Commit

Permalink
[refactor] improve string capitalize functions
Browse files Browse the repository at this point in the history
  • Loading branch information
cedoor committed Sep 1, 2019
1 parent 99320bd commit 3ca4598
Showing 1 changed file with 17 additions and 19 deletions.
36 changes: 17 additions & 19 deletions src/strings/index.js
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
}

0 comments on commit 3ca4598

Please sign in to comment.