UNPKG

sectom

Version:

Sectom is a useful npm package that has multiple easy to use functions.

40 lines (35 loc) 1.17 kB
/** * Changes the first letter of every word in a string from lowercase to uppercase * @param {string} s * @example * propercase("hello my name is") // Hello My Name Is * @returns {string} */ function propercase(str) { if (Array.isArray(str)) throw new TypeError(`"str" must be a string and not an array!`); var splitStr = str.toLowerCase().split(" "); for (var i = 0; i < splitStr.length; i++) { // You do not need to check if i is larger than splitStr length, as your for does that for you // Assign it back to the array splitStr[i] = splitStr[i].charAt(0).toUpperCase() + splitStr[i].substring(1); } // Directly return the joined string return splitStr.join(" "); } /** * Changes the first letter of a single string from lowercase to uppercase * @param {string} s * @example * capitalize("hello") // Hello * capitalize("HELLO") // HELLO * capitalize("HELLO") // HELLO * @returns {string} */ function capitalize(s) { if (typeof s !== "string") return ""; return s.charAt(0).toUpperCase() + s.slice(1); } exports.capitalize = capitalize; exports.propercase = propercase;