From b4abf0033a2803c9b28bc72e8f9fcd0be13e0130 Mon Sep 17 00:00:00 2001 From: Marcos Candeia Date: Sat, 15 Apr 2023 14:22:40 -0300 Subject: [PATCH] Remove ratings that were found Signed-off-by: Marcos Candeia --- extensions/yourViews.ts | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/extensions/yourViews.ts b/extensions/yourViews.ts index 8c239bed..f25ac670 100644 --- a/extensions/yourViews.ts +++ b/extensions/yourViews.ts @@ -46,8 +46,7 @@ const getFetcher = ( }; export default function AddYourViews(config: Props): ExtensionOf { - const client = getFetcher(config); - const aggregateRating = aggregateRatingFor(client); + const aggregateRating = aggregateRatingFor(getFetcher(config)); return { aggregateRating, @@ -58,9 +57,15 @@ const sameOrder = ( productIds: readonly string[], elements: Rating[], ): (Rating | undefined)[] => { - const ordered = new Array(productIds.length); + const ordered = new Array(productIds.length).fill( + undefined, + ); for (let i = 0; i < productIds.length; i++) { - ordered[i] = elements.find((e) => e.ProductId === productIds[i]); + const ratingIdx = elements.findIndex((e) => e.ProductId === productIds[i]); + if (ratingIdx !== -1) { + ordered[i] = elements[ratingIdx]; + elements.splice(ratingIdx, 1); // remove already found rating + } } return ordered; };