Skip to content

Commit

Permalink
✨ feat: useListMergeByKey
Browse files Browse the repository at this point in the history
  • Loading branch information
mic1on committed Oct 24, 2023
1 parent a87af22 commit e18cf22
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 3 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "usepy"
version = "0.2.6"
version = "0.2.7"
description = "usepy"
homepage = "https://usepy.code05.com/"
authors = ["miclon <[email protected]>"]
Expand Down
2 changes: 2 additions & 0 deletions src/usepy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
'useListSort',
'useListUnique',
'useListify',
'useListMergeByKey',
'usePath',
'useRandomString',
'useRandomUUID',
Expand Down Expand Up @@ -96,6 +97,7 @@ def find_module_alias():
'useListSome': 'usepy.core.useList:useListSome',
'useListSort': 'usepy.core.useList:useListSort',
'useListUnique': 'usepy.core.useList:useListUnique',
'useListMergeByKey': 'usepy.core.useList:useListMergeByKey',
'useRandomString': 'usepy.core.useRandom:useRandomString',
'useRandomUUID': 'usepy.core.useRandom:useRandomUUID',
'useStringMiddle': 'usepy.core.useString:useStringMiddle',
Expand Down
18 changes: 18 additions & 0 deletions src/usepy/core/useList.py
Original file line number Diff line number Diff line change
Expand Up @@ -302,3 +302,21 @@ def useListUnique(array: List, fn: Optional[Callable] = None):
:return: 去重后数组
"""
return list(useList.dedupe(array, fn))


def useListMergeByKey(collection1: List, collection2: List, key: str):
"""
数组中对象按key合并
"""
if not collection1 or not collection2:
return collection1 or collection2
if key not in collection1[0] or key not in collection2[0]:
return collection1 + collection2
array1_dict = useList.objs_to_obj(collection1, key)
array2_dict = useList.objs_to_obj(collection2, key)
for _key in array2_dict:
if _key in array1_dict:
array1_dict[_key].update(array2_dict[_key])
else:
array1_dict[_key] = array2_dict[_key]
return list(array1_dict.values())
3 changes: 2 additions & 1 deletion tests/test_core/test_useCleanHtml.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@ def tag_stripper():
yield ts
ts.close()


def test_close_method(tag_stripper):
extracted_data = tag_stripper.get_data()
assert extracted_data == "<h1>Title</h1>"
tag_stripper.close()
assert tag_stripper.get_data() == ""
assert tag_stripper.get_data() == ""
14 changes: 13 additions & 1 deletion tests/test_core/test_useList.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import pytest

from usepy import useList, useListFilter, useListFlatten, useListDifference, useListEvery, useListSome, useListSort, \
useListUnique
useListUnique, useListMergeByKey


def test_split():
Expand Down Expand Up @@ -118,3 +118,15 @@ def test_useListEvery(array, fn, expected):
)
def test_useListSome(array, fn, expected):
assert useListSome(array, fn) == expected


@pytest.mark.parametrize(
"array1,array2,key,expected",
[
([{"id": 1, "age": 18}], [{"id": 1, "name": "miclon"}], "id", [{"id": 1, "age": 18, "name": "miclon"}]),
([{"id": 1, "age": 18}], [{"name": "miclon"}], "id", [{'id': 1, "age": 18}, {'name': 'miclon'}]),
([{"id": 1, "age": 18}], [], "id", [{"id": 1, "age": 18}]),
]
)
def test_useListMergeByKey(array1, array2, key, expected):
assert useListMergeByKey(array1, array2, key) == expected

0 comments on commit e18cf22

Please sign in to comment.