18 lines
567 B
TypeScript
18 lines
567 B
TypeScript
import type { Product, Sku } from "@vmall/shared";
|
|
|
|
/**
|
|
* Cheapest active SKU, preferring one that is in stock. A pure helper over a
|
|
* `Product`, so it lives here rather than in the fixed-data module.
|
|
*/
|
|
export function lowestSku(product: Product): Sku | null {
|
|
const active = (product.skus ?? []).filter((sku) => sku.active && sku.stock > 0);
|
|
return (
|
|
active.reduce<Sku | null>(
|
|
(low, sku) => (low === null || sku.price_minor < low.price_minor ? sku : low),
|
|
null,
|
|
) ??
|
|
(product.skus ?? []).find((sku) => sku.active) ??
|
|
null
|
|
);
|
|
}
|