pad and pad_zero

This commit is contained in:
Jesse Bannon 2023-12-03 23:33:02 -08:00
parent fdcf86ef52
commit 6b01c5b83d
2 changed files with 45 additions and 0 deletions

View file

@ -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"),
)

View file

@ -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