Skip to content

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

core: remove `matchOnly` logic from fuzzy searcher

Abdullah Atta80a0b75887ef
diff --git a/packages/core/src/utils/__tests__/fuzzy.test.ts b/packages/core/src/utils/__tests__/fuzzy.test.ts
index 5bf9cec3d..569a4e780 100644
--- a/packages/core/src/utils/__tests__/fuzzy.test.ts
+++ b/packages/core/src/utils/__tests__/fuzzy.test.ts
@@ -17,11 +17,11 @@ You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
-import { fuzzy } from "../fuzzy";
+import { fuzzy } from "../fuzzy.js";
import { test, expect, describe } from "vitest";
describe("lookup.fuzzy", () => {
- test("should sort items by score when sort", () => {
+ test("should sort items by score", () => {
const items = [
{
title: "system"
@@ -34,45 +34,7 @@ describe("lookup.fuzzy", () => {
}
];
const query = "ems";
- expect(fuzzy(query, items, "title")).toStrictEqual([
- items[2],
- items[0],
- items[1]
- ]);
- });
- describe("opts.matchOnly", () => {
- test("should return all items when matchOnly is false", () => {
- const items = [
- {
- title: "hello"
- },
- {
- title: "world"
- }
- ];
- const successQuery = "o";
- const failureQuery = "i";
- expect(fuzzy(successQuery, items, "title")).toStrictEqual(items);
- expect(fuzzy(failureQuery, items, "title")).toStrictEqual(items);
- });
- test("should return only matching items when matchOnly is true", () => {
- const items = [
- {
- title: "hello"
- },
- {
- title: "world"
- }
- ];
- const successQuery = "or";
- const failureQuery = "i";
- expect(
- fuzzy(successQuery, items, "title", { matchOnly: true })
- ).toStrictEqual([items[1]]);
- expect(
- fuzzy(failureQuery, items, "title", { matchOnly: true })
- ).toStrictEqual([]);
- });
+ expect(fuzzy(query, items, "title")).toStrictEqual([items[2]]);
});
describe("opts.prefix", () => {
test("should prefix matched field with provided value when given", () => {
@@ -89,7 +51,7 @@ describe("lookup.fuzzy", () => {
fuzzy(query, items, "title", {
prefix: "prefix-"
})
- ).toStrictEqual([items[0], { title: "worlprefix-d" }]);
+ ).toStrictEqual([{ title: "worlprefix-d" }]);
});
});
describe("opt.suffix", () => {
@@ -107,7 +69,7 @@ describe("lookup.fuzzy", () => {
fuzzy(query, items, "title", {
suffix: "-suffix"
})
- ).toStrictEqual([{ title: "hello-suffix" }, items[1]]);
+ ).toStrictEqual([{ title: "hello-suffix" }]);
});
});
});
diff --git a/packages/core/src/utils/fuzzy.ts b/packages/core/src/utils/fuzzy.ts
index 3424b24c6..89c4e78f8 100644
--- a/packages/core/src/utils/fuzzy.ts
+++ b/packages/core/src/utils/fuzzy.ts
@@ -26,10 +26,6 @@ export function fuzzy<T>(
opts?: {
prefix?: string;
suffix?: string;
- /**
- * If true, only items that match the query will be returned
- */
- matchOnly?: boolean;
}
): T[] {
if (query === "") return items;
@@ -38,11 +34,8 @@ export function fuzzy<T>(
for (const item of items) {
const result = match(query, `${item[key]}`);
- if (!result.match) {
- if (opts?.matchOnly) continue;
- fuzzied.push([item, result.score]);
- continue;
- }
+ if (!result.match) continue;
+
if (opts?.prefix || opts?.suffix) {
fuzzied.push([
{
@@ -55,9 +48,7 @@ export function fuzzy<T>(
},
result.score
]);
- continue;
- }
- fuzzied.push([item, result.score]);
+ } else fuzzied.push([item, result.score]);
}
return fuzzied.sort((a, b) => b[1] - a[1]).map((f) => f[0]);