56 lines
1.9 KiB
JavaScript
56 lines
1.9 KiB
JavaScript
"use strict";
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
exports.CaseConvention = void 0;
|
|
exports.convertCase = convertCase;
|
|
exports.CaseConvention = Object.freeze({
|
|
Lowercase: 0,
|
|
Uppercase: 1,
|
|
PascalCase: 2,
|
|
CamelCase: 3,
|
|
SnakeCase: 4,
|
|
ScreamingSnakeCase: 5,
|
|
KebabCase: 6,
|
|
ScreamingKebabCase: 7
|
|
});
|
|
const wordBoundaryRegex = /[A-Z]{2,}(?=[A-Z][a-z]+[0-9]*|\b)|[A-Z]?[a-z]+[0-9]*|[A-Z]|[0-9]+/g;
|
|
function identifyWords(value) {
|
|
return value && value.match(wordBoundaryRegex);
|
|
}
|
|
const lower = (ch) => ch.toLowerCase();
|
|
const upper = (ch) => ch.toUpperCase();
|
|
const first = (xs) => xs && xs[0];
|
|
const tail = (xs) => xs && xs.slice(1);
|
|
function upperFirst(xs) {
|
|
return upper(first(xs)) + tail(xs);
|
|
}
|
|
function toPascalCase(words) {
|
|
return words.map(lower).map(upperFirst).join('');
|
|
}
|
|
const joinMap = (fn, delim, xs) => {
|
|
return xs.map(fn).join(delim);
|
|
};
|
|
function convertCase(value, convention) {
|
|
const words = identifyWords(value);
|
|
if (!words || words.length <= 0) {
|
|
return '';
|
|
}
|
|
switch (convention) {
|
|
case exports.CaseConvention.Lowercase:
|
|
return words.join('').toLowerCase();
|
|
case exports.CaseConvention.Uppercase:
|
|
return words.join('').toUpperCase();
|
|
case exports.CaseConvention.PascalCase:
|
|
return toPascalCase(words);
|
|
case exports.CaseConvention.CamelCase:
|
|
const pascal = toPascalCase(words);
|
|
return first(pascal).toLowerCase() + tail(pascal);
|
|
case exports.CaseConvention.SnakeCase:
|
|
return joinMap(lower, '_', words);
|
|
case exports.CaseConvention.ScreamingSnakeCase:
|
|
return joinMap(upper, '_', words);
|
|
case exports.CaseConvention.KebabCase:
|
|
return joinMap(lower, '-', words);
|
|
case exports.CaseConvention.ScreamingKebabCase:
|
|
return joinMap(upper, '-', words);
|
|
}
|
|
}
|