refactor(marketplace): substring-only q filter, fix test to use real substring
This commit is contained in:
@@ -62,7 +62,7 @@ describe('marketplace', () => {
|
|||||||
const u = await createUserDirect(env.db, { email: 'u@example.com' });
|
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: 'Spaans', visibility: 'shared' });
|
||||||
await createLessonOwnedBy(env.db, o.id, { name: 'Frans', 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).toHaveLength(1);
|
||||||
expect(r.rows[0]!.name).toBe('Spaans');
|
expect(r.rows[0]!.name).toBe('Spaans');
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -22,18 +22,9 @@ export async function listMarketplaceLessons(
|
|||||||
}
|
}
|
||||||
if (params.q && params.q.trim() !== '') {
|
if (params.q && params.q.trim() !== '') {
|
||||||
const q = params.q.trim().toLowerCase();
|
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) =>
|
candidates = candidates.filter((l) =>
|
||||||
matches(l.name) || matches(l.description ?? '')
|
l.name.toLowerCase().includes(q)
|
||||||
|
|| (l.description ?? '').toLowerCase().includes(q)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user