mirror of
https://github.com/kennethreitz/pydantic.git
synced 2026-06-05 23:00:18 +00:00
new to_lower_camel() function (#3473)
* Create utils.py I needed non pascal case camel case support * write tests for new `to_lower_camel()` function * Create 3463-schlerp.md * added mention to to_lower_camel() * changed quotes to single tick * adding second blank line at end of file. * again, adding second blank line... Co-authored-by: Samuel Colvin <s@muelcolvin.com>
This commit is contained in:
@@ -0,0 +1 @@
|
||||
created new function `to_lower_camel()` for "non pascal case" camel case.
|
||||
@@ -143,7 +143,7 @@ _(This script is complete, it should run "as is")_
|
||||
|
||||
Here camel case refers to ["upper camel case"](https://en.wikipedia.org/wiki/Camel_case) aka pascal case
|
||||
e.g. `CamelCase`. If you'd like instead to use lower camel case e.g. `camelCase`,
|
||||
it should be trivial to modify the `to_camel` function above.
|
||||
instead use the `to_lower_camel` function.
|
||||
|
||||
## Alias Precedence
|
||||
|
||||
|
||||
@@ -303,6 +303,13 @@ def to_camel(string: str) -> str:
|
||||
return ''.join(word.capitalize() for word in string.split('_'))
|
||||
|
||||
|
||||
def to_lower_camel(string: str) -> str:
|
||||
if len(string) >= 1:
|
||||
pascal_string = to_camel(string)
|
||||
return pascal_string[0].lower() + pascal_string[1:]
|
||||
return string.lower()
|
||||
|
||||
|
||||
T = TypeVar('T')
|
||||
|
||||
|
||||
|
||||
@@ -37,6 +37,7 @@ from pydantic.utils import (
|
||||
lenient_issubclass,
|
||||
path_type,
|
||||
smart_deepcopy,
|
||||
to_lower_camel,
|
||||
truncate,
|
||||
unique_list,
|
||||
)
|
||||
@@ -528,6 +529,18 @@ def test_undefined_pickle():
|
||||
assert undefined2 is Undefined
|
||||
|
||||
|
||||
def test_on_lower_camel_zero_length():
|
||||
assert to_lower_camel('') == ''
|
||||
|
||||
|
||||
def test_on_lower_camel_one_length():
|
||||
assert to_lower_camel('a') == 'a'
|
||||
|
||||
|
||||
def test_on_lower_camel_many_length():
|
||||
assert to_lower_camel('i_like_turtles') == 'iLikeTurtles'
|
||||
|
||||
|
||||
def test_limited_dict():
|
||||
d = LimitedDict(10)
|
||||
d[1] = '1'
|
||||
|
||||
Reference in New Issue
Block a user