MCPcopy
hub / github.com/prisma/prisma / mergeBy

Function mergeBy

packages/client/src/runtime/mergeBy.ts:9–22  ·  view source on GitHub ↗
(arr1: T[], arr2: T[], cb: (element: T) => string)

Source from the content-addressed store, hash-verified

7 * @param cb The function to calculate uniqueness
8 */
9export function mergeBy<T>(arr1: T[], arr2: T[], cb: (element: T) => string): T[] {
10 const groupedArr1 = groupBy(arr1, cb)
11 const groupedArr2 = groupBy(arr2, cb)
12 const result: T[] = Object.values(groupedArr2).map((value) => value[value.length - 1])
13
14 const arr2Keys = Object.keys(groupedArr2)
15 Object.entries(groupedArr1).forEach(([key, value]) => {
16 if (!arr2Keys.includes(key)) {
17 result.push(value[value.length - 1])
18 }
19 })
20
21 return result
22}
23
24const groupBy = <T>(arr: T[], cb: (element: T) => string): { [key: string]: T[] } => {
25 return arr.reduce<{ [key: string]: T[] }>((acc, curr) => {

Callers 1

mergeBy.test.tsFile · 0.90

Calls 5

valuesMethod · 0.80
entriesMethod · 0.80
includesMethod · 0.80
groupByFunction · 0.70
pushMethod · 0.45

Tested by

no test coverage detected