Skip to content

Commit 116cf4f

Browse files
committed
Added possibility to create own transformations
1 parent cb55f1c commit 116cf4f

File tree

3 files changed

+55
-19
lines changed

3 files changed

+55
-19
lines changed

README.md

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -30,26 +30,30 @@ import casey
3030

3131
subject = "every 1 WORD is very IMPORTANT"
3232

33-
subject = casey.camel(subject)
34-
print(subject)
33+
print(casey.camel(subject))
3534
# Prints: every1WORDIsVeryIMPORTANT
3635

37-
subject = casey.kebab(subject)
38-
print(subject)
36+
print(casey.kebab(subject))
3937
# Prints: every-1-WORD-is-very-IMPORTANT
4038

41-
subject = casey.pascal(subject)
42-
print(subject)
39+
print(casey.pascal(subject))
4340
# Prints: Every1WORDIsVeryIMPORTANT
4441

45-
subject = casey.snake(subject)
46-
print(subject)
42+
print(casey.snake(subject))
4743
# Prints: every_1_WORD_is_very_IMPORTANT
4844

49-
subject = casey.snake(subject, upper=True)
50-
print(subject)
45+
print(casey.snake(subject, upper=True))
5146
# Prints: EVERY_1_WORD_IS_VERY_IMPORTANT
5247

48+
def my_transformation(idx: int, word: str) -> str:
49+
if idx % 2 == 0:
50+
return word.lower()
51+
else:
52+
return word.upper()
53+
54+
print(casey.transform(subject, my_transformation, "_"))
55+
# Prints: every_1_word_IS_very_IMPORTANT
56+
5357
```
5458

5559
### API
@@ -88,9 +92,15 @@ print(subject)
8892

8993
Returns string with lower first letter (A-Z).
9094

95+
* `transform(subject: str, transformation: Callable, glue=" ") -> str: ...`
96+
97+
Returns string transformed by the transformation function.
98+
The transformation function accepts 2 parameters: current word index (int), and a word itself (str).
99+
Glue is the string used to concat transformed words into one string.
100+
91101
## License
92102

93103
This project is licensed under Apache-2.0 License - see the [LICENSE](LICENSE) file for details.
94104

95105

96-
[![FOSSA Status](https://app.fossa.com/api/projects/git%2Bgithub.com%2Fmarverix%2Fcasey.svg?type=large)](https://app.fossa.com/projects/git%2Bgithub.com%2Fmarverix%2Fcasey?ref=badge_large)
106+
[![FOSSA Status](https://app.fossa.com/api/projects/git%2Bgithub.com%2Fmarverix%2Fcasey.svg?type=large)](https://app.fossa.com/projects/git%2Bgithub.com%2Fmarverix%2Fcasey?ref=badge_large)

casey/__init__.py

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import re
2+
from typing import List, Callable, Union
23

34

45
def clean(subject: str) -> str:
@@ -30,35 +31,60 @@ def clean(subject: str) -> str:
3031
return subject
3132

3233

34+
def clean_list(subject: str) -> List[str]:
35+
return clean(subject).split(" ")
36+
37+
38+
def transform(subject: str, transformation: Union[Callable, None], glue=" ") -> str:
39+
normalized = clean_list(subject)
40+
words = []
41+
for idx, word in enumerate(normalized):
42+
if transformation:
43+
w = transformation(idx, word)
44+
else:
45+
w = word
46+
words.append(w)
47+
48+
return glue.join(words)
49+
50+
51+
def _camel_transformation(idx: int, word: str) -> str:
52+
if idx == 0:
53+
return word
54+
else:
55+
return upper_first(word)
56+
57+
3358
def camel(subject: str) -> str:
3459
"""
3560
Returns string in camelCase
3661
"""
37-
normalized_subject = clean(subject)
38-
return re.sub(r" [a-z0-9]", lambda m: m.group(0)[1].upper(), normalized_subject, flags=re.IGNORECASE)
62+
return transform(subject, _camel_transformation, "")
63+
64+
65+
def _pascal_transformation(idx: int, word: str) -> str:
66+
return upper_first(word)
3967

4068

4169
def pascal(subject: str) -> str:
4270
"""
4371
Returns string in PascalCase
4472
"""
45-
return upper_first(camel(subject))
73+
return transform(subject, _pascal_transformation, "")
4674

4775

4876
def kebab(subject: str) -> str:
4977
"""
5078
Returns string in kebab-case
5179
"""
52-
normalized_subject = clean(subject)
53-
return re.sub(r"\s", '-', normalized_subject)
80+
return transform(subject, None, "-")
5481

5582

5683
def snake(subject: str, upper=False) -> str:
5784
"""
5885
Returns string in snake_case
5986
"""
60-
normalized_subject = clean(subject)
61-
s = re.sub(r"\s", '_', normalized_subject)
87+
s = transform(subject, None, "_")
6288

6389
if upper:
6490
s = s.upper()

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "casey"
3-
version = "1.0.3"
3+
version = "1.1.0"
44
description = "A simple library to support various naming conventions and convert strings from one to another"
55
authors = [
66
"Marek Sierociński <mareksierocinski@gmail.com>"

0 commit comments

Comments
 (0)