refactor(server/utils): use a "real" Map for toMap

This commit is contained in:
Panagiotis Papadopoulos 2025-02-01 14:11:17 +01:00
parent f0ba056bb7
commit ab0c84a57e
2 changed files with 10 additions and 8 deletions

View File

@ -731,13 +731,13 @@ function updateNoteData(noteId: string, content: string, attachments: Attachment
note.setContent(newContent, { forceFrontendReload });
if (attachments?.length > 0) {
const existingAttachmentsByTitle = toMap(note.getAttachments({ includeContentLength: false }), "title");
const existingAttachmentsByTitle = toMap(note.getAttachments({ includeContentLength: false }), "title");
for (const { attachmentId, role, mime, title, position, content } of attachments) {
if (attachmentId || !(title in existingAttachmentsByTitle)) {
const existingAttachment = existingAttachmentsByTitle.get(title);
if (attachmentId || !existingAttachment) {
note.saveAttachment({ attachmentId, role, mime, title, content, position });
} else {
const existingAttachment = existingAttachmentsByTitle[title];
existingAttachment.role = role;
existingAttachment.mime = mime;
existingAttachment.position = position;

View File

@ -247,13 +247,15 @@ export function normalize(str: string) {
return removeDiacritic(str).toLowerCase();
}
export function toMap<T extends Record<string, any>>(list: T[], key: keyof T): Record<string, T> {
const map: Record<string, T> = {};
export function toMap<T extends Record<string, any>>(list: T[], key: keyof T) {
const map = new Map<string, T>();
for (const el of list) {
map[el[key]] = el;
const keyForMap = el[key];
if (!keyForMap) continue;
// TriliumNextTODO: do we need to handle the case when the same key is used?
// currently this will overwrite the existing entry in the map
map.set(keyForMap, el);
}
return map;
}