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