Coverage for src / lexigram / contracts / cli / naming.py: 62%

8 statements  

« prev     ^ index     » next       coverage.py v7.13.5, created at 2026-08-19 05:41 +0800

1"""Naming utilities for CLI code generation.""" 

2 

3from __future__ import annotations 

4 

5import re 

6 

7 

8def to_snake_case(name: str) -> str: 

9 """Convert a PascalCase or camelCase string to snake_case. 

10 

11 Args: 

12 name: The string to convert. 

13 

14 Returns: 

15 The snake_case representation. 

16 """ 

17 return re.sub(r"(?<!^)(?=[A-Z])", "_", name).lower() 

18 

19 

20def to_camel_case(name: str) -> str: 

21 """Convert snake_case to camelCase. 

22 

23 Args: 

24 name: The snake_case string to convert. 

25 

26 Returns: 

27 The camelCase representation. 

28 """ 

29 components = name.split("_") 

30 return components[0] + "".join(x.title() for x in components[1:]) 

31 

32 

33__all__ = ["to_camel_case", "to_snake_case"]