Skip to content

Instantly share code, notes, and snippets.

@nicolasmendonca
Created November 30, 2019 13:16
Show Gist options
  • Save nicolasmendonca/ca0ae89f6831ee72251740462351b86d to your computer and use it in GitHub Desktop.
Save nicolasmendonca/ca0ae89f6831ee72251740462351b86d to your computer and use it in GitHub Desktop.
String utilities
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'
);
});
// --- 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