[FEATURE] Add join and strip string functions to scripting (#1218)
This commit is contained in:
parent
dfcbadb69d
commit
a4293398b7
3 changed files with 93 additions and 0 deletions
|
|
@ -660,6 +660,24 @@ contains_any
|
|||
:description:
|
||||
Returns true if any element in ``contains_array`` is in ``string``. False otherwise.
|
||||
|
||||
join
|
||||
~~~~
|
||||
:spec: ``join(separator: String, array: Array) -> String``
|
||||
|
||||
:description:
|
||||
Join all elements in the array together as a string, and insert the
|
||||
separator between them.
|
||||
|
||||
:usage:
|
||||
|
||||
.. code-block:: python
|
||||
|
||||
{
|
||||
%join( ", ", ["item1", "item2"] )
|
||||
}
|
||||
|
||||
# "item1, item2"
|
||||
|
||||
lower
|
||||
~~~~~
|
||||
:spec: ``lower(string: String) -> String``
|
||||
|
|
@ -710,6 +728,23 @@ string
|
|||
:description:
|
||||
Cast to String.
|
||||
|
||||
strip
|
||||
~~~~~
|
||||
:spec: ``strip(string: String) -> String``
|
||||
|
||||
:description:
|
||||
Strip a string of all its whitespace at the beginning and end.
|
||||
|
||||
:usage:
|
||||
|
||||
.. code-block:: python
|
||||
|
||||
{
|
||||
%trim(" delete the outer! ")
|
||||
}
|
||||
|
||||
# "delete the outer!"
|
||||
|
||||
titlecase
|
||||
~~~~~~~~~
|
||||
:spec: ``titlecase(string: String) -> String``
|
||||
|
|
|
|||
|
|
@ -176,3 +176,41 @@ class StringFunctions:
|
|||
# World
|
||||
"""
|
||||
return String(string.value.encode("utf-8").decode("unicode_escape"))
|
||||
|
||||
@staticmethod
|
||||
def join(separator: String, array: Array) -> String:
|
||||
"""
|
||||
:description:
|
||||
Join all elements in the array together as a string, and insert the
|
||||
separator between them.
|
||||
|
||||
:usage:
|
||||
|
||||
.. code-block:: python
|
||||
|
||||
{
|
||||
%join( ", ", ["item1", "item2"] )
|
||||
}
|
||||
|
||||
# "item1, item2"
|
||||
"""
|
||||
|
||||
return String(separator.value.join(str(val) for val in array.value))
|
||||
|
||||
@staticmethod
|
||||
def strip(string: String) -> String:
|
||||
"""
|
||||
:description:
|
||||
Strip a string of all its whitespace at the beginning and end.
|
||||
|
||||
:usage:
|
||||
|
||||
.. code-block:: python
|
||||
|
||||
{
|
||||
%trim(" delete the outer! ")
|
||||
}
|
||||
|
||||
# "delete the outer!"
|
||||
"""
|
||||
return String(string.value.strip())
|
||||
|
|
|
|||
|
|
@ -152,3 +152,23 @@ class TestNumericFunctions:
|
|||
output = single_variable_output(f"{{%split('{input_string}', {split})}}")
|
||||
|
||||
assert output == expected_output
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"value, expected_output",
|
||||
[
|
||||
("['a', 'b', 'c']", "a, b, c"),
|
||||
("['nope', [], {}]", "nope, [], {}"),
|
||||
("['a', 1, 3.14, True]", "a, 1, 3.14, true"),
|
||||
],
|
||||
)
|
||||
def test_join(self, value, expected_output):
|
||||
output = single_variable_output(f"{{%join(', ', {value})}}")
|
||||
assert output == expected_output
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"value, expected_output",
|
||||
[(" delete outer ", "delete outer"), (" delete me\n\n", "delete me")],
|
||||
)
|
||||
def test_strip(self, value, expected_output):
|
||||
output = single_variable_output(f"{{%strip('{value}')}}")
|
||||
assert output == expected_output
|
||||
|
|
|
|||
Loading…
Reference in a new issue