diff --git a/README.md b/README.md index 4f1f6d4..6c5b9e7 100644 --- a/README.md +++ b/README.md @@ -111,6 +111,7 @@ assert rows == [{"name": "Joe"}, {"name": "William"}, {"name": "Jack"}, {"name": - regex-extract - replace - strip-whitespaces +- left-strip #### Validators diff --git a/magicparse/pre_processors.py b/magicparse/pre_processors.py index d3f65b9..cc1f25f 100644 --- a/magicparse/pre_processors.py +++ b/magicparse/pre_processors.py @@ -73,6 +73,18 @@ def key() -> str: return "strip-whitespaces" +class LeftStrip(PreProcessor): + def __init__(self, characters: str) -> None: + self.characters = characters + + def apply(self, value: str) -> str: + return value.lstrip(self.characters) + + @staticmethod + def key() -> str: + return "left-strip" + + class RegexExtract(PreProcessor): def __init__(self, pattern: str) -> None: pattern = re.compile(pattern) @@ -97,4 +109,4 @@ def key() -> str: return "regex-extract" -builtins = [LeftPadZeroes, Map, RegexExtract, Replace, StripWhitespaces] +builtins = [LeftPadZeroes, Map, RegexExtract, Replace, StripWhitespaces, LeftStrip] diff --git a/tests/test_pre_processors.py b/tests/test_pre_processors.py index cdec6b2..a9862e0 100644 --- a/tests/test_pre_processors.py +++ b/tests/test_pre_processors.py @@ -6,6 +6,7 @@ RegexExtract, Replace, StripWhitespaces, + LeftStrip ) import pytest from unittest import TestCase @@ -38,6 +39,10 @@ def test_strip_whitespaces(self): pre_processor = PreProcessor.build({"name": "strip-whitespaces"}) assert isinstance(pre_processor, StripWhitespaces) + def test_left_strip(self): + pre_processor = PreProcessor.build({"name": "left-strip", "parameters": {"characters": "0"}}) + assert isinstance(pre_processor, LeftStrip) + def test_regex_extract(self): pre_processor = PreProcessor.build( { @@ -113,6 +118,25 @@ def test_success(self): pre_processor = PreProcessor.build({"name": "strip-whitespaces"}) assert pre_processor.apply(" an input ") == "an input" +class TestLeftStrip(TestCase): + def test_do_nothing(self): + pre_processor = PreProcessor.build({ + "name": "left-strip", + "parameters": { + "characters": "0" + } + }) + assert pre_processor.apply("12345") == "12345" + + def test_success(self): + pre_processor = PreProcessor.build({ + "name": "left-strip", + "parameters": { + "characters": "0" + } + }) + assert pre_processor.apply("0000012345") == "12345" + class TestRegexExtract(TestCase): def test_build_without_value_group(self):