Skip to content

Commit e18cf22

Browse files
committed
✨ feat: useListMergeByKey
1 parent a87af22 commit e18cf22

File tree

5 files changed

+36
-3
lines changed

5 files changed

+36
-3
lines changed

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "usepy"
3-
version = "0.2.6"
3+
version = "0.2.7"
44
description = "usepy"
55
homepage = "https://usepy.code05.com/"
66
authors = ["miclon <jcnd@163.com>"]

src/usepy/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
'useListSort',
2929
'useListUnique',
3030
'useListify',
31+
'useListMergeByKey',
3132
'usePath',
3233
'useRandomString',
3334
'useRandomUUID',
@@ -96,6 +97,7 @@ def find_module_alias():
9697
'useListSome': 'usepy.core.useList:useListSome',
9798
'useListSort': 'usepy.core.useList:useListSort',
9899
'useListUnique': 'usepy.core.useList:useListUnique',
100+
'useListMergeByKey': 'usepy.core.useList:useListMergeByKey',
99101
'useRandomString': 'usepy.core.useRandom:useRandomString',
100102
'useRandomUUID': 'usepy.core.useRandom:useRandomUUID',
101103
'useStringMiddle': 'usepy.core.useString:useStringMiddle',

src/usepy/core/useList.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,3 +302,21 @@ def useListUnique(array: List, fn: Optional[Callable] = None):
302302
:return: 去重后数组
303303
"""
304304
return list(useList.dedupe(array, fn))
305+
306+
307+
def useListMergeByKey(collection1: List, collection2: List, key: str):
308+
"""
309+
数组中对象按key合并
310+
"""
311+
if not collection1 or not collection2:
312+
return collection1 or collection2
313+
if key not in collection1[0] or key not in collection2[0]:
314+
return collection1 + collection2
315+
array1_dict = useList.objs_to_obj(collection1, key)
316+
array2_dict = useList.objs_to_obj(collection2, key)
317+
for _key in array2_dict:
318+
if _key in array1_dict:
319+
array1_dict[_key].update(array2_dict[_key])
320+
else:
321+
array1_dict[_key] = array2_dict[_key]
322+
return list(array1_dict.values())

tests/test_core/test_useCleanHtml.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,9 @@ def tag_stripper():
2020
yield ts
2121
ts.close()
2222

23+
2324
def test_close_method(tag_stripper):
2425
extracted_data = tag_stripper.get_data()
2526
assert extracted_data == "<h1>Title</h1>"
2627
tag_stripper.close()
27-
assert tag_stripper.get_data() == ""
28+
assert tag_stripper.get_data() == ""

tests/test_core/test_useList.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import pytest
22

33
from usepy import useList, useListFilter, useListFlatten, useListDifference, useListEvery, useListSome, useListSort, \
4-
useListUnique
4+
useListUnique, useListMergeByKey
55

66

77
def test_split():
@@ -118,3 +118,15 @@ def test_useListEvery(array, fn, expected):
118118
)
119119
def test_useListSome(array, fn, expected):
120120
assert useListSome(array, fn) == expected
121+
122+
123+
@pytest.mark.parametrize(
124+
"array1,array2,key,expected",
125+
[
126+
([{"id": 1, "age": 18}], [{"id": 1, "name": "miclon"}], "id", [{"id": 1, "age": 18, "name": "miclon"}]),
127+
([{"id": 1, "age": 18}], [{"name": "miclon"}], "id", [{'id': 1, "age": 18}, {'name': 'miclon'}]),
128+
([{"id": 1, "age": 18}], [], "id", [{"id": 1, "age": 18}]),
129+
]
130+
)
131+
def test_useListMergeByKey(array1, array2, key, expected):
132+
assert useListMergeByKey(array1, array2, key) == expected

0 commit comments

Comments
 (0)