-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: refactor full sync code to fix duplication error
- Loading branch information
Showing
3 changed files
with
175 additions
and
133 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import { mergeArrayOfObjectsBy, mergeArrayOfObjectsWith } from '../../utils/mergeArrayOfObject'; | ||
|
||
describe('mergeArrayOfObject utils test', () => { | ||
test('mergeBy', () => { | ||
const a = [{ id: '1' }, { id: '2' }]; | ||
const b = [{ id: '1' }]; | ||
|
||
const result = mergeArrayOfObjectsBy(a, b, 'id'); | ||
|
||
expect(result.length).toBe(2); | ||
}); | ||
|
||
test('mergeBy with a sort property argument', () => { | ||
const a = [ | ||
{ id: '1', time: 1 }, | ||
{ id: '2', time: 1 }, | ||
]; | ||
const b = [{ id: '1', time: 2 }]; | ||
|
||
const result = mergeArrayOfObjectsBy(a, b, 'id', 'time'); | ||
|
||
expect(result).toEqual([ | ||
{ id: '1', time: 2 }, | ||
{ id: '2', time: 1 }, | ||
]); | ||
}); | ||
|
||
test('mergeWith', () => { | ||
const a = [ | ||
{ id: '1', time: 1 }, | ||
{ id: '2', time: 1 }, | ||
]; | ||
const b = [{ id: '1', time: 2 }]; | ||
|
||
const result = mergeArrayOfObjectsWith(a, b, (a, b) => a.id === b.id); | ||
|
||
expect(result.length).toBe(2); | ||
}); | ||
|
||
test('mergeWith with a sort property argument', () => { | ||
const a = [ | ||
{ id: '1', time: 1 }, | ||
{ id: '2', time: 1 }, | ||
]; | ||
const b = [{ id: '1', time: 2 }]; | ||
|
||
const result = mergeArrayOfObjectsWith(a, b, (a, b) => a.id === b.id, 'time'); | ||
|
||
expect(result).toEqual([ | ||
{ id: '1', time: 2 }, | ||
{ id: '2', time: 1 }, | ||
]); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { unionBy, uniqBy, uniqWith } from 'lodash'; | ||
|
||
export function mergeArrayOfObjectsBy<T, K extends keyof T>( | ||
aArray: T[], | ||
bArray: T[], | ||
property: K, | ||
sortProperty?: K, | ||
): T[] { | ||
if (typeof sortProperty === 'undefined') { | ||
return unionBy(aArray, bArray, property); | ||
} | ||
|
||
let allArray = [...aArray, ...bArray]; | ||
|
||
if (sortProperty) { | ||
allArray = sortObjectArrayBy([...aArray, ...bArray], sortProperty); | ||
} | ||
|
||
return uniqBy(allArray, property); | ||
} | ||
|
||
export function mergeArrayOfObjectsWith<T, K extends keyof T>( | ||
aArray: T[], | ||
bArray: T[], | ||
comparatorFn: (a: T, b: T) => boolean, | ||
sortProperty?: K, | ||
): T[] { | ||
let allArray = [...aArray, ...bArray]; | ||
|
||
if (sortProperty) { | ||
allArray = sortObjectArrayBy(allArray, sortProperty); | ||
} | ||
|
||
return uniqWith(allArray, comparatorFn); | ||
} | ||
|
||
function sortObjectArrayBy<T, K extends keyof T>(arr: T[], sortProperty: K): T[] { | ||
return arr.sort((a, b) => (b[sortProperty] as any) - (a[sortProperty] as any)); | ||
} |