simple-string-utils-study
Version:
Simple string utility functions for study purposes
37 lines (33 loc) • 699 B
JavaScript
function capitalize(str) {
if (typeof str !== 'string' || str.length === 0) {
return str;
}
return str.charAt(0).toUpperCase() + str.slice(1).toLowerCase();
}
function reverse(str) {
if (typeof str !== 'string') {
return str;
}
return str.split('').reverse().join('');
}
function kebabCase(str) {
if (typeof str !== 'string') {
return str;
}
return str
.replace(/([a-z])([A-Z])/g, '$1-$2')
.replace(/\s+/g, '-')
.toLowerCase();
}
function wordCount(str) {
if (typeof str !== 'string') {
return 0;
}
return str.trim().split(/\s+/).filter(word => word.length > 0).length;
}
module.exports = {
capitalize,
reverse,
kebabCase,
wordCount
};