Skip to content

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

feat: add lookup class for searching

thecodrr968faab53726
diff --git a/packages/core/__tests__/lookup.test.js b/packages/core/__tests__/lookup.test.js
new file mode 100644
index 000000000..5d28447be
--- /dev/null
+++ b/packages/core/__tests__/lookup.test.js
@@ -0,0 +1,38 @@
+import {
+ StorageInterface,
+ databaseTest,
+ noteTest,
+ groupedTest,
+ LONG_TEXT,
+ TEST_NOTE,
+ TEST_NOTEBOOK,
+ notebookTest,
+ TEST_NOTEBOOK2
+} from "./utils";
+
+beforeEach(async () => {
+ StorageInterface.clear();
+});
+
+test("search notes", () =>
+ noteTest({
+ content: { delta: "5", text: "5" }
+ }).then(async ({ db }) => {
+ await db.notes.add(TEST_NOTE);
+ let filtered = db.lookup.notes(db.notes.all, "5");
+ expect(filtered.length).toBe(1);
+ }));
+
+test("search notebooks", () =>
+ notebookTest().then(async ({ db }) => {
+ await db.notebooks.add(TEST_NOTEBOOK2);
+ let filtered = db.lookup.notebooks(db.notebooks.all, "hello");
+ expect(filtered.length).toBe(1);
+ }));
+
+test("search topics", () =>
+ notebookTest().then(async ({ db, id }) => {
+ const topics = db.notebooks.notebook(id).topics.all;
+ let filtered = db.lookup.topics(topics, "hello");
+ expect(filtered.length).toBe(1);
+ }));
diff --git a/packages/core/__tests__/notes.test.js b/packages/core/__tests__/notes.test.js
index 8269d3c08..5b5a67d20 100644
--- a/packages/core/__tests__/notes.test.js
+++ b/packages/core/__tests__/notes.test.js
@@ -53,14 +53,6 @@ test("get all notes", () =>
expect(db.notes.all.length).toBeGreaterThan(0);
}));
-test("search all notes", () =>
- noteTest({
- content: { delta: "5", text: "5" }
- }).then(async ({ db }) => {
- let filtered = db.notes.filter("5");
- expect(filtered.length).toBeGreaterThan(0);
- }));
-
test("note without a title should get title from content", () =>
noteTest().then(async ({ db, id }) => {
let note = db.notes.note(id);
diff --git a/packages/core/api/index.js b/packages/core/api/index.js
index 53676d337..9548d2ec0 100644
--- a/packages/core/api/index.js
+++ b/packages/core/api/index.js
@@ -5,6 +5,7 @@ import Tags from "../collections/tags";
import User from "../models/user";
import Sync from "./sync";
import Vault from "./vault";
+import Lookup from "./lookup";
class Database {
constructor(context) {
@@ -24,6 +25,7 @@ class Database {
await this.trash.init(this.notes, this.notebooks);
this.syncer = new Sync(this);
this.vault = new Vault(this, this.context);
+ this.lookup = new Lookup();
}
sync() {
diff --git a/packages/core/api/lookup.js b/packages/core/api/lookup.js
new file mode 100644
index 000000000..0bedf0724
--- /dev/null
+++ b/packages/core/api/lookup.js
@@ -0,0 +1,37 @@
+import fuzzysearch from "fuzzysearch";
+var tfun = require("transfun/transfun.js").tfun;
+if (!tfun) {
+ tfun = global.tfun;
+}
+
+export default class Lookup {
+ notes(array, query) {
+ return tfun.filter(note =>
+ fuzzysearch(query, note.title + " " + note.content.text)
+ )(array);
+ }
+
+ notebooks(array, query) {
+ return tfun.filter(
+ nb =>
+ fuzzysearch(query, nb.title + " " + nb.description) ||
+ nb.topics.some(topic => fuzzysearch(query, topic.title))
+ )(array);
+ }
+
+ topics(array, query) {
+ return this._byTitle(array, query);
+ }
+
+ tags(array, query) {
+ return this._byTitle(array, query);
+ }
+
+ trash(array, query) {
+ return this._byTitle(array, query);
+ }
+
+ _byTitle(array, query) {
+ return tfun.filter(item => fuzzysearch(query, item.title))(array);
+ }
+}
diff --git a/packages/core/collections/notes.js b/packages/core/collections/notes.js
index 25d673486..889294005 100644
--- a/packages/core/collections/notes.js
+++ b/packages/core/collections/notes.js
@@ -1,6 +1,5 @@
import CachedCollection from "../database/cached-collection";
import fuzzysearch from "fuzzysearch";
-import Tags from "./tags";
import { groupBy } from "../utils";
import sort from "fast-sort";
import {
@@ -116,13 +115,6 @@ export default class Notes {
return tfun.filter(`.colors.includes('${color}')`)(this.all);
}
- filter(query) {
- if (!query) return [];
- let queryFn = v => fuzzysearch(query, v.title + " " + v.content.text);
- if (query instanceof Function) queryFn = query;
- return tfun.filter(queryFn)(this.all);
- }
-
group(by, special = false) {
let notes = !special
? tfun.filter(".pinned === false")(this.all)