-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #203 from hearchco/as/fix/forgot-lazy-results
fix(loadlazyresults): forgot to change field names
- Loading branch information
Showing
1 changed file
with
33 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]; | ||
} |