UNPKG

@scayle/storefront-core

Version:

Collection of essential utilities to work with the Storefront API

44 lines (43 loc) 1.4 kB
export const getFirstAttributeValue = (attributes, attributeName) => { const attribute = attributes && attributes[attributeName]; if (!attribute || !attribute.values) { return; } if (attribute.multiSelect) { return attribute.values.length > 0 ? attribute.values[0] : void 0; } return attribute.values; }; export const getAttributeValuesByName = (attributes, name) => { const attribute = attributes && attributes[name]; if (!attribute || !attribute.values) { return []; } if (attribute.multiSelect) { return attribute.values; } return [attribute.values]; }; export const getAttributeValuesByGroupId = (attributes, groupId) => { if (!attributes) { return []; } const attribute = Object.values(attributes).find( (attribute2) => attribute2?.id === groupId ); if (!attribute || !attribute.values) { return []; } if (attribute.multiSelect) { return attribute.values; } return [attribute.values]; }; export const getAttributeValue = (attributes, attributeName) => { const values = getFirstAttributeValue(attributes, attributeName); return values?.value ?? values?.label; }; export const getManyAttributeValueTuples = (attributes, attributeNames) => { return attributeNames.map((key) => getFirstAttributeValue(attributes, key)).filter((entry) => !!entry); }; export { getAttributeValuesByName as getAttributeValueTuples };