-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
91 additions
and
32 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
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
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
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
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 |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/** | ||
* More info at: https://en.wikipedia.org/wiki/Order_statistic | ||
*/ | ||
|
||
// O(n) search to find the max of an array | ||
export const max = <T>(array: T[], cmp: (a: T, b: T) => number) => { | ||
let best = 0 | ||
|
||
for (let curr = 1; curr < array.length; curr++) { | ||
if (cmp(array[best], array[curr]) < 0) { | ||
best = curr | ||
} | ||
} | ||
|
||
return array[best] | ||
} | ||
|
||
// O(n) search to find the max of an array | ||
export const min = <T>(array: T[], cmp: (a: T, b: T) => number) => { | ||
let best = 0 | ||
|
||
for (let curr = 1; curr < array.length; curr++) { | ||
if (cmp(array[best], array[curr]) > 0) { | ||
best = curr | ||
} | ||
} | ||
|
||
return array[best] | ||
} |
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 |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { min } from './orderStatistics' | ||
import { bestOfferFirst } from './productStock' | ||
import type { Item } from '../clients/search/types/ProductSearchResult' | ||
|
||
/** | ||
* This function implements Portal heuristics for returning the best sku for a product. | ||
* | ||
* The best sku is the one with the best (cheapest available) offer | ||
* */ | ||
export const pickBestSku = (skus: Item[]) => { | ||
const offersBySku = skus.flatMap((sku) => | ||
sku.sellers.map((seller) => ({ | ||
offer: seller.commertialOffer, | ||
sku, | ||
})) | ||
) | ||
|
||
const best = min(offersBySku, ({ offer: o1 }, { offer: o2 }) => | ||
bestOfferFirst(o1, o2) | ||
) | ||
|
||
return best.sku | ||
} | ||
|
||
export const isValidSkuId = (skuId: string) => | ||
skuId !== '' && !Number.isNaN(Number(skuId)) |