Skip to content

Commit 4b96503

Browse files
committed
O(n)
1 parent fc26884 commit 4b96503

1 file changed

Lines changed: 24 additions & 6 deletions

File tree

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,25 @@
1-
export function mergeMultiJoin(baseMultiJoin, multiJoinToMerge) {
2-
if (!multiJoinToMerge) {
3-
return baseMultiJoin;
4-
}
5-
const checkJoinIsUnique = join => !baseMultiJoin.find(j => j.joinWith === join.joinWith);
6-
return [...baseMultiJoin].concat(...multiJoinToMerge.filter(checkJoinIsUnique));
1+
/**
2+
* @typedef {import('../BaseDatabase').JoinType} JoinType
3+
* @typedef {{
4+
* joinWith: string;
5+
* joinAs?: string;
6+
* joinType?: JoinType;
7+
* joinCondition: [string, string];
8+
* fields?: Record<string, string | undefined>;
9+
* }} MultiJoinItem
10+
*/
11+
12+
/**
13+
* @template {readonly MultiJoinItem[]} Base
14+
* @param {readonly Base} base
15+
* @param {readonly MultiJoinItem[]} source
16+
* @returns {[...Base, ...MultiJoinItem[]]}
17+
*/
18+
export function mergeMultiJoin(base, source) {
19+
if (!source || source.length === 0) return base;
20+
21+
const existingJoinTables = new Set(base.map(j => j.joinWith));
22+
const uniques = source.filter(j => !existingJoinTables.has(j.joinWith));
23+
24+
return [...base, ...uniques];
725
}

0 commit comments

Comments
 (0)