mirror of
https://github.com/FH-Complete/FHC-Core.git
synced 2026-07-26 00:54:27 +00:00
Merge branch 'master' into cis40_2026-05_ma_rc
This commit is contained in:
@@ -16,4 +16,49 @@ export function capitalize(string) {
|
||||
|
||||
return '';
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
export function convertCase(string, from, to) {
|
||||
const possibleCases = ["kebab", "snake", "camel"];
|
||||
if (
|
||||
from === to ||
|
||||
!possibleCases.includes(from) ||
|
||||
!possibleCases.includes(to)
|
||||
)
|
||||
return string;
|
||||
|
||||
const individualWords =
|
||||
from === "kebab"
|
||||
? string.split("-")
|
||||
: from === "snake"
|
||||
? string.split("_")
|
||||
: splitWordsInCamelCase(string);
|
||||
|
||||
const formattedIndividualWords = ["snake", "kebab"].includes(to)
|
||||
? individualWords.map((word) => word.toLowerCase())
|
||||
: individualWords.map((word, index) => {
|
||||
if (!index) return word.toLowerCase();
|
||||
return word.slice(0, 1).toUpperCase() + word.slice(1).toLowerCase();
|
||||
});
|
||||
|
||||
const joinCharacter = to === "snake" ? "_" : to === "kebab" ? "-" : "";
|
||||
return formattedIndividualWords.join(joinCharacter);
|
||||
}
|
||||
|
||||
function splitWordsInCamelCase(string) {
|
||||
const lastUpperCaseLetterIndex = string
|
||||
.split("")
|
||||
.findLastIndex((character) => {
|
||||
return character.toUpperCase() === character;
|
||||
});
|
||||
|
||||
if (lastUpperCaseLetterIndex < 1) {
|
||||
return [string];
|
||||
}
|
||||
|
||||
let splitWords = splitWordsInCamelCase(
|
||||
string.slice(0, lastUpperCaseLetterIndex),
|
||||
);
|
||||
splitWords.push(string.slice(lastUpperCaseLetterIndex));
|
||||
return splitWords;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user