UNPKG

@fnlb-project/stanza

Version:

Modern XMPP in the browser, with a JSON API

31 lines (30 loc) 918 B
export function mergeFields(original, updated) { const merged = []; const mappedUpdates = new Map(); for (const field of updated) { if (field.name) { mappedUpdates.set(field.name, field); } } const usedUpdates = new Set(); // Update any existing fields with new values. for (const field of original) { if (field.name && mappedUpdates.has(field.name)) { merged.push({ ...field, ...mappedUpdates.get(field.name) }); usedUpdates.add(field.name); } else { merged.push({ ...field }); } } // Append any brand new fields to the list for (const field of updated) { if (!field.name || (field.name && !usedUpdates.has(field.name))) { merged.push({ ...field }); } } return merged; }