Skip to content

Instantly share code, notes, and snippets.

@Woodsphreaker
Created November 12, 2017 14:44
Show Gist options
  • Save Woodsphreaker/2340adc70f7e3727f17fb79dcf026a37 to your computer and use it in GitHub Desktop.
Save Woodsphreaker/2340adc70f7e3727f17fb79dcf026a37 to your computer and use it in GitHub Desktop.
capitalize
const breakLine = (qtd = 3) => Array.from({ length: qtd }, () => console.log())
// strings
const str1 = 'lorem ipsum'
const str2 = 'testing FUNCTION'
const str3 = 'rest of params'
const str4 = 'ALL CAPITALIZED WORDS'
// common functions
const join = (arr, to = ' ') => arr.join(to)
const toArray = (str, to = ' ') => str.split(to)
const lcase = str => str.toLowerCase()
const ucase = str => str.toUpperCase()
const ucaseFirst = str => ucase(str.slice(0, 1))
const restOfString = str => str.slice(1)
// ############# Method 1 #############
const createWord = str => `${ucaseFirst(str)}${lcase(restOfString(str))}`
const capitalize_1 = str => join(toArray(str).map(createWord))
// usage
console.log(capitalize_1(str1))
console.log(capitalize_1(str2))
console.log(capitalize_1(str3))
console.log(capitalize_1(str4))
breakLine()
// ############# Method 2 #############
const createWord_2 = ([first, ...rest]) => `${ucase(first)}${lcase(join(rest, ''))}`
const capitalize_2 = str => join(toArray(str).map(createWord_2))
// usage
console.log(capitalize_2(str1))
console.log(capitalize_2(str2))
console.log(capitalize_2(str3))
console.log(capitalize_2(str4))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment