refactor(marketplace): substring-only q filter, fix test to use real substring

This commit is contained in:
2026-05-21 00:24:01 +02:00
parent 9dcce76f01
commit 181a757323
2 changed files with 3 additions and 12 deletions

View File

@@ -62,7 +62,7 @@ describe('marketplace', () => {
const u = await createUserDirect(env.db, { email: 'u@example.com' });
await createLessonOwnedBy(env.db, o.id, { name: 'Spaans', visibility: 'shared' });
await createLessonOwnedBy(env.db, o.id, { name: 'Frans', visibility: 'shared' });
const r = await listMarketplaceLessons(env.db, u.id, { q: 'span' });
const r = await listMarketplaceLessons(env.db, u.id, { q: 'SPA' });
expect(r.rows).toHaveLength(1);
expect(r.rows[0]!.name).toBe('Spaans');
});

View File

@@ -22,18 +22,9 @@ export async function listMarketplaceLessons(
}
if (params.q && params.q.trim() !== '') {
const q = params.q.trim().toLowerCase();
const matches = (haystack: string): boolean => {
const h = haystack.toLowerCase();
if (h.includes(q)) return true;
// Subsequence match: chars of q appear in order in haystack
let i = 0;
for (let j = 0; j < h.length && i < q.length; j++) {
if (h[j] === q[i]) i++;
}
return i === q.length;
};
candidates = candidates.filter((l) =>
matches(l.name) || matches(l.description ?? '')
l.name.toLowerCase().includes(q)
|| (l.description ?? '').toLowerCase().includes(q)
);
}