Skip to content

A fully open source & end-to-end encrypted note taking alternative to Evernote.

fix: notes / notebooks overlap; faster filtering

thecodrr9e3dfd48ebad
diff --git a/packages/core/__tests__/database.test.js b/packages/core/__tests__/database.test.js
index 89134043f..86cff2cf6 100644
--- a/packages/core/__tests__/database.test.js
+++ b/packages/core/__tests__/database.test.js
@@ -133,7 +133,7 @@ test("duplicate note to topic should not be added", async () => {
test("note has notebook", () => {
let note = db.getNote(TEST_NOTE.dateCreated);
- expect(note.notebooks.hasOwnProperty(TEST_NOTEBOOK.dateCreated)).toBe(true);
+ expect(note.notebook.notebook).toBe(TEST_NOTEBOOK.dateCreated);
});
test("get empty topic", () => {
@@ -163,6 +163,16 @@ test("move note", async () => {
expect(res).toBe(true);
});
+test("notes don't contain notebooks", () => {
+ let notes = db.getNotes().filter(v => v.type === "notebook");
+ expect(notes.length).toBe(0);
+});
+
+test("notebooks don't contain notes", () => {
+ let notebooks = db.getNotebooks().filter(v => v.type === "note");
+ expect(notebooks.length).toBe(0);
+});
+
test("deletion of note from invalid topic should return false", async () => {
let res = await db.deleteNoteFromTopic(
TEST_NOTEBOOK.dateCreated,
@@ -174,8 +184,8 @@ test("deletion of note from invalid topic should return false", async () => {
test("delete note from topic", async () => {
await db.deleteNoteFromTopic(
- TEST_NOTEBOOK.dateCreated,
- "Home",
+ TEST_NOTEBOOK2.dateCreated,
+ "Home2",
TEST_NOTE.dateCreated
);
let notebook = db.getNotebook(TEST_NOTEBOOK.dateCreated);
@@ -185,7 +195,7 @@ test("delete note from topic", async () => {
test("note doesn't have notebook", () => {
let note = db.getNote(TEST_NOTE.dateCreated);
- expect(note.notebooks.hasOwnProperty(TEST_NOTEBOOK.dateCreated)).toBe(false);
+ expect(note.notebook).toStrictEqual({});
});
test("delete topic from notebook", async () => {
diff --git a/packages/core/api/database.js b/packages/core/api/database.js
index ea50f86bc..a8c79fe10 100644
--- a/packages/core/api/database.js
+++ b/packages/core/api/database.js
@@ -1,6 +1,7 @@
import Storage from "../helpers/storage";
import fuzzysearch from "fuzzysearch";
-import ff from "fast-filter";
+var tfun = require("transfun/transfun.js").tfun;
+tfun = global.tfun;
import { extractValues } from "../utils";
const KEYS = {
@@ -14,6 +15,13 @@ function checkInitialized() {
);
}
}
+function groupBy(xs, key) {
+ return tfun.reduce(function(rv, x) {
+ var v = key instanceof Function ? key(x) : x[key];
+ (rv[v] = rv[v] || []).push(x);
+ return rv;
+ })(xs);
+}
class Database {
constructor(storage) {
this.storage = new Storage(storage);
@@ -42,7 +50,15 @@ class Database {
*/
getNotes() {
checkInitialized.call(this);
- return extractValues(this.notes);
+ return extractValues(this.notes).reverse();
+ }
+
+ //TODO
+ groupNotes() {
+ var c = groupBy(this.getNotes(), x =>
+ new Date(x.dateCreated).getFullYear()
+ );
+ return c;
}
/**
@@ -108,10 +124,8 @@ class Database {
*/
searchNotes(query) {
if (!query) return [];
- return ff(
- extractValues(this.notes),
- v => fuzzysearch(query, v.title + " " + v.content.text),
- this
+ return tfun.filter(v => fuzzysearch(query, v.title + " " + v.content.text))(
+ extractValues(this.notes)
);
}
@@ -307,7 +321,7 @@ export default Database;
function deleteItems(items, key) {
if (!items || items.length <= 0 || !this[key] || this[key].length <= 0)
- return false; //TODO add test
+ return false;
for (let item of items) {
if (!item) continue;
if (this[key].hasOwnProperty(item.dateCreated)) {
@@ -322,10 +336,9 @@ function notebookTopicFn(notebookId, topic, fn) {
const notebook = this.notebooks[notebookId];
const result = fn(notebook);
if (result === true) {
- this.notes[notebookId] = notebook;
+ this.notebooks[notebookId] = notebook;
return this.storage.write(KEYS.notebooks, this.notebooks).then(s => true);
}
- //TODO add test
return result;
}
diff --git a/packages/core/package.json b/packages/core/package.json
index b4d347b30..12d1796cf 100644
--- a/packages/core/package.json
+++ b/packages/core/package.json
@@ -18,6 +18,8 @@
},
"dependencies": {
"fast-filter": "^1.0.4",
- "fuzzysearch": "^1.0.3"
+ "fast-sort": "^1.6.0",
+ "fuzzysearch": "^1.0.3",
+ "transfun": "^1.0.2"
}
}
diff --git a/packages/core/yarn.lock b/packages/core/yarn.lock
index 66972efd6..d4b23d7c4 100644
--- a/packages/core/yarn.lock
+++ b/packages/core/yarn.lock
@@ -2176,6 +2176,11 @@ fast-levenshtein@~2.0.6:
resolved "https://registry.yarnpkg.com/fast-levenshtein/-/fast-levenshtein-2.0.6.tgz#3d8a5c66883a16a30ca8643e851f19baa7797917"
integrity sha1-PYpcZog6FqMMqGQ+hR8Zuqd5eRc=
+fast-sort@^1.6.0:
+ version "1.6.0"
+ resolved "https://registry.yarnpkg.com/fast-sort/-/fast-sort-1.6.0.tgz#68c6d94ce309ead19d562014159902c80c68d0a0"
+ integrity sha512-sjV6dxWdHs14cef7GzJgCWSOagw57cUBYMey+seWeOsU0HgMf2g4Wkdflv65X/8E4UWptUcq9EZ3YqSXUnUy9Q==
+
fb-watchman@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/fb-watchman/-/fb-watchman-2.0.0.tgz#54e9abf7dfa2f26cd9b1636c588c1afc05de5d58"
@@ -4505,6 +4510,11 @@ tr46@^1.0.1:
dependencies:
punycode "^2.1.0"
+transfun@^1.0.2:
+ version "1.0.2"
+ resolved "https://registry.yarnpkg.com/transfun/-/transfun-1.0.2.tgz#14a3116296a8d921cbf9678ebd5eee1c5bb77b98"
+ integrity sha1-FKMRYpao2SHL+WeOvV7uHFu3e5g=
+
tunnel-agent@^0.6.0:
version "0.6.0"
resolved "https://registry.yarnpkg.com/tunnel-agent/-/tunnel-agent-0.6.0.tgz#27a5dea06b36b04a0a9966774b290868f0fc40fd"