Skip to content

Commit

Permalink
feat: add min parameter to search query
Browse files Browse the repository at this point in the history
  • Loading branch information
cyberalien committed May 22, 2023
1 parent bcad340 commit c53a1e7
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 3 deletions.
5 changes: 3 additions & 2 deletions src/data/search/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ export function search(
return newItem;
};
const limit = params.limit;
const softLimit = params.softLimit;

interface ExtendedSearchKeywordsEntry extends SearchKeywordsEntry {
// Add prefixes cache to avoid re-calculating it for every partial keyword
Expand Down Expand Up @@ -256,9 +257,9 @@ export function search(
// Extract results
const results: string[] = [];
const prefixes: Set<string> = new Set();
for (let i = 0; i < allMatches.length && results.length < limit; i++) {
for (let i = 0; i < allMatches.length && (softLimit || results.length < limit); i++) {
const { names } = allMatches[i];
for (let j = 0; j < names.length && results.length < limit; j++) {
for (let j = 0; j < names.length && (softLimit || results.length < limit); j++) {
const name = names[j];
results.push(name);
prefixes.add(name.split(':').shift() as string);
Expand Down
9 changes: 9 additions & 0 deletions src/http/responses/search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,15 @@ export function generateAPIv2SearchResponse(query: FastifyRequest['query'], res:
}
params.limit = Math.max(minSearchLimit, Math.min(limit, maxSearchLimit));
}
if (v2Query.min) {
const limit = parseInt(v2Query.min);
if (!limit) {
res.send(400);
return;
}
params.limit = Math.max(minSearchLimit, Math.min(limit, maxSearchLimit));
params.softLimit = true;
}

let start = 0;
if (v2Query.start) {
Expand Down
1 change: 1 addition & 0 deletions src/types/search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ export interface SearchParams {

// Search results limit
limit: number;
softLimit?: boolean; // True if limit can be exceeded

// Toggle partial matches
partial?: boolean;
Expand Down
4 changes: 3 additions & 1 deletion src/types/server/v2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,9 @@ export interface APIv2SearchParams extends APIv2CommonParams {
query: SearchQuery;

// Maximum number of items in response
limit?: number;
// If `min` is set, `limit` is ignored
limit?: number; // Hard limit. Number of results will not exceed `limit`.
min?: number; // Soft limit. Number of results can exceed `limit` if function already retrieved more icons.

// Start index for results
start?: number;
Expand Down

0 comments on commit c53a1e7

Please sign in to comment.