39 lines
659 B
JavaScript
39 lines
659 B
JavaScript
export const resolveCorrespondents = (doc) => {
|
|
if (!doc || !Array.isArray(doc.correspondents)) {
|
|
return [];
|
|
}
|
|
|
|
const seen = new Set();
|
|
const results = [];
|
|
|
|
doc.correspondents.forEach((entry = {}, index) => {
|
|
const { id, name } = entry;
|
|
if (typeof name !== 'string') {
|
|
return;
|
|
}
|
|
|
|
const trimmedName = name.trim();
|
|
if (!trimmedName) {
|
|
return;
|
|
}
|
|
|
|
if (id && seen.has(id)) {
|
|
return;
|
|
}
|
|
|
|
if (id) {
|
|
seen.add(id);
|
|
}
|
|
|
|
results.push({
|
|
id,
|
|
name: trimmedName,
|
|
key: id ?? `${trimmedName}-${index}`,
|
|
});
|
|
});
|
|
|
|
return results;
|
|
};
|
|
|
|
export default resolveCorrespondents;
|