Skip to content

Commit

Permalink
Merge pull request #203 from hearchco/as/fix/forgot-lazy-results
Browse files Browse the repository at this point in the history
fix(loadlazyresults): forgot to change field names
  • Loading branch information
aleksasiriski authored Apr 3, 2024
2 parents fda7921 + 83f7dae commit 270b725
Showing 1 changed file with 33 additions and 23 deletions.
56 changes: 33 additions & 23 deletions src/lib/functions/fetchResultsLazily.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,38 @@
import { fetchPublicResultsJSON } from "$lib/functions/fetchPublicResultsJSON";
import type { ResultType } from "$lib/types/result";
import { fetchPublicResultsJSON } from '$lib/functions/fetchPublicResultsJSON';
import type { ResultType } from '$lib/types/result';

export async function fetchResultsLazily(query: string, start: number, offset: number, pages: number, results: ResultType[], paramsString: string): Promise<[number, ResultType[]]> {
// create URLParams object from string and set start parameter
const params: URLSearchParams = new URLSearchParams(paramsString);
params.set("q", query);
params.set("start", (start + offset).toString());
params.set("pages", pages.toString());
export async function fetchResultsLazily(
query: string,
start: number,
offset: number,
pages: number,
results: ResultType[],
paramsString: string
): Promise<[number, ResultType[]]> {
// create URLParams object from string and set start parameter
const params: URLSearchParams = new URLSearchParams(paramsString);
params.set('q', query);
params.set('start', (start + offset).toString());
params.set('pages', pages.toString());

// get additional results
let additionalResults: ResultType[];
try {
additionalResults = await fetchPublicResultsJSON(params);
} catch(err: any) {
throw new Error(`Error fetching additional results: ${err.message}`);
}
// get additional results
let additionalResults: ResultType[];
try {
additionalResults = await fetchPublicResultsJSON(params);
} catch (err: any) {
throw new Error(`Error fetching additional results: ${err.message}`);
}

// append additional results to combined results while removing duplicates
const combinedResults: ResultType[] = [...results, ...additionalResults.filter((r) => !results.some((result) => result.URL === r.URL))];
// append additional results to combined results while removing duplicates
const combinedResults: ResultType[] = [
...results,
...additionalResults.filter((r) => !results.some((result) => result.url === r.url))
];

// adjust new results' ranks to continue from the last rank of the existing results
for (let i: number = results.length; i < combinedResults.length; i++) {
combinedResults[i].Rank = i + 1;
}
// adjust new results' ranks to continue from the last rank of the existing results
for (let i: number = results.length; i < combinedResults.length; i++) {
combinedResults[i].rank = i + 1;
}

return [offset+pages, combinedResults];
}
return [offset + pages, combinedResults];
}

0 comments on commit 270b725

Please sign in to comment.