[DEV] %map_extend scripting function

This commit is contained in:
Jesse Bannon 2024-01-18 23:40:14 -08:00
parent 213580ee84
commit 0f37558a60
3 changed files with 32 additions and 1 deletions

View file

@ -415,6 +415,14 @@ map_enumerate
Apply a lambda function on the Map, where each arg
passed to the lambda function is ``idx, key, value`` as three separate args.
map_extend
~~~~~~~~~~
:spec: ``map_extend(maps: Map, ...) -> Map``
:description:
Return maps combined in the order from left-to-right. Duplicate keys will use the
right-most map's value.
map_get
~~~~~~~
:spec: ``map_get(mapping: Map, key: AnyArgument, default: Optional[AnyArgument]) -> AnyArgument``

View file

@ -1,4 +1,4 @@
from typing import Optional
from typing import Optional, Dict
from ytdl_sub.script.types.array import Array
from ytdl_sub.script.types.map import Map
@ -66,6 +66,19 @@ class MapFunctions:
)
return mapping.value[key]
@staticmethod
def map_extend(*maps: Map) -> Map:
"""
:description:
Return maps combined in the order from left-to-right. Duplicate keys will use the
right-most map's value.
"""
output_dict: Dict = {}
for map_i in maps:
output_dict |= map_i.value
return Map(output_dict)
@staticmethod
def map_get_non_empty(mapping: Map, key: AnyArgument, default: AnyArgument) -> AnyArgument:
"""

View file

@ -138,3 +138,13 @@ class TestMapFunctions:
FunctionRuntimeException, match="Tried and failed to cast Integer as a Map"
):
single_variable_output("{%map(1)}")
def test_map_extend(self):
output = single_variable_output("""{
%map_extend(
{'key': 'value', 1: 3},
{'key': 'override'}
{'new': [1, 2]}
)
}""")
assert output == {'key': 'override', 'new': [1, 2], 1: 3}