Skip to content

Commit

Permalink
Changed order of transformation arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
marverix committed Nov 16, 2021
1 parent 7b007b1 commit 0038868
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 10 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ print(casey.snake(subject))
print(casey.snake(subject, upper=True))
# Prints: EVERY_1_WORD_IS_VERY_IMPORTANT

def my_transformation(idx: int, word: str) -> str:
def my_transformation(word: str, idx: int) -> str:
if idx % 2 == 0:
return word.lower()
else:
Expand Down
28 changes: 20 additions & 8 deletions casey/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import re
from typing import List, Callable, Union
from inspect import signature


def clean(subject: str) -> str:
Expand Down Expand Up @@ -38,17 +39,28 @@ def clean_list(subject: str) -> List[str]:
def transform(subject: str, transformation: Union[Callable, None], glue=" ") -> str:
normalized = clean_list(subject)
words = []
for idx, word in enumerate(normalized):
if transformation:
w = transformation(idx, word)
else:
w = word
words.append(w)

if transformation:
sig = signature(transformation)
params_number = len(sig.parameters)

for idx, word in enumerate(normalized):
if params_number == 0:
w = transformation()
elif params_number == 1:
w = transformation(word)
elif params_number == 2:
w = transformation(word, idx)
else:
raise Exception("Wrong number of arguments for transformation")
words.append(w)
else:
words = normalized

return glue.join(words)


def _camel_transformation(idx: int, word: str) -> str:
def _camel_transformation(word: str, idx: int) -> str:
if idx == 0:
return word
else:
Expand All @@ -62,7 +74,7 @@ def camel(subject: str) -> str:
return transform(subject, _camel_transformation, "")


def _pascal_transformation(idx: int, word: str) -> str:
def _pascal_transformation(word: str, _) -> str:
return upper_first(word)


Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "casey"
version = "1.1.0"
version = "1.2.0"
description = "A simple library to support various naming conventions and convert strings from one to another"
authors = [
"Marek Sierociński <[email protected]>"
Expand Down
27 changes: 27 additions & 0 deletions tests/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,33 @@ def test_lower_first(self):
'Return string with very first letter lower'
)

def test_transform(self):
phrase = "Every 1 WORD, is very IMPORTANT!"

self.assertEqual(
casey.transform(phrase, None, '_'),
'every_1_WORD_is_very_IMPORTANT',
'Return glued with underscore string'
)

self.assertEqual(
casey.transform(phrase, lambda: "x", '_'),
'x_x_x_x_x_x',
'Return string for transformation without any argument'
)

self.assertEqual(
casey.transform(phrase, lambda word: word.upper(), '__'),
'EVERY__1__WORD__IS__VERY__IMPORTANT',
'Return string for transformation with one argument'
)

self.assertEqual(
casey.transform(phrase, lambda word, idx: word.upper() + str(idx), '__'),
'EVERY0__11__WORD2__IS3__VERY4__IMPORTANT5',
'Return string for transformation with two arguments'
)


if __name__ == '__main__':
unittest.main()

0 comments on commit 0038868

Please sign in to comment.