Skip to content

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

feat: use fast-fuzzy for searching

thecodrrbdfd9683e607
diff --git a/packages/core/api/lookup.js b/packages/core/api/lookup.js
index bbafe41c9..58e646810 100644
--- a/packages/core/api/lookup.js
+++ b/packages/core/api/lookup.js
@@ -1,4 +1,5 @@
-import fuzzysearch from "fuzzysearch";
+import { search } from "fast-fuzzy";
+import { qclone } from "qclone";
import { getContentFromData } from "../content-types";
export default class Lookup {
@@ -11,29 +12,37 @@ export default class Lookup {
}
async notes(notes, query) {
+ notes = qclone(notes);
const contents = await this._db.content.multi(
- notes.map((note) => note.contentId)
+ notes.map((note) => note.contentId || "")
);
- const results = [];
- notes.forEach((note) => {
- const title = note.title;
+ const candidates = notes.map((note) => {
+ note.content = "";
if (!note.locked) {
let content = contents.find((content) => content.id === note.contentId);
+ if (!content) return note;
content = getContentFromData(content.type, content.data);
- if (fzs(query, title) || content.search(query)) results.push(note);
- } else {
- if (fzs(query, title)) results.push(note);
+ note.content = content.toHTML();
}
+ return note;
+ });
+ return search(query, candidates, {
+ keySelector: (item) => [item.title, item.content],
+ ignoreCase: false,
});
- return results;
}
notebooks(array, query) {
- return array.filter(
- (nb) =>
- fzs(query, nb.title + " " + nb.description) ||
- nb.topics.some((topic) => fuzzysearch(query, topic.title))
- );
+ return search(query, array, {
+ keySelector: (item) => {
+ return [
+ item.title,
+ item.description || "",
+ ...item.topics.map((t) => t.title),
+ ];
+ },
+ ignoreCase: true,
+ });
}
topics(array, query) {
@@ -49,10 +58,9 @@ export default class Lookup {
}
_byTitle(array, query) {
- return array.filter((item) => fzs(query, item.title));
+ return search(query, array, {
+ keySelector: (item) => item.title || "",
+ ignoreCase: true,
+ });
}
}
-
-function fzs(query, text) {
- return fuzzysearch(query.toLowerCase(), text.toLowerCase());
-}
diff --git a/packages/core/package.json b/packages/core/package.json
index 9ab5f29db..acf2dd8e9 100644
--- a/packages/core/package.json
+++ b/packages/core/package.json
@@ -21,8 +21,8 @@
"test": "jest"
},
"dependencies": {
+ "fast-fuzzy": "^1.10.8",
"fast-sort": "^2.0.1",
- "fuzzysearch": "^1.0.3",
"lean-he": "^2.1.2",
"no-internet": "^1.5.2",
"qclone": "^1.0.4",
diff --git a/packages/core/yarn.lock b/packages/core/yarn.lock
index 18aedaab9..033bf169f 100644
--- a/packages/core/yarn.lock
+++ b/packages/core/yarn.lock
@@ -2429,6 +2429,13 @@ fast-deep-equal@^3.1.1:
resolved "https://registry.yarnpkg.com/fast-deep-equal/-/fast-deep-equal-3.1.1.tgz#545145077c501491e33b15ec408c294376e94ae4"
integrity sha512-8UEa58QDLauDNfpbrX55Q9jrGHThw2ZMdOky5Gl1CDtVeJDPVrG4Jxx1N8jw2gkWaff5UUuX1KJd+9zGe2B+ZA==
+fast-fuzzy@^1.10.8:
+ version "1.10.8"
+ resolved "https://registry.yarnpkg.com/fast-fuzzy/-/fast-fuzzy-1.10.8.tgz#4c45d38b4f55644a3b0144aa27f1a53c0b8fd06f"
+ integrity sha512-jXxymuLb3p4S873sWswpWS7GTAqFSnYLNdHCeB9gvXAmjskrAkfX9eu0lWDUFfHIXPSFZe8jsfw9t4KifR6YMg==
+ dependencies:
+ graphemesplit "^2.4.1"
+
fast-json-stable-stringify@^2.0.0:
version "2.1.0"
resolved "https://registry.yarnpkg.com/fast-json-stable-stringify/-/fast-json-stable-stringify-2.1.0.tgz#874bf69c6f404c2b5d99c481341399fd55892633"
@@ -2596,6 +2603,13 @@ graceful-fs@^4.1.11, graceful-fs@^4.1.15, graceful-fs@^4.1.2:
resolved "https://registry.yarnpkg.com/graceful-fs/-/graceful-fs-4.2.4.tgz#2256bde14d3632958c465ebc96dc467ca07a29fb"
integrity sha512-WjKPNJF79dtJAVniUlGGWHYGz2jWxT6VhN/4m1NdkbZ2nOsEF+cI1Edgql5zCRhs/VsQYRvrXctxktVXZUkixw==
+graphemesplit@^2.4.1:
+ version "2.4.2"
+ resolved "https://registry.yarnpkg.com/graphemesplit/-/graphemesplit-2.4.2.tgz#e3ff455b157ad0d40554dbd368c74d4ac7f3d97d"
+ integrity sha512-N44DD8cDcWCtZesOhe0zS0x1SVO5zQ0HE6XKlS/JHDB+vhyEvorzI/gsTN/ae0gjH4KEDtUvkjEI5w/sgu8QHw==
+ dependencies:
+ unicode-trie "^2.0.0"
+
growly@^1.3.0:
version "1.3.0"
resolved "https://registry.yarnpkg.com/growly/-/growly-1.3.0.tgz#f10748cbe76af964b7c96c93c6bcc28af120c081"
@@ -3912,6 +3926,11 @@ p-try@^2.0.0:
resolved "https://registry.yarnpkg.com/p-try/-/p-try-2.2.0.tgz#cb2868540e313d61de58fafbe35ce9004d5540e6"
integrity sha512-R4nPAVTAU0B9D35/Gk3uJf/7XYbQcyohSKdvAxIRSNghFl4e71hVoGnBNQz9cWaXxO2I10KTC+3jMdvvoKw6dQ==
+pako@^0.2.5:
+ version "0.2.9"
+ resolved "https://registry.yarnpkg.com/pako/-/pako-0.2.9.tgz#f3f7522f4ef782348da8161bad9ecfd51bf83a75"
+ integrity sha1-8/dSL073gjSNqBYbrZ7P1Rv4OnU=
+
parse-json@^4.0.0:
version "4.0.0"
resolved "https://registry.yarnpkg.com/parse-json/-/parse-json-4.0.0.tgz#be35f5425be1f7f6c747184f98a788cb99477ee0"
@@ -4716,6 +4735,11 @@ throat@^4.0.0:
resolved "https://registry.yarnpkg.com/throat/-/throat-4.1.0.tgz#89037cbc92c56ab18926e6ba4cbb200e15672a6a"
integrity sha1-iQN8vJLFarGJJua6TLsgDhVnKmo=
+tiny-inflate@^1.0.0:
+ version "1.0.3"
+ resolved "https://registry.yarnpkg.com/tiny-inflate/-/tiny-inflate-1.0.3.tgz#122715494913a1805166aaf7c93467933eea26c4"
+ integrity sha512-pkY1fj1cKHb2seWDy0B16HeWyczlJA9/WW3u3c4z/NiWDsO3DOU5D7nhTLE9CF0yXv/QZFY7sEJmj24dK+Rrqw==
+
version "1.0.4"
resolved "https://registry.yarnpkg.com/tmpl/-/tmpl-1.0.4.tgz#23640dd7b42d00433911140820e5cf440e521dd1"
@@ -4825,6 +4849,14 @@ unicode-property-aliases-ecmascript@^1.0.4:
resolved "https://registry.yarnpkg.com/unicode-property-aliases-ecmascript/-/unicode-property-aliases-ecmascript-1.1.0.tgz#dd57a99f6207bedff4628abefb94c50db941c8f4"
integrity sha512-PqSoPh/pWetQ2phoj5RLiaqIk4kCNwoV3CI+LfGmWLKI3rE3kl1h59XpX2BjgDrmbxD9ARtQobPGU1SguCYuQg==
+unicode-trie@^2.0.0:
+ version "2.0.0"
+ resolved "https://registry.yarnpkg.com/unicode-trie/-/unicode-trie-2.0.0.tgz#8fd8845696e2e14a8b67d78fa9e0dd2cad62fec8"
+ integrity sha512-x7bc76x0bm4prf1VLg79uhAzKw8DVboClSN5VxJuQ+LKDOVEW9CdH+VY7SP+vX7xCYQqzzgQpFqz15zeLvAtZQ==
+ dependencies:
+ pako "^0.2.5"
+ tiny-inflate "^1.0.0"
+
union-value@^1.0.0:
version "1.0.1"
resolved "https://registry.yarnpkg.com/union-value/-/union-value-1.0.1.tgz#0b6fe7b835aecda61c6ea4d4f02c14221e109847"