Skip to content

Commit e7d6553

Browse files
committed
feat: add mergeArrays tests
1 parent 49eee1c commit e7d6553

File tree

2 files changed

+70
-54
lines changed

2 files changed

+70
-54
lines changed

test/mergeAndCompare.test.ts

Lines changed: 0 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -60,57 +60,3 @@ test('Extend with custom concat arrays', () => {
6060
// const res2 = mergeAndCompare(concatArr, ['a'], ['b'])
6161
// expect(res2).toEqual( ['a', 'b'])
6262
})
63-
64-
test('undefined object', () => {
65-
const origin = {
66-
pages: {
67-
'aa': 'ttt',
68-
},
69-
}
70-
71-
const newData = {
72-
pages: {
73-
'aa': '1111',
74-
'bb': '2222',
75-
}
76-
}
77-
78-
function convertTimestamps(originVal: any, targetVal: any, key: any) {
79-
if (originVal !== undefined)
80-
return targetVal
81-
}
82-
83-
const res = mergeAndCompare(convertTimestamps, origin, newData)
84-
expect(res as any).toEqual({ pages: { aa: "1111", bb: undefined } })
85-
})
86-
87-
test('undefined array', () => {
88-
function convertTimestamps(originVal: any, targetVal: any, key: any) {
89-
if (originVal !== undefined)
90-
return targetVal
91-
}
92-
const origin = {
93-
date: [
94-
{
95-
'new': 'aaa',
96-
}
97-
]
98-
}
99-
const target = {
100-
date: [
101-
{
102-
'new': 'aaa',
103-
'old': 'bbb',
104-
}
105-
]
106-
}
107-
const res = mergeAndCompare(convertTimestamps, origin, target)
108-
expect(res as any).toEqual({
109-
date: [
110-
{
111-
'new': 'aaa',
112-
'old': undefined,
113-
}
114-
]
115-
})
116-
})

test/mergeArrays.test.ts

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
import { test, expect } from 'vitest'
2+
import { isArray } from 'is-what'
3+
import { mergeAndCompare, merge } from '../src/index'
4+
5+
function mergeArrays(originVal: any, newVal: any): any | any[] {
6+
if (isArray(originVal) && isArray(newVal)) {
7+
// concat & merge logic
8+
const overlappingPart = originVal.slice(0, newVal.length)
9+
10+
return overlappingPart
11+
.map((p, i) => (newVal[i] ? merge(p, newVal[i]) : p))
12+
.concat(
13+
newVal.length > originVal.length
14+
? originVal.slice(newVal.length)
15+
: newVal.slice(originVal.length)
16+
)
17+
}
18+
return newVal // always return newVal as fallback!!
19+
}
20+
21+
test('undefined object', () => {
22+
function merge(originVal: any, targetVal: any, key: any) {
23+
if (originVal !== undefined) return targetVal
24+
}
25+
26+
const origin = {
27+
pages: {
28+
aa: 'ttt',
29+
},
30+
}
31+
32+
const newData = {
33+
pages: {
34+
aa: '1111',
35+
bb: '2222',
36+
},
37+
}
38+
39+
const res = mergeAndCompare(mergeArrays, origin, newData)
40+
expect(res as any).toEqual({ pages: { aa: '1111', bb: '2222' } })
41+
})
42+
43+
test('undefined array', () => {
44+
const origin = {
45+
date: [
46+
{
47+
new: 'aa',
48+
something: 'yy',
49+
},
50+
],
51+
}
52+
const target = {
53+
date: [
54+
{
55+
new: 'bb',
56+
old: 'zz',
57+
},
58+
],
59+
}
60+
const res = mergeAndCompare(mergeArrays, origin, target)
61+
expect(res as any).toEqual({
62+
date: [
63+
{
64+
new: 'bb',
65+
something: 'yy',
66+
old: 'zz',
67+
},
68+
],
69+
})
70+
})

0 commit comments

Comments
 (0)