/** * Add a new field to the data from an external source by matching on the same field name * @param data The data to add the new field to * @param externalSource The external source to get the new field from * @param externalSourceFieldName The field in both data and external source to match on * @param newFieldName The name of the new field to add * @param defaultValue The default value to use if there is no match */ export const addNewFieldFromExternalSource = ( data: Record[], externalSource: Record[], externalSourceFieldName: string, newFieldName: string, defaultValue: string = "-" ): Record[] => { return data.map((current) => { const cacheEntry = externalSource.find( (existing) => existing[externalSourceFieldName] === current[externalSourceFieldName] ); const newFieldValue = cacheEntry?.[newFieldName] || defaultValue; return { ...current, [newFieldName]: newFieldValue, }; }); }; /** * Add a new field to the data from an external source by matching on different field names * @param data The data to add the new field to * @param externalSource The external source to get the new field from * @param lookupField The field in the external source to match on * @param currentField The field in the current data to match on * @param newFieldName The name of the new field to add * @param defaultValue The default value to use if there is no match */ export const addNewFieldFromLookupField = ( data: Record[], externalSource: Record[], lookupField: string, currentField: string, newFieldName: string, defaultValue: string = "-" ): Record[] => { return data.map((current) => { const cacheEntry = externalSource.find( (existing) => existing[lookupField] === current[currentField] ); const newFieldValue = cacheEntry?.[currentField] || defaultValue; return { ...current, [newFieldName]: newFieldValue, }; }); };