Skip to content
This repository was archived by the owner on Apr 15, 2025. It is now read-only.

Commit 1fa9331

Browse files
RobertCraigielxxonx
andcommitted
chore(internal): add change case util functions
Co-Authored-By: lxxonx <leeonechang92@gmail.com>
1 parent 5096b6f commit 1fa9331

2 files changed

Lines changed: 107 additions & 1 deletion

File tree

src/prisma/generator/utils.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1+
from __future__ import annotations
2+
13
import os
4+
import re
25
import shutil
36
from typing import TYPE_CHECKING, Any, Dict, List, Union, TypeVar, Iterator
47
from pathlib import Path
@@ -122,3 +125,46 @@ def clean_multiline(string: str) -> str:
122125
assert string, 'Expected non-empty string'
123126
lines = string.splitlines()
124127
return '\n'.join([dedent(lines[0]), *lines[1:]])
128+
129+
130+
# https://github.com/nficano/humps/blob/master/humps/main.py
131+
132+
ACRONYM_RE = re.compile(r'([A-Z\d]+)(?=[A-Z\d]|$)')
133+
PASCAL_RE = re.compile(r'([^\-_]+)')
134+
SPLIT_RE = re.compile(r'([\-_]*[A-Z][^A-Z]*[\-_]*)')
135+
UNDERSCORE_RE = re.compile(r'(?<=[^\-_])[\-_]+[^\-_]')
136+
137+
138+
def to_snake_case(input_str: str) -> str:
139+
if to_camel_case(input_str) == input_str or to_pascal_case(input_str) == input_str: # if camel case or pascal case
140+
input_str = ACRONYM_RE.sub(lambda m: m.group(0).title(), input_str)
141+
input_str = '_'.join(s for s in SPLIT_RE.split(input_str) if s)
142+
return input_str.lower()
143+
else:
144+
input_str = re.sub(r'[^a-zA-Z0-9]', '_', input_str)
145+
input_str = input_str.lower().strip('_')
146+
147+
return input_str
148+
149+
150+
def to_camel_case(input_str: str) -> str:
151+
if len(input_str) != 0 and not input_str[:2].isupper():
152+
input_str = input_str[0].lower() + input_str[1:]
153+
return UNDERSCORE_RE.sub(lambda m: m.group(0)[-1].upper(), input_str)
154+
155+
156+
def to_pascal_case(input_str: str) -> str:
157+
def _replace_fn(match: re.Match[str]) -> str:
158+
return match.group(1)[0].upper() + match.group(1)[1:]
159+
160+
input_str = to_camel_case(PASCAL_RE.sub(_replace_fn, input_str))
161+
return input_str[0].upper() + input_str[1:] if len(input_str) != 0 else input_str
162+
163+
164+
def to_constant_case(input_str: str) -> str:
165+
"""Converts to snake case + uppercase, examples:
166+
167+
foo_bar -> FOO_BAR
168+
fooBar -> FOO_BAR
169+
"""
170+
return to_snake_case(input_str).upper()

tests/test_generation/test_utils.py

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,12 @@
1-
from prisma.generator.utils import copy_tree
1+
import pytest
2+
3+
from prisma.generator.utils import (
4+
copy_tree,
5+
to_camel_case,
6+
to_snake_case,
7+
to_pascal_case,
8+
to_constant_case,
9+
)
210

311
from ..utils import Testdir
412

@@ -26,3 +34,55 @@ def test_copy_tree_ignores_files(testdir: Testdir) -> None:
2634
assert files[0].name == 'bar.py'
2735
assert files[1].name == 'foo.py'
2836
assert files[2].name == 'hello.py'
37+
38+
39+
@pytest.mark.parametrize(
40+
'input_str,expected',
41+
[
42+
('snake_case_test', 'snake_case_test'),
43+
('PascalCaseTest', 'pascal_case_test'),
44+
('camelCaseTest', 'camel_case_test'),
45+
('Mixed_Case_Test', 'mixed_case_test'),
46+
],
47+
)
48+
def test_to_snake_case(input_str: str, expected: str) -> None:
49+
assert to_snake_case(input_str) == expected
50+
51+
52+
@pytest.mark.parametrize(
53+
'input_str,expected',
54+
[
55+
('snake_case_test', 'SnakeCaseTest'),
56+
('PascalCaseTest', 'PascalCaseTest'),
57+
('camelCaseTest', 'CamelCaseTest'),
58+
('Mixed_Case_Test', 'MixedCaseTest'),
59+
],
60+
)
61+
def test_to_pascal_case(input_str: str, expected: str) -> None:
62+
assert to_pascal_case(input_str) == expected
63+
64+
65+
@pytest.mark.parametrize(
66+
'input_str,expected',
67+
[
68+
('snake_case_test', 'snakeCaseTest'),
69+
('PascalCaseTest', 'pascalCaseTest'),
70+
('camelCaseTest', 'camelCaseTest'),
71+
('Mixed_Case_Test', 'mixedCaseTest'),
72+
],
73+
)
74+
def test_to_camel_case(input_str: str, expected: str) -> None:
75+
assert to_camel_case(input_str) == expected
76+
77+
78+
@pytest.mark.parametrize(
79+
'input_str,expected',
80+
[
81+
('snake_case_test', 'SNAKE_CASE_TEST'),
82+
('PascalCaseTest', 'PASCAL_CASE_TEST'),
83+
('camelCaseTest', 'CAMEL_CASE_TEST'),
84+
('Mixed_Case_Test', 'MIXED_CASE_TEST'),
85+
],
86+
)
87+
def test_to_constant_case(input_str: str, expected: str) -> None:
88+
assert to_constant_case(input_str) == expected

0 commit comments

Comments
 (0)