Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 | 3x 9x 9x 9x 9x 9x 8x 8x 9x 8x 5x 3x 3x 9x 4x 4x 5x 5x 4x 4x 3x | const { normalizeName } = require('./nameUniqueness')
/**
* Determines whether `targetFields` can receive item data originally shaped
* for `sourceFields` — used when importing an exported entity file on top of
* an existing entity that shares its name (see entities/import.js).
*
* A source field is considered covered when the target has a field with the
* same normalized name (case-insensitive, multilingual-aware) and the same
* `type`. The import is only offered as a merge when every source field is
* covered — extra target fields are fine, they just stay unset on imported
* items.
*
* @param {Array<{fieldId?: string, name: unknown, type?: string}>} sourceFields
* @param {Array<{fieldId?: string, name: unknown, type?: string}>} targetFields
* @returns {{
* compatible: boolean,
* fieldIdMapping: Record<string, string>,
* missingFields: Array<{ name: unknown, type: string | undefined }>
* }}
*/
function computeFieldCompatibility(sourceFields, targetFields) {
const sources = Array.isArray(sourceFields) ? sourceFields : []
const targets = Array.isArray(targetFields) ? targetFields : []
const fieldIdMapping = {}
const missingFields = []
for (const sourceField of sources) {
const sourceName = normalizeName(sourceField?.name)
const match = targets.find(
(targetField) =>
normalizeName(targetField?.name) === sourceName && targetField?.type === sourceField?.type
)
if (match && sourceField?.fieldId) {
fieldIdMapping[sourceField.fieldId] = match.fieldId
} else Eif (!match) {
missingFields.push({ name: sourceField?.name, type: sourceField?.type })
}
}
return {
compatible: missingFields.length === 0,
fieldIdMapping,
missingFields
}
}
/**
* Remaps an item's field values, keyed by source `fieldId`s, onto the target
* entity's `fieldId`s using `fieldIdMapping` (see computeFieldCompatibility).
* Keys without a mapping are dropped.
*
* @param {Record<string, unknown>} itemData
* @param {Record<string, string>} fieldIdMapping
* @returns {Record<string, unknown>}
*/
function remapItemFieldIds(itemData, fieldIdMapping) {
const remapped = {}
for (const [key, value] of Object.entries(itemData || {})) {
const targetKey = fieldIdMapping[key]
if (targetKey) {
remapped[targetKey] = value
}
}
return remapped
}
module.exports = { computeFieldCompatibility, remapItemFieldIds }
|