capitalize-string-ts
Version:
capitalize string is easy
25 lines (24 loc) • 1.24 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.capitalizeString = exports.capitalizeWord = void 0;
var SPACE = ' ';
var capitalizeWord = function (str, letter) {
if (letter === void 0) { letter = 0; }
if (str[letter] === SPACE) {
return (0, exports.capitalizeWord)(str, letter + 1);
}
return "".concat(SPACE.repeat(letter)).concat(str[letter].toUpperCase()).concat(str.slice(letter + 1));
};
exports.capitalizeWord = capitalizeWord;
var capitalizeString = function (str, _a) {
var _b = _a === void 0 ? { separator: SPACE, isUppercaseAll: false } : _a, separator = _b.separator, isUppercaseAll = _b.isUppercaseAll;
if (typeof str !== 'string') {
throw new Error('value is not the string');
}
var splitedStr = str.split(separator !== null && separator !== void 0 ? separator : SPACE);
if (isUppercaseAll) {
return splitedStr.map(function (word) { return (0, exports.capitalizeWord)(word); }).join(separator !== null && separator !== void 0 ? separator : SPACE);
}
return "".concat((0, exports.capitalizeWord)(splitedStr[0]), " ").concat(splitedStr.slice(1));
};
exports.capitalizeString = capitalizeString;