Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions src/service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,42 @@ await test('find', async (t) => {
name: OBJECT,
res: obj,
},
// Search functionality tests
{
name: POSTS,
params: { q: 'a' },
res: [post1, post2, post3],
},
{
name: POSTS,
params: { q: 'baz' },
res: [post3],
},
{
name: POSTS,
params: { q: 'c' },
res: [post3],
},
{
name: POSTS,
params: { q: 'foo' },
res: [post1, post3],
},
{
name: POSTS,
params: { q: 'bar' },
res: [post1, post2],
},
{
name: POSTS,
params: { q: 'nonexistent' },
res: [],
},
{
name: COMMENTS,
params: { q: 'a' },
res: [comment1],
},
]
for (const tc of arr) {
await t.test(`${tc.name} ${JSON.stringify(tc.params)}`, () => {
Expand Down
49 changes: 49 additions & 0 deletions src/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,14 @@ export class Service {
return items
}

// Handle full-text search with q parameter
if (query['q'] && typeof query['q'] === 'string') {
const searchTerm = query['q'].toLowerCase()
items = items.filter((item: Item) => {
return this.#searchInItem(item, searchTerm)
})
}

// Convert query params to conditions
const conds: [string, Condition, string | string[]][] = []
for (const [key, value] of Object.entries(query)) {
Expand All @@ -221,6 +229,7 @@ export class Service {
'_limit',
'_page',
'_per_page',
'q',
].includes(key)
) {
continue
Expand Down Expand Up @@ -458,4 +467,44 @@ export class Service {
await this.#db.write()
return item
}

#searchInItem(item: Item, searchTerm: string): boolean {
for (const [, value] of Object.entries(item)) {
if (typeof value === 'string' && value.toLowerCase().includes(searchTerm)) {
return true
}
if (typeof value === 'object' && value !== null) {
if (this.#searchInObject(value as Record<string, unknown>, searchTerm)) {
return true
}
}
}
return false
}

#searchInObject(obj: Record<string, unknown>, searchTerm: string): boolean {
for (const [, value] of Object.entries(obj)) {
if (typeof value === 'string' && value.toLowerCase().includes(searchTerm)) {
return true
}
if (typeof value === 'object' && value !== null && !Array.isArray(value)) {
if (this.#searchInObject(value as Record<string, unknown>, searchTerm)) {
return true
}
}
if (Array.isArray(value)) {
for (const arrayItem of value) {
if (typeof arrayItem === 'string' && arrayItem.toLowerCase().includes(searchTerm)) {
return true
}
if (typeof arrayItem === 'object' && arrayItem !== null) {
if (this.#searchInObject(arrayItem as Record<string, unknown>, searchTerm)) {
return true
}
}
}
}
}
return false
}
}