add case fixing tools

This commit is contained in:
Timur Gordon
2025-03-13 12:40:50 +01:00
parent c09e424890
commit 28359984ff
5 changed files with 7 additions and 39 deletions

View File

@@ -22,7 +22,7 @@ const separators = ['.', '_', '-', '/', ' ', ':', ',', ';']
fn separate_words(s string) []string {
mut words := []string{}
mut word := ''
for i, c in s {
for _, c in s {
if (c.is_capital() || c.ascii_str() in separators) && word != '' {
words << word.to_lower()
word = ''

View File

@@ -84,38 +84,6 @@ pub fn name_fix_no_underscore(name string) string {
return x
}
pub fn name_fix_snake_to_pascal(name string) string {
x := name.replace('_', ' ')
p := x.title().replace(' ', '')
return p
}
pub fn name_fix_dot_notation_to_pascal(name string) string {
x := name.replace('.', ' ')
p := x.title().replace(' ', '')
return p
}
pub fn name_fix_pascal(name string) string {
name_ := name_fix_snake_to_pascal(name)
return name_fix_dot_notation_to_pascal(name_)
}
pub fn name_fix_pascal_to_snake(name string) string {
mut fixed := ''
for i, c in name {
if c.is_capital() && i != 0 {
fixed += '_'
}
fixed += c.ascii_str()
}
return fixed.to_lower()
}
pub fn name_fix_dot_notation_to_snake_case(name string) string {
return name.replace('.', '_')
}
// remove underscores and extension
pub fn name_fix_no_underscore_no_ext(name_ string) string {
return name_fix_keepext(name_).all_before_last('.').replace('_', '')

View File

@@ -41,7 +41,7 @@ The TextTools module provides a comprehensive set of utilities for text manipula
- `name_fix_keepspace(name string) !string` - Like name_fix but preserves spaces
- `name_fix_no_ext(name_ string) string` - Removes file extension
- `name_fix_snake_to_pascal(name string) string` - Converts snake_case to PascalCase
- `name_fix_pascal_to_snake(name string) string` - Converts PascalCase to snake_case
- `snake_case(name string) string` - Converts PascalCase to snake_case
- `name_split(name string) !(string, string)` - Splits name into site and page components
### Text Splitting
@@ -103,7 +103,7 @@ text := texttools.dedent(" line1\n line2")
### Name Processing
```v
// Convert to snake case
name := texttools.name_fix_pascal_to_snake("HelloWorld")
name := texttools.snake_case("HelloWorld")
// Result: "hello_world"
// Convert to pascal case

View File

@@ -19,7 +19,7 @@ fn decode_struct[T](_ T, data string) !T {
mut typ := T{}
$if T is $struct {
obj_name := texttools.name_fix_pascal_to_snake(T.name.all_after_last('.'))
obj_name := texttools.snake_case(T.name.all_after_last('.'))
action_name := 'define.${obj_name}'
actions_split := data.split('!!')
actions := actions_split.filter(it.starts_with(action_name))

View File

@@ -118,7 +118,7 @@ pub fn (mut e Encoder) encode_struct[T](t T) ! {
mut mytype := reflection.type_of[T](t)
struct_attrs := attrs_get_reflection(mytype)
mut action_name := texttools.name_fix_pascal_to_snake(T.name.all_after_last('.'))
mut action_name := texttools.snake_case(T.name.all_after_last('.'))
if 'alias' in struct_attrs {
action_name = struct_attrs['alias'].to_lower()
}