case-converters
Version:
Change case functions for all cases in TypeScript and JavaScript. Combined version of all [`case-converters`](https://github.com/cvchauhan/case-converter) methods, so you do not need to install them separately. ESM and CJS bundles are included, also bac
100 lines (99 loc) • 2.97 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.Case = void 0;
// Exporting the Case class
class Case {
static camel(str) {
return str
.toLowerCase()
.replace(/([-_\s][a-z])/g, (group) => group.toUpperCase().replace("-", "").replace("_", "").replace(" ", ""));
}
static capital(str) {
const pieces = str.split(" ");
for (var i = 0; i < pieces.length; i++) {
var j = pieces[i].charAt(0).toUpperCase();
pieces[i] = j + pieces[i].substring(1);
}
return pieces.join(" ");
}
static constant(str) {
return str.toUpperCase().replace(/\s+/g, "_");
}
static dot(str) {
return str.toLowerCase().replace(/\s+/g, ".");
}
static no(str) {
return str.toLowerCase();
}
static pascal(str) {
return str
.toLowerCase()
.replace(/(?:^\w|[A-Z]|\b\w|\s+)/g, (match) => match.toUpperCase())
.replace(/\s+/g, "");
}
static path(str) {
return str.toLowerCase().replace(/\s+/g, "/");
}
static sentence(str) {
return str.charAt(0).toUpperCase() + str.slice(1).toLowerCase();
}
static snake(str) {
return str.toLowerCase().replace(/\s+/g, "_");
}
static train(str) {
return str
.replace(/\s+/g, "-")
.replace(/(?:^\w|[A-Z]|\b\w|\s+)/g, (match) => match.toUpperCase());
}
static kebap(str) {
return str.replace(/\s+/g, "-").toLowerCase();
}
static sponge(str) {
let upper = true;
return str
.split("")
.map((char) => {
if (char.match(/[a-zA-Z]/)) {
// Only change case for alphabetic characters
const result = upper ? char.toUpperCase() : char.toLowerCase();
upper = !upper; // Toggle the case for the next character
return result;
}
return char; // Keep non-alphabetic characters unchanged
})
.join("");
}
static swap(str) {
return str.toUpperCase();
}
static title(str) {
return str
.replace(/\b\w/g, (char) => char.toUpperCase())
.replace(/\s+/g, " ");
}
static upper(str) {
return str.toUpperCase();
}
static localeUpper(str, locale) {
return str.toLocaleUpperCase(locale);
}
static lower(str) {
return str.toLowerCase();
}
static localeLower(str, locale) {
return str.toLocaleLowerCase(locale);
}
static lowerFirst(str) {
return str.charAt(0).toLowerCase() + str.slice(1);
}
static upperFirst(str) {
return str.charAt(0).toUpperCase() + str.slice(1);
}
static isUpper(str) {
return str === str.toUpperCase();
}
static isLower(str) {
return str === str.toLowerCase();
}
}
exports.Case = Case;