@rashedmakkouk/dev-utils
Version:
Utility library.
53 lines (52 loc) • 1.8 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
/** Utilities */
const capitalize_1 = __importDefault(require("lodash/capitalize"));
const upperFirst_1 = __importDefault(require("lodash/upperFirst"));
/**
* Formats supplied string to defined case.
*
* {@link https://en.wikipedia.org/wiki/Letter_case#Stylistic_or_specialised_usage | Start Case}
*
* @returns Formatted string.
*/
function letterCase(text = '', options) {
if (!text) {
return '';
}
let nextText = '';
try {
const { letterCase, separators } = options;
nextText = !separators
? text.toLowerCase()
: text
.replace(new RegExp(`[${separators.join('|')}]+`, 'gi'), ' ')
.toLowerCase();
switch (letterCase) {
case 'title':
return (!separators ? nextText.replace(/[_-\s]+/gi, ' ') : nextText).replace(/\w+/gi, capitalize_1.default);
case 'sentence':
return (0, upperFirst_1.default)(nextText);
case 'upper':
return nextText.toUpperCase();
case 'lower':
return nextText;
case 'kebab':
nextText = nextText
.trim()
/** W: all non alphanumeric characters and white space. */
.replace(/[\W_-]+/gi, '-');
return nextText.slice(!nextText.startsWith('-') ? 0 : 1, !nextText.endsWith('-') ? undefined : -1);
}
}
catch (error) {
/**
* - Exception thrown.
*/
}
return nextText;
}
exports.default = letterCase;