diff --git a/src/ytdl_sub/script/functions/string_functions.py b/src/ytdl_sub/script/functions/string_functions.py index 8e731674..6ec26ef3 100644 --- a/src/ytdl_sub/script/functions/string_functions.py +++ b/src/ytdl_sub/script/functions/string_functions.py @@ -16,6 +16,15 @@ class StringFunctions: """ return String(value=str(value.value)) + @staticmethod + def slice(string: String, start: Integer, end: Optional[Integer] = None) -> String: + """ + Returns the slice of the Array. + """ + if end is not None: + return String(string.value[start.value : end.value]) + return String(string.value[start.value :]) + @staticmethod def lower(string: String) -> String: """ diff --git a/tests/unit/script/functions/test_string_functions.py b/tests/unit/script/functions/test_string_functions.py index 5f0c226e..ad34a75a 100644 --- a/tests/unit/script/functions/test_string_functions.py +++ b/tests/unit/script/functions/test_string_functions.py @@ -98,3 +98,14 @@ class TestNumericFunctions: def test_pad_zero(self, values: str, expected_output: str): output = single_variable_output(f"{{%pad_zero({values})}}") assert output == expected_output + + @pytest.mark.parametrize( + "values, expected_output", + [ + ("'2012', 2", "12"), + ("'2012', 1, 3", "01"), + ], + ) + def test_slice(self, values, expected_output): + output = single_variable_output(f"{{%slice({values})}}") + assert output == expected_output