Created
November 30, 2019 13:16
-
-
Save nicolasmendonca/ca0ae89f6831ee72251740462351b86d to your computer and use it in GitHub Desktop.
String utilities
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
const capitalize = require('./index'); | |
test('Capitalize is a function', () => { | |
expect(typeof capitalize).toEqual('function'); | |
}); | |
test('capitalizes the first letter of every word in a sentence', () => { | |
expect(capitalize('hi there, how is it going?')).toEqual( | |
'Hi There, How Is It Going?' | |
); | |
}); | |
test('capitalizes the first letter', () => { | |
expect(capitalize('i love breakfast at bill miller bbq')).toEqual( | |
'I Love Breakfast At Bill Miller Bbq' | |
); | |
}); |
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
// --- Directions | |
// Write a function that accepts a string. The function should | |
// capitalize the first letter of each word in the string then | |
// return the capitalized string. | |
// --- Examples | |
// capitalize('a short sentence') --> 'A Short Sentence' | |
// capitalize('a lazy fox') --> 'A Lazy Fox' | |
// capitalize('look, it is working!') --> 'Look, It Is Working!' | |
/** | |
* @param {string} str | |
* @returns {string} Capitalized string | |
*/ | |
function capitalize(str) { | |
return str.toLowerCase().replace(/^\w|\s\w/g, match => match.toUpperCase()); | |
} | |
module.exports = capitalize; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment