Skip to content

Commit

Permalink
Merge pull request #35 from productdevbook/bug-test
Browse files Browse the repository at this point in the history
feat: new test
  • Loading branch information
mesqueeb committed Nov 17, 2022
2 parents 0482f68 + e7d6553 commit 1c4e58d
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 1 deletion.
2 changes: 1 addition & 1 deletion test/mergeAndCompare.test.ts
@@ -1,5 +1,5 @@
import { test, expect } from 'vitest'
import { isDate, isString, isArray, isObject } from 'is-what'
import { isDate, isString, isArray } from 'is-what'
import { mergeAndCompare } from '../src/index'

test('conversion based on original val', () => {
Expand Down
70 changes: 70 additions & 0 deletions test/mergeArrays.test.ts
@@ -0,0 +1,70 @@
import { test, expect } from 'vitest'
import { isArray } from 'is-what'
import { mergeAndCompare, merge } from '../src/index'

function mergeArrays(originVal: any, newVal: any): any | any[] {
if (isArray(originVal) && isArray(newVal)) {
// concat & merge logic
const overlappingPart = originVal.slice(0, newVal.length)

return overlappingPart
.map((p, i) => (newVal[i] ? merge(p, newVal[i]) : p))
.concat(
newVal.length > originVal.length
? originVal.slice(newVal.length)
: newVal.slice(originVal.length)
)
}
return newVal // always return newVal as fallback!!
}

test('undefined object', () => {
function merge(originVal: any, targetVal: any, key: any) {
if (originVal !== undefined) return targetVal
}

const origin = {
pages: {
aa: 'ttt',
},
}

const newData = {
pages: {
aa: '1111',
bb: '2222',
},
}

const res = mergeAndCompare(mergeArrays, origin, newData)
expect(res as any).toEqual({ pages: { aa: '1111', bb: '2222' } })
})

test('undefined array', () => {
const origin = {
date: [
{
new: 'aa',
something: 'yy',
},
],
}
const target = {
date: [
{
new: 'bb',
old: 'zz',
},
],
}
const res = mergeAndCompare(mergeArrays, origin, target)
expect(res as any).toEqual({
date: [
{
new: 'bb',
something: 'yy',
old: 'zz',
},
],
})
})

0 comments on commit 1c4e58d

Please sign in to comment.