Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
refactor(server/utils): use a "real" Map for toMap
Panagiotis Papadopoulosab0c84a57eea
diff --git a/src/services/notes.ts b/src/services/notes.tsindex 0faf08d746..26e51675e2 100644--- a/src/services/notes.ts+++ b/src/services/notes.ts@@ -731,13 +731,13 @@ function updateNoteData(noteId: string, content: string, attachments: Attachmentnote.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.tsindex ba97519652..09e3964e5a 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<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;}