Skip to content

Commit

Permalink
fix: handle empty keys and values in zip_object and zip_object_deep
Browse files Browse the repository at this point in the history
Fixes #235
  • Loading branch information
dgilland committed Nov 4, 2024
1 parent 1c63bda commit cdd84b4
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
15 changes: 11 additions & 4 deletions src/pydash/arrays.py
Original file line number Diff line number Diff line change
Expand Up @@ -2694,9 +2694,12 @@ def zip_object(keys, values=None):
.. versionchanged:: 4.0.0
Removed alias ``object_``.
"""

if values is None:
keys, values = unzip(keys)
keys_values = unzip(keys)
if len(keys_values) == 0:
keys, values = [], []
else:
keys, values = keys_values

return dict(zip(keys, values))

Expand All @@ -2722,8 +2725,12 @@ def zip_object_deep(
.. versionadded:: 4.0.0
"""
if values is None: # pragma: no cover
keys, values = unzip(keys)
if values is None:
keys_values = unzip(keys)
if len(keys_values) == 0:
keys, values = [], []
else:
keys, values = keys_values

obj: t.Dict[t.Any, t.Any] = {}
for idx, key in enumerate(keys):
Expand Down
5 changes: 5 additions & 0 deletions tests/test_arrays.py
Original file line number Diff line number Diff line change
Expand Up @@ -915,6 +915,8 @@ def test_zip_(case, expected):
[
((["moe", "larry"], [30, 40]), {"moe": 30, "larry": 40}),
(([["moe", 30], ["larry", 40]],), {"moe": 30, "larry": 40}),
(([],), {}),
(([], []), {}),
],
)
def test_zip_object(case, expected):
Expand All @@ -925,7 +927,10 @@ def test_zip_object(case, expected):
"case,expected",
[
((["a.b.c", "a.b.d"], [1, 2]), {"a": {"b": {"c": 1, "d": 2}}}),
(([["a.b.c", 1], ["a.b.d", 2]],), {"a": {"b": {"c": 1, "d": 2}}}),
((["a.b[0].c", "a.b[1].d"], [1, 2]), {"a": {"b": [{"c": 1}, {"d": 2}]}}),
(([],), {}),
(([], []), {}),
],
)
def test_zip_object_deep(case, expected):
Expand Down

0 comments on commit cdd84b4

Please sign in to comment.