A fully open source & end-to-end encrypted note taking alternative to Evernote.
TypeScript asp-net-coredotnet-coreelectronfosshacktoberfestnote-managmentnote-takingnote-taking-appnotebooknotesnotes-appopen-sourceopensourceprivacyproductivityreactreact-nativereactjsself-hosted
refactor: make code more DRY
thecodrradfde20b1d5c
diff --git a/packages/core/__tests__/database.test.js b/packages/core/__tests__/database.test.jsindex 1c2ebd420..611a25dab 100644--- a/packages/core/__tests__/database.test.js+++ b/packages/core/__tests__/database.test.js@@ -86,6 +86,11 @@ test("add note to topic", async () => {expect(notebook.topics["Home"][0]).toBeGreaterThan(0);});+test("note has notebook", () => {+ let note = db.getNote(TEST_NOTE.dateCreated);+ expect(note.notebooks.hasOwnProperty(TEST_NOTEBOOK.dateCreated)).toBe(true);+});+test("get topic", () => {let notes = db.getTopic(TEST_NOTEBOOK.dateCreated, "Home");expect(notes.length).toBeGreaterThan(0);@@ -101,6 +106,11 @@ test("delete note from topic", async () => {expect(notebook.topics["Home"][0]).toBeUndefined();});+test("note doesn't have notebook", () => {+ let note = db.getNote(TEST_NOTE.dateCreated);+ expect(note.notebooks.hasOwnProperty(TEST_NOTEBOOK.dateCreated)).toBe(false);+});+test("delete topic from notebook", async () => {await db.deleteTopicFromNotebook(TEST_NOTEBOOK.dateCreated, "Home");let notebook = db.getNotebook(TEST_NOTEBOOK.dateCreated);diff --git a/packages/core/api/database.js b/packages/core/api/database.jsindex e20e28510..8b7ebd63f 100644--- a/packages/core/api/database.js+++ b/packages/core/api/database.js@@ -47,7 +47,7 @@ class Database {content: note.content,pinned: note.pinned || false,tags: note.tags || [],- notebooks: note.notebooks || [],+ notebooks: note.notebooks || {},colors: note.colors || [],favorite: note.favorite || false,headline: note.content.text.substring(0, 150) + "...",@@ -160,10 +160,10 @@ class Database {* @param {number} noteId The ID of the note*/addNoteToTopic(notebookId, topic, noteId) {- return notebookTopicFn.call(this, notebookId, topic, notebook => {- if (!notebook.topics.hasOwnProperty(topic)) return false;- let nbTopic = notebook.topics[topic];- notebook.topics[topic][nbTopic.length] = noteId;+ return topicNoteFn.call(this, notebookId, topic, noteId, async notebook => {+ notebook.topics[topic][notebook.topics[topic].length] = noteId;+ //add notebookId to the note+ this.notes[noteId].notebooks[notebookId] = notebook.title;return true;});}@@ -175,12 +175,12 @@ class Database {* @param {number} noteId The ID of the note*/deleteNoteFromTopic(notebookId, topic, noteId) {- return notebookTopicFn.call(this, notebookId, topic, notebook => {- if (!notebook.topics[topic]) return false;- let nbTopic = notebook.topics[topic];- let index = nbTopic.indexOf(noteId);- if (index <= -1) return;+ return topicNoteFn.call(this, notebookId, topic, noteId, async notebook => {+ let index = notebook.topics[topic].indexOf(noteId);+ if (index <= -1) return false;notebook.topics[topic].splice(index, 1);+ //delete notebook from note+ delete this.notes[noteId].notebooks[notebookId];return true;});}@@ -242,6 +242,22 @@ function notebookTopicFn(notebookId, topic, fn) {return Promise.resolve();}+function topicNoteFn(notebookId, topic, noteId, fn) {+ return notebookTopicFn.call(this, notebookId, topic, async notebook => {+ if (+ !notebook.topics.hasOwnProperty(topic) ||+ !this.notes.hasOwnProperty(noteId)+ )+ return false;++ if (fn(notebook)) {+ await this.storage.write(KEYS.notes, this.notes);+ return true;+ }+ return false;+ });+}+function getItem(id, key) {if (this[key].hasOwnProperty(id)) {return this[key][id];