-
Notifications
You must be signed in to change notification settings - Fork 2
/
join.js
32 lines (30 loc) · 931 Bytes
/
join.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
export const createJoinKeys = (record, fieldIds, caseSensitive, joinOnAll) => {
const getJoinKeys = (rec) =>
fieldIds
.filter((fieldId) => fieldId !== null)
.map((fieldId) => {
const val = rec.getCellValueAsString(fieldId)
return caseSensitive ? val : val.toLowerCase()
})
const joinKeys = getJoinKeys(record)
const validKeys = joinKeys.filter((value) => value.length > 0)
return joinOnAll ? [joinKeys.join(",")] : validKeys
}
// Create a mapping of join key values to record IDs
export const createJoinKeyMap = (records, fieldIds, caseSensitive, joinOnAll) =>
records
.map((record) =>
createJoinKeys(record, fieldIds, caseSensitive, joinOnAll).map((key) => ({
key,
record,
}))
)
.flat()
.filter(({ key }) => key.length > 0)
.reduce(
(obj, { record, key }) => ({
...obj,
[key]: record,
}),
{}
)