diff --git a/src/ytdl_sub/script/functions/string_functions.py b/src/ytdl_sub/script/functions/string_functions.py index d54f0744..ec7cc70e 100644 --- a/src/ytdl_sub/script/functions/string_functions.py +++ b/src/ytdl_sub/script/functions/string_functions.py @@ -2,6 +2,7 @@ from typing import Optional from ytdl_sub.script.types.resolvable import AnyArgument from ytdl_sub.script.types.resolvable import Integer +from ytdl_sub.script.types.resolvable import Numeric from ytdl_sub.script.types.resolvable import String @@ -60,3 +61,25 @@ class StringFunctions: Concatenate multiple Strings into a single String. """ return String("".join(list([val.value for val in values]))) + + @staticmethod + def pad(string: String, length: Integer, char: String) -> String: + """ + Pads the string to the given length + """ + output = string.value + while len(output) < length.value: + output = f"{char}{output}" + + return String(output) + + @staticmethod + def pad_zero(numeric: Numeric, length: Integer) -> String: + """ + Pads a numeric with zeros to the given length + """ + return StringFunctions.pad( + string=String(str(numeric.value)), + length=length, + char=String("0"), + ) diff --git a/tests/unit/script/functions/test_string_functions.py b/tests/unit/script/functions/test_string_functions.py index 7352debe..5f0c226e 100644 --- a/tests/unit/script/functions/test_string_functions.py +++ b/tests/unit/script/functions/test_string_functions.py @@ -76,3 +76,25 @@ class TestNumericFunctions: def test_concat(self, values: str, expected_output: str): output = single_variable_output(f"{{%concat({values})}}") assert output == expected_output + + @pytest.mark.parametrize( + "values, expected_output", + [ + ("'HI', 6, '.'", "....HI"), + ("'HI', 2, '.'", "HI"), + ], + ) + def test_pad(self, values: str, expected_output: str): + output = single_variable_output(f"{{%pad({values})}}") + assert output == expected_output + + @pytest.mark.parametrize( + "values, expected_output", + [ + ("2012, 6", "002012"), + ("2012, 2", "2012"), + ], + ) + def test_pad_zero(self, values: str, expected_output: str): + output = single_variable_output(f"{{%pad_zero({values})}}") + assert output == expected_output