From ab0c84a57eea12e970ab4c6ae312b08ee5c9fd94 Mon Sep 17 00:00:00 2001 From: Panagiotis Papadopoulos Date: Sat, 1 Feb 2025 14:11:17 +0100 Subject: [PATCH] refactor(server/utils): use a "real" Map for toMap --- src/services/notes.ts | 6 +++--- src/services/utils.ts | 12 +++++++----- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/src/services/notes.ts b/src/services/notes.ts index 0faf08d74..26e51675e 100644 --- a/src/services/notes.ts +++ b/src/services/notes.ts @@ -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; diff --git a/src/services/utils.ts b/src/services/utils.ts index ba9751965..09e3964e5 100644 --- a/src/services/utils.ts +++ b/src/services/utils.ts @@ -247,13 +247,15 @@ export function normalize(str: string) { return removeDiacritic(str).toLowerCase(); } -export function toMap>(list: T[], key: keyof T): Record { - const map: Record = {}; - +export function toMap>(list: T[], key: keyof T) { + const map = new Map(); 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; }