[BACKEND] String split scripting function (#877)
This commit is contained in:
parent
ba71abc466
commit
d80a63631a
3 changed files with 45 additions and 0 deletions
|
|
@ -565,6 +565,13 @@ slice
|
|||
:description:
|
||||
Returns the slice of the Array.
|
||||
|
||||
split
|
||||
~~~~~
|
||||
:spec: ``split(string: String, sep: String, max_split: Optional[Integer]) -> Array``
|
||||
|
||||
:description:
|
||||
Splits the input string into multiple strings.
|
||||
|
||||
string
|
||||
~~~~~~
|
||||
:spec: ``string(value: AnyArgument) -> String``
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
from typing import Optional
|
||||
|
||||
from ytdl_sub.script.types.array import Array
|
||||
from ytdl_sub.script.types.resolvable import AnyArgument
|
||||
from ytdl_sub.script.types.resolvable import Boolean
|
||||
from ytdl_sub.script.types.resolvable import Integer
|
||||
|
|
@ -80,6 +81,22 @@ class StringFunctions:
|
|||
|
||||
return String(string.value.replace(old.value, new.value))
|
||||
|
||||
@staticmethod
|
||||
def split(string: String, sep: String, max_split: Optional[Integer] = None) -> Array:
|
||||
"""
|
||||
:description:
|
||||
Splits the input string into multiple strings.
|
||||
"""
|
||||
if max_split is not None:
|
||||
return Array(
|
||||
[
|
||||
String(split_val)
|
||||
for split_val in string.value.split(sep=sep.value, maxsplit=max_split.value)
|
||||
]
|
||||
)
|
||||
|
||||
return Array([String(split_val) for split_val in string.value.split(sep=sep.value)])
|
||||
|
||||
@staticmethod
|
||||
def concat(*values: String) -> String:
|
||||
"""
|
||||
|
|
|
|||
|
|
@ -1,3 +1,6 @@
|
|||
from typing import List
|
||||
from typing import Optional
|
||||
|
||||
import pytest
|
||||
from unit.script.conftest import single_variable_output
|
||||
|
||||
|
|
@ -114,3 +117,21 @@ class TestNumericFunctions:
|
|||
def test_contains(self, value, expected_output):
|
||||
output = single_variable_output(f"{{%contains('a brown dog', '{value}')}}")
|
||||
assert output == expected_output
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"input_string, split, max_split, expected_output",
|
||||
[
|
||||
("no splits", " | ", None, ["no splits"]),
|
||||
("one | split", " | ", None, ["one", "split"]),
|
||||
("max | split | one", " | ", 1, ["max", "split | one"]),
|
||||
],
|
||||
)
|
||||
def test_split(
|
||||
self, input_string: str, split: str, max_split: Optional[int], expected_output: List[str]
|
||||
):
|
||||
if max_split:
|
||||
output = single_variable_output(f"{{%split('{input_string}', '{split}', {max_split})}}")
|
||||
else:
|
||||
output = single_variable_output(f"{{%split('{input_string}', '{split}')}}")
|
||||
|
||||
assert output == expected_output
|
||||
|
|
|
|||
Loading…
Reference in a new issue