@commercelayer/react-components
Version:
The Official Commerce Layer React Components
2 lines • 962 B
JavaScript
"use client";
import{createElement as _createElement}from"react";import{jsx as _jsx}from"react/jsx-runtime";import{isEmpty,first,isArray,has}from"lodash";import PriceTemplate from"../components/utils/PriceTemplate";export function getPriceByCode(skuPrices,code=""){return code?first(skuPrices.filter(p=>p.currency_code===code)):first(skuPrices)}export function getPricesComponent(skuPrices,props){return isEmpty(skuPrices)?_jsx(PriceTemplate,{...props}):skuPrices.map((p,k)=>{const showCompare=typeof props.showCompare>"u"&&p?.compare_at_amount_cents!=null&&p?.compare_at_amount_cents>p?.amount_cents||props.showCompare;return _createElement(PriceTemplate,{...props,key:k,showCompare,formattedAmount:p.formatted_amount,formattedCompare:p.formatted_compare_at_amount,skuCode:p.sku_code})})}export default function getPrices(prices){const obj={};return isArray(prices)&&prices.forEach(p=>{const sku=p?.sku_code??"";has(obj,sku)?obj[sku].push(p):obj[sku]=[p]}),obj}