refactor
This commit is contained in:
@@ -0,0 +1,38 @@
|
||||
export const resolveCorrespondents = (doc) => {
|
||||
if (!doc || !Array.isArray(doc.correspondents)) {
|
||||
return [];
|
||||
}
|
||||
|
||||
const seen = new Set();
|
||||
const results = [];
|
||||
|
||||
doc.correspondents.forEach((entry, index) => {
|
||||
if (!entry || typeof entry.name !== 'string') {
|
||||
return;
|
||||
}
|
||||
|
||||
const id = entry.id;
|
||||
const name = entry.name.trim();
|
||||
if (!name) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (id && seen.has(id)) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (id) {
|
||||
seen.add(id);
|
||||
}
|
||||
|
||||
results.push({
|
||||
id,
|
||||
name,
|
||||
key: id ?? `${name}-${index}`,
|
||||
});
|
||||
});
|
||||
|
||||
return results;
|
||||
};
|
||||
|
||||
export default resolveCorrespondents;
|
||||
Reference in New Issue
Block a user