mirror of
https://github.com/earthjasonlin/star-rail-warp-export.git
synced 2025-07-07 14:40:16 +08:00
Departure commit
This commit is contained in:
40
src/main/utils/mergeData.js
Normal file
40
src/main/utils/mergeData.js
Normal file
@ -0,0 +1,40 @@
|
||||
const mergeList = (a, b) => {
|
||||
if (!a || !a.length) return b || []
|
||||
if (!b || !b.length) return a
|
||||
const list = [...b, ...a]
|
||||
const result = []
|
||||
const idSet = new Set()
|
||||
list.forEach(item => {
|
||||
if (!idSet.has(item.id)) {
|
||||
result.push(item)
|
||||
}
|
||||
idSet.add(item.id)
|
||||
})
|
||||
return result.sort((m, n) => {
|
||||
const num = BigInt(m.id) - BigInt(n.id)
|
||||
if (num > 0) {
|
||||
return 1
|
||||
} else if (num < 0) {
|
||||
return -1
|
||||
}
|
||||
return 0
|
||||
})
|
||||
}
|
||||
|
||||
const mergeData = (local, origin) => {
|
||||
if (local && local.result) {
|
||||
const localResult = local.result
|
||||
const localUid = local.uid
|
||||
const originUid = origin.uid
|
||||
if (localUid !== originUid) return origin.result
|
||||
const originResult = new Map()
|
||||
for (let [key, value] of origin.result) {
|
||||
const newVal = mergeList(localResult.get(key), value)
|
||||
originResult.set(key, newVal)
|
||||
}
|
||||
return originResult
|
||||
}
|
||||
return origin.result
|
||||
}
|
||||
|
||||
module.exports = { mergeData, mergeList }
|
72
src/main/utils/mergeData.test.js
Normal file
72
src/main/utils/mergeData.test.js
Normal file
@ -0,0 +1,72 @@
|
||||
const { mergeList } = require('./mergeData')
|
||||
|
||||
test('mergeList successed', () => {
|
||||
const listA = [{
|
||||
"id": "1682521800010412850",
|
||||
},
|
||||
{
|
||||
"id": "1682521800010412950",
|
||||
}]
|
||||
|
||||
const listB = [{
|
||||
"id": "1682521800010412900",
|
||||
}]
|
||||
|
||||
expect(mergeList(listA, listB)).toEqual([
|
||||
{
|
||||
"id": "1682521800010412850",
|
||||
},
|
||||
{
|
||||
"id": "1682521800010412900",
|
||||
},
|
||||
{
|
||||
"id": "1682521800010412950",
|
||||
}
|
||||
])
|
||||
})
|
||||
|
||||
test('mergeList with repeated data successed', () => {
|
||||
const listA = [{
|
||||
"id": "1682521800010412850",
|
||||
},
|
||||
{
|
||||
"id": "1682521800010412950",
|
||||
}]
|
||||
|
||||
const listB = [{
|
||||
"id": "1682521800010412950",
|
||||
}]
|
||||
|
||||
expect(mergeList(listA, listB)).toEqual([
|
||||
{
|
||||
"id": "1682521800010412850",
|
||||
},
|
||||
{
|
||||
"id": "1682521800010412950",
|
||||
}
|
||||
])
|
||||
})
|
||||
|
||||
test('mergeList empty successed', () => {
|
||||
const listA = []
|
||||
const listB = [{
|
||||
"id": "1682521800010412900",
|
||||
}]
|
||||
expect(mergeList(listA, listB)).toEqual([
|
||||
{
|
||||
"id": "1682521800010412900",
|
||||
}
|
||||
])
|
||||
})
|
||||
|
||||
test('mergeList empty 2 successed', () => {
|
||||
const listA = [{
|
||||
"id": "1682521800010412900",
|
||||
}]
|
||||
const listB = []
|
||||
expect(mergeList(listA, listB)).toEqual([
|
||||
{
|
||||
"id": "1682521800010412900",
|
||||
}
|
||||
])
|
||||
})
|
Reference in New Issue
Block a user