| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465 |
- import _ from "lodash-es"
- export async function generateTaxonomy({ config: actionConfig }) {
- if (!Array.isArray(actionConfig.inputs)) {
- return {
- detail: actionConfig.indexOn ? {} : [],
- ref: actionConfig.name,
- }
- }
- const orderBy = actionConfig.orderBy || "date"
- const inputs = actionConfig.inputs
- const sortedInputs = actionConfig.sortAscending
- ? _.sortBy(inputs, orderBy)
- : _.sortBy(inputs, orderBy).reverse()
- const buildEntry = item => actionConfig.properties
- ? actionConfig.properties.reduce(
- (ent, prop) => ({ ...ent, [prop]: item[prop] }),
- {},
- )
- : item
- if (!actionConfig.indexOn) {
- return {
- detail: sortedInputs.map(buildEntry),
- ref: actionConfig.name,
- }
- }
- const groupOrder = []
- const groupSeen = new Set()
- for (const item of inputs) {
- const items = item?.[actionConfig.indexOn]
- const normalizedItems = Array.isArray(items)
- ? items
- : (items == null ? [] : [items])
- for (const value of normalizedItems) {
- if (!groupSeen.has(value)) {
- groupSeen.add(value)
- groupOrder.push(value)
- }
- }
- }
- const groups = new Map(groupOrder.map(value => [value, []]))
- for (const item of sortedInputs) {
- const items = item?.[actionConfig.indexOn]
- const normalizedItems = Array.isArray(items)
- ? items
- : (items == null ? [] : [items])
- if (normalizedItems.length === 0) continue
- const entry = buildEntry(item)
- for (const value of normalizedItems) {
- if (!groups.has(value)) {
- groups.set(value, [])
- }
- groups.get(value).push(entry)
- }
- }
- const taxonomy = Object.fromEntries(groups)
- return {
- detail: taxonomy,
- ref: actionConfig.name,
- }
- }
|