From 0038868f5831e9eab62778f7a729da8587908a89 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marek=20Sieroci=C5=84ski?= Date: Tue, 16 Nov 2021 17:44:47 +0100 Subject: [PATCH] Changed order of transformation arguments --- README.md | 2 +- casey/__init__.py | 28 ++++++++++++++++++++-------- pyproject.toml | 2 +- tests/__main__.py | 27 +++++++++++++++++++++++++++ 4 files changed, 49 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 8d14965..7d8f813 100644 --- a/README.md +++ b/README.md @@ -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: diff --git a/casey/__init__.py b/casey/__init__.py index dbb1fbc..8cf2307 100644 --- a/casey/__init__.py +++ b/casey/__init__.py @@ -1,5 +1,6 @@ import re from typing import List, Callable, Union +from inspect import signature def clean(subject: str) -> str: @@ -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: @@ -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) diff --git a/pyproject.toml b/pyproject.toml index 9c05617..3a76660 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -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 " diff --git a/tests/__main__.py b/tests/__main__.py index 8343757..c6bd420 100644 --- a/tests/__main__.py +++ b/tests/__main__.py @@ -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()