Files
herolib/lib/core/texttools/casing.v
2025-03-24 06:44:39 +01:00

39 lines
786 B
V

module texttools
pub fn snake_case(s string) string {
return separate_words(s).join('_')
}
pub fn title_case(s string) string {
return separate_words(s).join(' ').title()
}
pub fn pascal_case(s string) string {
mut pascal := s.replace('_', ' ')
return pascal.title().replace(' ', '')
}
pub fn camel_case(s string) string {
return pascal_case(s).uncapitalize()
}
const separators = ['.', '_', '-', '/', ' ', ':', ',', ';']
fn separate_words(s string) []string {
mut words := []string{}
mut word := ''
for _, c in s {
if (c.is_capital() || c.ascii_str() in separators) && word != '' {
words << word.to_lower()
word = ''
}
if c.ascii_str() !in separators {
word += c.ascii_str().to_lower()
}
}
if word != '' {
words << word.to_lower()
}
return words
}