@ubnt/store.core 9.11.1 → 9.12.0-alpha.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +59 -2
- package/lib/_virtual/_virtual_multi-entry.js +15 -2
- package/lib/_virtual/_virtual_multi-entry.js.map +1 -1
- package/lib/_virtual/_virtual_multi-entry.mjs +2 -0
- package/lib/_virtual/_virtual_multi-entry.mjs.map +1 -1
- package/lib/assets/ai360.jpg +0 -0
- package/lib/assets/attention.svg +5 -5
- package/lib/assets/attention2.svg +5 -5
- package/lib/assets/check2.svg +3 -3
- package/lib/assets/check3.svg +3 -3
- package/lib/assets/close2.svg +3 -2
- package/lib/assets/close3.svg +2 -3
- package/lib/assets/g5-ptz.jpg +0 -0
- package/lib/assets/g5Ptz.jpg +0 -0
- package/lib/assets/keyFeatures/music.svg +3 -0
- package/lib/{components/Explore/assets/ProAV.jpg.js → assets/keyFeatures/music.svg.js} +6 -6
- package/lib/assets/keyFeatures/music.svg.js.map +1 -0
- package/lib/assets/keyFeatures/music.svg.mjs +3 -0
- package/lib/assets/keyFeatures/music.svg.mjs.map +1 -0
- package/lib/assets/minus4.svg +8 -8
- package/lib/assets/minus5.svg +8 -8
- package/lib/assets/music.svg +3 -0
- package/lib/assets/pause.svg +3 -3
- package/lib/assets/pause2.svg +3 -3
- package/lib/assets/plus5.svg +9 -9
- package/lib/assets/plus6.svg +9 -9
- package/lib/assets/powerAmp.jpg +0 -0
- package/lib/assets/powerAmp.svg +3 -0
- package/lib/assets/ucgMax.jpg +0 -0
- package/lib/assets/ucgMax2.jpg +0 -0
- package/lib/assets/ucgUltra.jpg +0 -0
- package/lib/assets/ucgUltra2.jpg +0 -0
- package/lib/assets/udr.jpg +0 -0
- package/lib/assets/udr2.jpg +0 -0
- package/lib/assets/udw.jpg +0 -0
- package/lib/assets/udw2.jpg +0 -0
- package/lib/assets/umr.jpg +0 -0
- package/lib/assets/umr2.jpg +0 -0
- package/lib/assets/wideAngle.jpg +0 -0
- package/lib/components/Explore/Explore.js +2 -9
- package/lib/components/Explore/Explore.js.map +1 -1
- package/lib/components/Explore/Explore.mjs +2 -9
- package/lib/components/Explore/Explore.mjs.map +1 -1
- package/lib/components/Explore/ExploreData.d.ts +4 -7
- package/lib/components/Explore/ExploreData.js +79 -139
- package/lib/components/Explore/ExploreData.js.map +1 -1
- package/lib/components/Explore/ExploreData.mjs +71 -129
- package/lib/components/Explore/ExploreData.mjs.map +1 -1
- package/lib/components/Explore/assets/UISPConsole.jpg.js +5 -5
- package/lib/components/Explore/assets/{baseStation-mobile.jpg.js → baseStation--mobile.jpg.js} +6 -6
- package/lib/components/Explore/assets/baseStation--mobile.jpg.js.map +1 -0
- package/lib/components/Explore/assets/baseStation--mobile.jpg.mjs +3 -0
- package/lib/components/Explore/assets/baseStation--mobile.jpg.mjs.map +1 -0
- package/lib/components/Explore/assets/baseStation.jpg.js +5 -5
- package/lib/components/Explore/assets/{g4-ptz.jpg.js → g4Ptz.jpg.js} +2 -2
- package/lib/components/Explore/assets/g4Ptz.jpg.js.map +1 -0
- package/lib/components/Explore/assets/g4Ptz.jpg.mjs +3 -0
- package/lib/components/Explore/assets/g4Ptz.jpg.mjs.map +1 -0
- package/lib/components/Explore/assets/{g5-ptz.jpg.js → g5Ptz.jpg.js} +2 -2
- package/lib/components/Explore/assets/g5Ptz.jpg.js.map +1 -0
- package/lib/components/Explore/assets/g5Ptz.jpg.mjs +3 -0
- package/lib/components/Explore/assets/g5Ptz.jpg.mjs.map +1 -0
- package/lib/components/Explore/assets/{AITheta.jpg.js → powerAmp.jpg.js} +6 -6
- package/lib/components/Explore/assets/powerAmp.jpg.js.map +1 -0
- package/lib/components/Explore/assets/powerAmp.jpg.mjs +3 -0
- package/lib/components/Explore/assets/powerAmp.jpg.mjs.map +1 -0
- package/lib/components/Explore/{LensCompare/assets/ai360.jpg.js → assets/udr.jpg.js} +6 -6
- package/lib/components/Explore/assets/udr.jpg.js.map +1 -0
- package/lib/components/Explore/assets/udr.jpg.mjs +3 -0
- package/lib/components/Explore/assets/udr.jpg.mjs.map +1 -0
- package/lib/components/Explore/assets/udw.jpg.js +27 -0
- package/lib/components/Explore/assets/udw.jpg.js.map +1 -0
- package/lib/components/Explore/assets/udw.jpg.mjs +3 -0
- package/lib/components/Explore/assets/udw.jpg.mjs.map +1 -0
- package/lib/components/Explore/assets/umr.jpg.js +27 -0
- package/lib/components/Explore/assets/umr.jpg.js.map +1 -0
- package/lib/components/Explore/assets/umr.jpg.mjs +3 -0
- package/lib/components/Explore/assets/umr.jpg.mjs.map +1 -0
- package/lib/components/Explore/assets/ux.jpg.js +27 -0
- package/lib/components/Explore/assets/ux.jpg.js.map +1 -0
- package/lib/components/Explore/assets/ux.jpg.mjs +3 -0
- package/lib/components/Explore/assets/ux.jpg.mjs.map +1 -0
- package/lib/components/Explore/translations/en.json.js +8 -30
- package/lib/components/Explore/translations/en.json.js.map +1 -1
- package/lib/components/Explore/translations/en.json.mjs +9 -30
- package/lib/components/Explore/translations/en.json.mjs.map +1 -1
- package/lib/components/FeaturesSection_deprecated/FeaturesSection.js +1 -1
- package/lib/components/FeaturesSection_deprecated/FeaturesSection.mjs +1 -1
- package/lib/components/FeaturesSection_deprecated/assets/close.svg.js +1 -1
- package/lib/components/FeaturesSection_deprecated/assets/close.svg.mjs +2 -2
- package/lib/components/Header/ProHeader/ProHeader.js +19 -2
- package/lib/components/Header/ProHeader/ProHeader.js.map +1 -1
- package/lib/components/Header/ProHeader/ProHeader.mjs +19 -2
- package/lib/components/Header/ProHeader/ProHeader.mjs.map +1 -1
- package/lib/components/Header/ProHeader/ProHeader.styles.d.ts +1 -0
- package/lib/components/Header/ProHeader/ProHeader.styles.js +6 -0
- package/lib/components/Header/ProHeader/ProHeader.styles.js.map +1 -1
- package/lib/components/Header/ProHeader/ProHeader.styles.mjs +6 -0
- package/lib/components/Header/ProHeader/ProHeader.styles.mjs.map +1 -1
- package/lib/components/LandingPagePro/LandingPagePro.d.ts +3 -0
- package/lib/components/LandingPagePro/LandingPagePro.js +3 -3
- package/lib/components/LandingPagePro/LandingPagePro.js.map +1 -1
- package/lib/components/LandingPagePro/LandingPagePro.mjs +2 -2
- package/lib/components/LandingPagePro/LandingPagePro.mjs.map +1 -1
- package/lib/components/MicrositeVideoSlideshow/MicrositeVideoSlideshow.js +1 -1
- package/lib/components/MicrositeVideoSlideshow/MicrositeVideoSlideshow.mjs +1 -1
- package/lib/components/MicrositeVideoSlideshow/assets/pause.svg.js +1 -1
- package/lib/components/MicrositeVideoSlideshow/assets/pause.svg.mjs +2 -2
- package/lib/components/ModelCompareV3/ModelComparePropertyValueGridItem.js +1 -1
- package/lib/components/ModelCompareV3/ModelComparePropertyValueGridItem.mjs +1 -1
- package/lib/components/ModelCompareV3/assets/Consoles/bigCompare/ucgMax.jpg.js +5 -5
- package/lib/components/ModelCompareV3/assets/Consoles/bigCompare/ucgMax.jpg.mjs +2 -2
- package/lib/components/ModelCompareV3/assets/Consoles/bigCompare/ucgUltra.jpg.js +5 -5
- package/lib/components/ModelCompareV3/assets/Consoles/bigCompare/ucgUltra.jpg.mjs +2 -2
- package/lib/components/ModelCompareV3/assets/Consoles/bigCompare/udr.jpg.js +1 -1
- package/lib/components/ModelCompareV3/assets/Consoles/bigCompare/udr.jpg.mjs +2 -2
- package/lib/components/ModelCompareV3/assets/Consoles/bigCompare/udw.jpg.js +1 -1
- package/lib/components/ModelCompareV3/assets/Consoles/bigCompare/udw.jpg.mjs +2 -2
- package/lib/components/ModelCompareV3/assets/Consoles/ucgMax.jpg.js +1 -1
- package/lib/components/ModelCompareV3/assets/Consoles/ucgMax.jpg.mjs +2 -2
- package/lib/components/ModelCompareV3/assets/Consoles/ucgUltra.jpg.js +1 -1
- package/lib/components/ModelCompareV3/assets/Consoles/ucgUltra.jpg.mjs +2 -2
- package/lib/components/ModelCompareV3/assets/Interior/ai360.jpg.js +1 -1
- package/lib/components/ModelCompareV3/assets/Interior/ai360.jpg.mjs +2 -2
- package/lib/components/ModelCompareV3/assets/Interior/wideAngle.jpg.js +1 -1
- package/lib/components/ModelCompareV3/assets/Interior/wideAngle.jpg.mjs +2 -2
- package/lib/components/ModelCompareV3/assets/MobileRouters/umr.jpg.js +1 -1
- package/lib/components/ModelCompareV3/assets/MobileRouters/umr.jpg.mjs +2 -2
- package/lib/components/ModelCompareV3/assets/PTZ/g5-ptz.jpg.js +1 -1
- package/lib/components/ModelCompareV3/assets/PTZ/g5-ptz.jpg.mjs +2 -2
- package/lib/components/ModelCompareV3/assets/check.svg.js +1 -1
- package/lib/components/ModelCompareV3/assets/check.svg.mjs +2 -2
- package/lib/components/ModelCompareV3/data/CloudGateways.js +8 -8
- package/lib/components/ModelCompareV3/data/CloudGateways.mjs +6 -6
- package/lib/components/ModelCompareV3/data/products/g5Ptz.js +1 -1
- package/lib/components/ModelCompareV3/data/products/g5Ptz.mjs +1 -1
- package/lib/components/ModelCompareV3/data/products/theta360.js +1 -1
- package/lib/components/ModelCompareV3/data/products/theta360.mjs +1 -1
- package/lib/components/ModelCompareV3/data/products/thetaWideAngle.js +1 -1
- package/lib/components/ModelCompareV3/data/products/thetaWideAngle.mjs +1 -1
- package/lib/components/ModelCompareV3/data/products/ucgMax.js +1 -1
- package/lib/components/ModelCompareV3/data/products/ucgMax.mjs +1 -1
- package/lib/components/ModelCompareV3/data/products/ucgUltra.js +1 -1
- package/lib/components/ModelCompareV3/data/products/ucgUltra.mjs +1 -1
- package/lib/components/ModelCompareV3/data/products/udr.js +1 -1
- package/lib/components/ModelCompareV3/data/products/udr.mjs +1 -1
- package/lib/components/ModelCompareV3/data/products/udw.js +1 -1
- package/lib/components/ModelCompareV3/data/products/udw.mjs +1 -1
- package/lib/components/ModelCompareV3/data/products/umr.js +1 -1
- package/lib/components/ModelCompareV3/data/products/umr.mjs +1 -1
- package/lib/components/ResourceCalculatorV1/ComputeResourceCalculator/ComputeResourceCalculator.constants.js +6 -1
- package/lib/components/ResourceCalculatorV1/ComputeResourceCalculator/ComputeResourceCalculator.constants.js.map +1 -1
- package/lib/components/ResourceCalculatorV1/ComputeResourceCalculator/ComputeResourceCalculator.constants.mjs +6 -1
- package/lib/components/ResourceCalculatorV1/ComputeResourceCalculator/ComputeResourceCalculator.constants.mjs.map +1 -1
- package/lib/components/ResourceCalculatorV1/ComputeResourceCalculator/components/InputCard/InputCard.js +2 -2
- package/lib/components/ResourceCalculatorV1/ComputeResourceCalculator/components/InputCard/InputCard.js.map +1 -1
- package/lib/components/ResourceCalculatorV1/ComputeResourceCalculator/components/InputCard/InputCard.mjs +2 -2
- package/lib/components/ResourceCalculatorV1/ComputeResourceCalculator/components/InputCard/InputCard.mjs.map +1 -1
- package/lib/components/ResourceCalculatorV1/ComputeResourceCalculator/components/InputCard/InputCard.styles.js +2 -0
- package/lib/components/ResourceCalculatorV1/ComputeResourceCalculator/components/InputCard/InputCard.styles.js.map +1 -1
- package/lib/components/ResourceCalculatorV1/ComputeResourceCalculator/components/InputCard/InputCard.styles.mjs +2 -0
- package/lib/components/ResourceCalculatorV1/ComputeResourceCalculator/components/InputCard/InputCard.styles.mjs.map +1 -1
- package/lib/components/ResourceCalculatorV1/ComputeResourceCalculator/components/InputCard/InputSections/CounterSection.js +2 -2
- package/lib/components/ResourceCalculatorV1/ComputeResourceCalculator/components/InputCard/InputSections/CounterSection.mjs +2 -2
- package/lib/components/ResourceCalculatorV1/ComputeResourceCalculator/components/InputCard/assets/attention.svg.js +1 -1
- package/lib/components/ResourceCalculatorV1/ComputeResourceCalculator/components/InputCard/assets/attention.svg.mjs +2 -2
- package/lib/components/ResourceCalculatorV1/ComputeResourceCalculator/components/InputCard/assets/minus.svg.js +1 -1
- package/lib/components/ResourceCalculatorV1/ComputeResourceCalculator/components/InputCard/assets/minus.svg.mjs +2 -2
- package/lib/components/ResourceCalculatorV1/ComputeResourceCalculator/components/InputCard/assets/plus.svg.js +1 -1
- package/lib/components/ResourceCalculatorV1/ComputeResourceCalculator/components/InputCard/assets/plus.svg.mjs +2 -2
- package/lib/components/ResourceCalculatorV1/ComputeResourceCalculator/components/ProgressBarChart/ProgressBarChart.js +1 -1
- package/lib/components/ResourceCalculatorV1/ComputeResourceCalculator/components/ProgressBarChart/ProgressBarChart.mjs +1 -1
- package/lib/components/ResourceCalculatorV1/ComputeResourceCalculator/components/ProgressBarChart/assets/attention.svg.js +1 -1
- package/lib/components/ResourceCalculatorV1/ComputeResourceCalculator/components/ProgressBarChart/assets/attention.svg.mjs +2 -2
- package/lib/components/ResourceCalculatorV1/PoeCalculator/components/PoeConsumerConfigurator/PoeConsumerSelectModal/PoeConsumerSelectModal.js +2 -2
- package/lib/components/ResourceCalculatorV1/PoeCalculator/components/PoeConsumerConfigurator/PoeConsumerSelectModal/PoeConsumerSelectModal.mjs +2 -2
- package/lib/components/ResourceCalculatorV1/PoeCalculator/components/PoeConsumerConfigurator/assets/minus.svg.js +1 -1
- package/lib/components/ResourceCalculatorV1/PoeCalculator/components/PoeConsumerConfigurator/assets/minus.svg.mjs +2 -2
- package/lib/components/ResourceCalculatorV1/PoeCalculator/components/PoeConsumerConfigurator/assets/plus.svg.js +1 -1
- package/lib/components/ResourceCalculatorV1/PoeCalculator/components/PoeConsumerConfigurator/assets/plus.svg.mjs +2 -2
- package/lib/components/Tooltip/Tooltip.d.ts +2 -0
- package/lib/components/Tooltip/Tooltip.js +45 -0
- package/lib/components/Tooltip/Tooltip.js.map +1 -0
- package/lib/components/Tooltip/Tooltip.mjs +43 -0
- package/lib/components/Tooltip/Tooltip.mjs.map +1 -0
- package/lib/components/Tooltip/Tooltip.styles.d.ts +32 -0
- package/lib/components/Tooltip/Tooltip.styles.js +44 -0
- package/lib/components/Tooltip/Tooltip.styles.js.map +1 -0
- package/lib/components/Tooltip/Tooltip.styles.mjs +34 -0
- package/lib/components/Tooltip/Tooltip.styles.mjs.map +1 -0
- package/lib/components/Tooltip/index.d.ts +1 -0
- package/lib/components/Tooltip/index.js +6 -0
- package/lib/components/Tooltip/index.js.map +1 -0
- package/lib/components/Tooltip/index.mjs +3 -0
- package/lib/components/Tooltip/index.mjs.map +1 -0
- package/lib/components/VariantSelector/CheckBox/CheckBox.js +1 -1
- package/lib/components/VariantSelector/CheckBox/CheckBox.mjs +1 -1
- package/lib/components/VariantSelector/CheckBox/assets/check.svg.js +1 -1
- package/lib/components/VariantSelector/CheckBox/assets/check.svg.mjs +2 -2
- package/lib/components/VideoPlayerNew/VideoPlayer.js +2 -2
- package/lib/components/VideoPlayerNew/VideoPlayer.mjs +2 -2
- package/lib/components/VideoPlayerNew/assets/close.svg.js +1 -1
- package/lib/components/VideoPlayerNew/assets/close.svg.mjs +2 -2
- package/lib/components/VideoPlayerNew/assets/pause.svg.js +1 -1
- package/lib/components/VideoPlayerNew/assets/pause.svg.mjs +2 -2
- package/lib/constants/icons/menu/index.d.ts +1 -0
- package/lib/constants/icons/menu/index.js +9 -0
- package/lib/constants/icons/menu/index.js.map +1 -1
- package/lib/constants/icons/menu/index.mjs +2 -0
- package/lib/constants/icons/menu/index.mjs.map +1 -1
- package/lib/{components/Explore/assets/MobileR.jpg.js → constants/icons/menu/powerAmp.svg.js} +6 -6
- package/lib/constants/icons/menu/powerAmp.svg.js.map +1 -0
- package/lib/constants/icons/menu/powerAmp.svg.mjs +3 -0
- package/lib/constants/icons/menu/powerAmp.svg.mjs.map +1 -0
- package/lib/constants/keyFeatureIcons.d.ts +1 -0
- package/lib/constants/keyFeatureIcons.js +3 -0
- package/lib/constants/keyFeatureIcons.js.map +1 -1
- package/lib/constants/keyFeatureIcons.mjs +3 -0
- package/lib/constants/keyFeatureIcons.mjs.map +1 -1
- package/lib/constants/proStoreCategories/mapping/index.d.ts +1 -0
- package/lib/constants/proStoreCategories/mapping/index.js +13 -8
- package/lib/constants/proStoreCategories/mapping/index.js.map +1 -1
- package/lib/constants/proStoreCategories/mapping/index.mjs +13 -8
- package/lib/constants/proStoreCategories/mapping/index.mjs.map +1 -1
- package/lib/constants/proStoreCategories/mapping/unifi.d.ts +2 -1
- package/lib/constants/proStoreCategories/mapping/unifi.js +1 -0
- package/lib/constants/proStoreCategories/mapping/unifi.js.map +1 -1
- package/lib/constants/proStoreCategories/mapping/unifi.mjs +1 -0
- package/lib/constants/proStoreCategories/mapping/unifi.mjs.map +1 -1
- package/lib/constants/proStoreCategories/translations/en.json.js +3 -0
- package/lib/constants/proStoreCategories/translations/en.json.js.map +1 -1
- package/lib/constants/proStoreCategories/translations/en.json.mjs +3 -1
- package/lib/constants/proStoreCategories/translations/en.json.mjs.map +1 -1
- package/lib/constants/proStoreCategories/translations/fr.json.js +1 -1
- package/lib/constants/proStoreCategories/translations/fr.json.mjs +1 -1
- package/lib/constants/proStoreCategories/translations/zh-Hans.json.js +1 -1
- package/lib/constants/proStoreCategories/translations/zh-Hans.json.mjs +1 -1
- package/lib/constants/proStoreCategories/translations/zh-Hant-TW.json.js +1 -1
- package/lib/constants/proStoreCategories/translations/zh-Hant-TW.json.mjs +1 -1
- package/lib/constants/shopify/keyFeatureIcons.js +4 -0
- package/lib/constants/shopify/keyFeatureIcons.js.map +1 -1
- package/lib/constants/shopify/keyFeatureIcons.mjs +3 -0
- package/lib/constants/shopify/keyFeatureIcons.mjs.map +1 -1
- package/lib/contexts/MenuContext/mapping.js +20 -11
- package/lib/contexts/MenuContext/mapping.js.map +1 -1
- package/lib/contexts/MenuContext/mapping.mjs +19 -11
- package/lib/contexts/MenuContext/mapping.mjs.map +1 -1
- package/lib/helpers/index.d.ts +1 -0
- package/lib/helpers/index.js +11 -0
- package/lib/helpers/index.js.map +1 -1
- package/lib/helpers/index.mjs +1 -0
- package/lib/helpers/index.mjs.map +1 -1
- package/lib/helpers/product.d.ts +87 -0
- package/lib/helpers/product.js +346 -0
- package/lib/helpers/product.js.map +1 -0
- package/lib/helpers/product.mjs +335 -0
- package/lib/helpers/product.mjs.map +1 -0
- package/lib/types/index.d.ts +1 -0
- package/lib/types/index.js +2 -0
- package/lib/types/index.js.map +1 -1
- package/lib/types/index.mjs +1 -0
- package/lib/types/index.mjs.map +1 -1
- package/lib/types/product.d.ts +87 -0
- package/lib/types/product.js +14 -0
- package/lib/types/product.js.map +1 -0
- package/lib/types/product.mjs +13 -0
- package/lib/types/product.mjs.map +1 -0
- package/lib/utils/array.d.ts +4 -0
- package/lib/utils/array.js +10 -0
- package/lib/utils/array.js.map +1 -1
- package/lib/utils/array.mjs +10 -1
- package/lib/utils/array.mjs.map +1 -1
- package/lib/utils/money/allocators/fixedAmount.d.ts +26 -0
- package/lib/utils/money/allocators/fixedAmount.js +55 -0
- package/lib/utils/money/allocators/fixedAmount.js.map +1 -0
- package/lib/utils/money/allocators/fixedAmount.mjs +53 -0
- package/lib/utils/money/allocators/fixedAmount.mjs.map +1 -0
- package/lib/utils/money/allocators/index.d.ts +2 -0
- package/lib/utils/money/allocators/index.js +8 -0
- package/lib/utils/money/allocators/index.js.map +1 -0
- package/lib/utils/money/allocators/index.mjs +4 -0
- package/lib/utils/money/allocators/index.mjs.map +1 -0
- package/lib/utils/money/allocators/productAmount.d.ts +13 -0
- package/lib/utils/money/allocators/productAmount.js +110 -0
- package/lib/utils/money/allocators/productAmount.js.map +1 -0
- package/lib/utils/money/allocators/productAmount.mjs +108 -0
- package/lib/utils/money/allocators/productAmount.mjs.map +1 -0
- package/lib/utils/money/constants.d.ts +56 -25
- package/lib/utils/money/constants.js +72 -7
- package/lib/utils/money/constants.js.map +1 -1
- package/lib/utils/money/constants.mjs +70 -7
- package/lib/utils/money/constants.mjs.map +1 -1
- package/lib/utils/money/exchangeRate.d.ts +31 -0
- package/lib/utils/money/exchangeRate.js +27 -0
- package/lib/utils/money/exchangeRate.js.map +1 -0
- package/lib/utils/money/exchangeRate.mjs +23 -0
- package/lib/utils/money/exchangeRate.mjs.map +1 -0
- package/lib/utils/money/format.d.ts +10 -5
- package/lib/utils/money/format.js +22 -14
- package/lib/utils/money/format.js.map +1 -1
- package/lib/utils/money/format.mjs +23 -16
- package/lib/utils/money/format.mjs.map +1 -1
- package/lib/utils/money/index.d.ts +4 -1
- package/lib/utils/money/index.js +26 -8
- package/lib/utils/money/index.js.map +1 -1
- package/lib/utils/money/index.mjs +8 -4
- package/lib/utils/money/index.mjs.map +1 -1
- package/lib/utils/money/misc.d.ts +68 -28
- package/lib/utils/money/misc.js +35 -64
- package/lib/utils/money/misc.js.map +1 -1
- package/lib/utils/money/misc.mjs +32 -43
- package/lib/utils/money/misc.mjs.map +1 -1
- package/lib/utils/money/misc.private.d.ts +23 -0
- package/lib/utils/money/misc.private.js +46 -0
- package/lib/utils/money/misc.private.js.map +1 -0
- package/lib/utils/money/misc.private.mjs +21 -0
- package/lib/utils/money/misc.private.mjs.map +1 -0
- package/lib/utils/money/shipping.d.ts +5 -0
- package/lib/utils/money/shipping.js +8 -0
- package/lib/utils/money/shipping.js.map +1 -0
- package/lib/utils/money/shipping.mjs +6 -0
- package/lib/utils/money/shipping.mjs.map +1 -0
- package/lib/utils/money/tax.d.ts +38 -2
- package/lib/utils/money/tax.js +46 -10
- package/lib/utils/money/tax.js.map +1 -1
- package/lib/utils/money/tax.mjs +42 -11
- package/lib/utils/money/tax.mjs.map +1 -1
- package/lib/utils/money/tax.private.d.ts +17 -0
- package/lib/utils/money/tax.private.js +65 -0
- package/lib/utils/money/tax.private.js.map +1 -0
- package/lib/utils/money/tax.private.mjs +60 -0
- package/lib/utils/money/tax.private.mjs.map +1 -0
- package/lib/utils/money/types.d.ts +78 -3
- package/lib/utils/number.d.ts +1 -1
- package/lib/utils/number.js +1 -1
- package/lib/utils/number.js.map +1 -1
- package/lib/utils/number.mjs +1 -1
- package/lib/utils/number.mjs.map +1 -1
- package/lib/utils/productSort.d.ts +41 -0
- package/lib/utils/productSort.js +66 -0
- package/lib/utils/productSort.js.map +1 -0
- package/lib/utils/productSort.mjs +59 -0
- package/lib/utils/productSort.mjs.map +1 -0
- package/package.json +53 -53
- package/lib/assets/AITheta.jpg +0 -0
- package/lib/assets/MobileR.jpg +0 -0
- package/lib/assets/ProAV.jpg +0 -0
- package/lib/assets/ai3602.jpg +0 -0
- package/lib/assets/cloudgatewayultra.jpg +0 -0
- package/lib/assets/dreamRouter.jpg +0 -0
- package/lib/assets/dreamWall.jpg +0 -0
- package/lib/assets/g5-ptz2.jpg +0 -0
- package/lib/assets/wideAngle2.jpg +0 -0
- package/lib/components/Explore/LensCompare/LensCompare.d.ts +0 -2
- package/lib/components/Explore/LensCompare/LensCompare.js +0 -111
- package/lib/components/Explore/LensCompare/LensCompare.js.map +0 -1
- package/lib/components/Explore/LensCompare/LensCompare.mjs +0 -109
- package/lib/components/Explore/LensCompare/LensCompare.mjs.map +0 -1
- package/lib/components/Explore/LensCompare/LensCompare.styles.d.ts +0 -16
- package/lib/components/Explore/LensCompare/LensCompare.styles.js +0 -153
- package/lib/components/Explore/LensCompare/LensCompare.styles.js.map +0 -1
- package/lib/components/Explore/LensCompare/LensCompare.styles.mjs +0 -132
- package/lib/components/Explore/LensCompare/LensCompare.styles.mjs.map +0 -1
- package/lib/components/Explore/LensCompare/LensCompare.types.d.ts +0 -9
- package/lib/components/Explore/LensCompare/LensCompare.utils.d.ts +0 -2
- package/lib/components/Explore/LensCompare/LensCompare.utils.js +0 -23
- package/lib/components/Explore/LensCompare/LensCompare.utils.js.map +0 -1
- package/lib/components/Explore/LensCompare/LensCompare.utils.mjs +0 -21
- package/lib/components/Explore/LensCompare/LensCompare.utils.mjs.map +0 -1
- package/lib/components/Explore/LensCompare/LensCompareData.d.ts +0 -29
- package/lib/components/Explore/LensCompare/LensCompareData.js +0 -68
- package/lib/components/Explore/LensCompare/LensCompareData.js.map +0 -1
- package/lib/components/Explore/LensCompare/LensCompareData.mjs +0 -58
- package/lib/components/Explore/LensCompare/LensCompareData.mjs.map +0 -1
- package/lib/components/Explore/LensCompare/assets/ai360.jpg.js.map +0 -1
- package/lib/components/Explore/LensCompare/assets/ai360.jpg.mjs +0 -3
- package/lib/components/Explore/LensCompare/assets/ai360.jpg.mjs.map +0 -1
- package/lib/components/Explore/LensCompare/assets/wideAngle.jpg.js +0 -27
- package/lib/components/Explore/LensCompare/assets/wideAngle.jpg.js.map +0 -1
- package/lib/components/Explore/LensCompare/assets/wideAngle.jpg.mjs +0 -3
- package/lib/components/Explore/LensCompare/assets/wideAngle.jpg.mjs.map +0 -1
- package/lib/components/Explore/assets/AITheta.jpg.js.map +0 -1
- package/lib/components/Explore/assets/AITheta.jpg.mjs +0 -3
- package/lib/components/Explore/assets/AITheta.jpg.mjs.map +0 -1
- package/lib/components/Explore/assets/MobileR.jpg.js.map +0 -1
- package/lib/components/Explore/assets/MobileR.jpg.mjs +0 -3
- package/lib/components/Explore/assets/MobileR.jpg.mjs.map +0 -1
- package/lib/components/Explore/assets/ProAV.jpg.js.map +0 -1
- package/lib/components/Explore/assets/ProAV.jpg.mjs +0 -3
- package/lib/components/Explore/assets/ProAV.jpg.mjs.map +0 -1
- package/lib/components/Explore/assets/baseStation-mobile.jpg.js.map +0 -1
- package/lib/components/Explore/assets/baseStation-mobile.jpg.mjs +0 -3
- package/lib/components/Explore/assets/baseStation-mobile.jpg.mjs.map +0 -1
- package/lib/components/Explore/assets/cloudgatewayultra.jpg.js +0 -27
- package/lib/components/Explore/assets/cloudgatewayultra.jpg.js.map +0 -1
- package/lib/components/Explore/assets/cloudgatewayultra.jpg.mjs +0 -3
- package/lib/components/Explore/assets/cloudgatewayultra.jpg.mjs.map +0 -1
- package/lib/components/Explore/assets/dreamRouter.jpg.js +0 -27
- package/lib/components/Explore/assets/dreamRouter.jpg.js.map +0 -1
- package/lib/components/Explore/assets/dreamRouter.jpg.mjs +0 -3
- package/lib/components/Explore/assets/dreamRouter.jpg.mjs.map +0 -1
- package/lib/components/Explore/assets/dreamWall.jpg.js +0 -27
- package/lib/components/Explore/assets/dreamWall.jpg.js.map +0 -1
- package/lib/components/Explore/assets/dreamWall.jpg.mjs +0 -3
- package/lib/components/Explore/assets/dreamWall.jpg.mjs.map +0 -1
- package/lib/components/Explore/assets/g4-ptz.jpg.js.map +0 -1
- package/lib/components/Explore/assets/g4-ptz.jpg.mjs +0 -3
- package/lib/components/Explore/assets/g4-ptz.jpg.mjs.map +0 -1
- package/lib/components/Explore/assets/g5-ptz.jpg.js.map +0 -1
- package/lib/components/Explore/assets/g5-ptz.jpg.mjs +0 -3
- package/lib/components/Explore/assets/g5-ptz.jpg.mjs.map +0 -1
- package/lib/components/Explore/assets/unifiExpress.jpg.js +0 -27
- package/lib/components/Explore/assets/unifiExpress.jpg.js.map +0 -1
- package/lib/components/Explore/assets/unifiExpress.jpg.mjs +0 -3
- package/lib/components/Explore/assets/unifiExpress.jpg.mjs.map +0 -1
- package/lib/utils/validations.d.ts +0 -15
- package/lib/utils/validations.js +0 -23
- package/lib/utils/validations.js.map +0 -1
- package/lib/utils/validations.mjs +0 -21
- package/lib/utils/validations.mjs.map +0 -1
- /package/lib/assets/{baseStation-mobile.jpg → baseStation--mobile.jpg} +0 -0
- /package/lib/assets/{g4-ptz.jpg → g4Ptz.jpg} +0 -0
- /package/lib/assets/{unifiExpress.jpg → ux.jpg} +0 -0
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var dinero_js = require('dinero.js');
|
|
4
|
+
var errors = require('../errors.js');
|
|
5
|
+
var fixedAmount = require('./allocators/fixedAmount.js');
|
|
6
|
+
var misc = require('./misc.js');
|
|
7
|
+
"use strict";
|
|
8
|
+
const taxRateToDineroAllocation = taxRate => {
|
|
9
|
+
if (taxRate <= 0 || taxRate >= 1) {
|
|
10
|
+
throw new RangeError("Unsupported rate, expected in range of 0>rate<1");
|
|
11
|
+
}
|
|
12
|
+
const rateScale = String(taxRate).split(".")[1]?.length;
|
|
13
|
+
errors.assert(rateScale !== void 0, "rateScale is undefined");
|
|
14
|
+
const rateInt = Math.round(taxRate * Math.pow(10, rateScale));
|
|
15
|
+
return [rateInt, Math.pow(10, rateScale) - rateInt];
|
|
16
|
+
};
|
|
17
|
+
const allocatePerItemTaxableAmount = itemSummary => {
|
|
18
|
+
if (itemSummary.quantity === 1) {
|
|
19
|
+
return [itemSummary];
|
|
20
|
+
}
|
|
21
|
+
const preDiscountAmount = dinero_js.add(misc.getDinero(itemSummary.taxableAmount, itemSummary.currency), misc.getDinero(itemSummary.discountAmount, itemSummary.currency));
|
|
22
|
+
const allocator = new fixedAmount.FixedAmountAllocator({
|
|
23
|
+
initialAllocateAmount: itemSummary.discountAmount,
|
|
24
|
+
initialTotalAmount: misc.amount(preDiscountAmount),
|
|
25
|
+
currency: itemSummary.currency
|
|
26
|
+
});
|
|
27
|
+
return Array.from({
|
|
28
|
+
length: itemSummary.quantity
|
|
29
|
+
}, () => {
|
|
30
|
+
const discountAllocatedAmount = allocator.allocate(itemSummary.itemPrice, itemSummary.currency);
|
|
31
|
+
const taxableAmount = dinero_js.subtract(misc.getDinero(itemSummary.itemPrice, itemSummary.currency), discountAllocatedAmount);
|
|
32
|
+
return {
|
|
33
|
+
itemPrice: itemSummary.itemPrice,
|
|
34
|
+
taxableAmount: misc.amount(taxableAmount),
|
|
35
|
+
discountAmount: misc.amount(discountAllocatedAmount),
|
|
36
|
+
currency: itemSummary.currency,
|
|
37
|
+
quantity: 1
|
|
38
|
+
};
|
|
39
|
+
});
|
|
40
|
+
};
|
|
41
|
+
const calculateWholeQtyLineItemTax = (itemSummary, taxRate) => {
|
|
42
|
+
if (taxRate === 0) {
|
|
43
|
+
return misc.getDinero(0, itemSummary.currency);
|
|
44
|
+
}
|
|
45
|
+
const [taxAmount] = dinero_js.allocate(misc.getDinero(itemSummary.taxableAmount, itemSummary.currency), taxRateToDineroAllocation(taxRate));
|
|
46
|
+
errors.assert(taxAmount !== void 0, "taxAmount is undefined");
|
|
47
|
+
return taxAmount;
|
|
48
|
+
};
|
|
49
|
+
const calculatePerQtyLineItemTax = (itemSummary, taxRate) => {
|
|
50
|
+
if (taxRate === 0) {
|
|
51
|
+
return misc.getDinero(0, itemSummary.currency);
|
|
52
|
+
}
|
|
53
|
+
const singleItemSummaries = allocatePerItemTaxableAmount(itemSummary);
|
|
54
|
+
let totalTaxAmount = misc.getDinero(0, itemSummary.currency);
|
|
55
|
+
singleItemSummaries.forEach(itemSummary2 => {
|
|
56
|
+
const taxAmount = calculateWholeQtyLineItemTax(itemSummary2, taxRate);
|
|
57
|
+
totalTaxAmount = dinero_js.add(totalTaxAmount, taxAmount);
|
|
58
|
+
});
|
|
59
|
+
return totalTaxAmount;
|
|
60
|
+
};
|
|
61
|
+
exports.allocatePerItemTaxableAmount = allocatePerItemTaxableAmount;
|
|
62
|
+
exports.calculatePerQtyLineItemTax = calculatePerQtyLineItemTax;
|
|
63
|
+
exports.calculateWholeQtyLineItemTax = calculateWholeQtyLineItemTax;
|
|
64
|
+
exports.taxRateToDineroAllocation = taxRateToDineroAllocation;
|
|
65
|
+
//# sourceMappingURL=tax.private.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"tax.private.js","sources":["../../../src/utils/money/tax.private.ts"],"sourcesContent":["// #############################################################################\n// Internal helper functions not for consumption outside of store.core!\n// Wrote as separate files so that they can be easily unit tested without exposing them\n// #############################################################################\nimport type { Dinero } from \"dinero.js\";\nimport { add, allocate, subtract } from \"dinero.js\";\n\nimport { assert } from \"../errors\";\nimport { FixedAmountAllocator } from \"./allocators/fixedAmount\";\nimport { amount, getDinero } from \"./misc\";\nimport type {\n Cents,\n LineItemSummary,\n SingleLineItemSummary,\n TaxRate,\n} from \"./types\";\n\n/**\n * Private helper to convert a floating number to a dinero friendly allocation integer pair\n * @see https://v2.dinerojs.com/docs/api/mutations/allocate\n * @example\n * taxRateToDineroAllocation(0.1) // [1, 9]\n * taxRateToDineroAllocation(0.5) // [5, 5]\n */\nexport const taxRateToDineroAllocation = (\n taxRate: TaxRate,\n): [number, number] => {\n if (taxRate <= 0 || taxRate >= 1) {\n throw new RangeError(\"Unsupported rate, expected in range of 0>rate<1\");\n }\n\n // Get the scale of the rate\n const rateScale = String(taxRate).split(\".\")[1]?.length;\n assert(rateScale !== undefined, \"rateScale is undefined\");\n\n // Convert to integers\n const rateInt = Math.round(taxRate * Math.pow(10, rateScale));\n return [rateInt, Math.pow(10, rateScale) - rateInt];\n};\n\n/**\n * Reverse engineer the taxable amount from \"whole item\" taxable amount into \"per item\" taxable amount for custom tax\n * rate calculations\n */\nexport const allocatePerItemTaxableAmount = (\n itemSummary: LineItemSummary,\n): SingleLineItemSummary[] => {\n /**\n * If the item quantity is 1, we can just return the item summary as is\n */\n if (itemSummary.quantity === 1) {\n return [itemSummary as SingleLineItemSummary];\n }\n\n // Reverse engineer price before discount\n const preDiscountAmount = add(\n getDinero(itemSummary.taxableAmount, itemSummary.currency),\n getDinero(itemSummary.discountAmount, itemSummary.currency),\n );\n\n const allocator = new FixedAmountAllocator({\n initialAllocateAmount: itemSummary.discountAmount,\n initialTotalAmount: amount(preDiscountAmount),\n currency: itemSummary.currency,\n });\n\n // Iterate over the amount of qty to reverse engineer the discount price\n return Array.from({ length: itemSummary.quantity }, () => {\n const discountAllocatedAmount = allocator.allocate(\n itemSummary.itemPrice,\n itemSummary.currency,\n );\n\n const taxableAmount = subtract(\n getDinero(itemSummary.itemPrice, itemSummary.currency),\n discountAllocatedAmount,\n );\n\n return {\n itemPrice: itemSummary.itemPrice,\n taxableAmount: amount(taxableAmount),\n discountAmount: amount(discountAllocatedAmount),\n currency: itemSummary.currency,\n quantity: 1,\n } as SingleLineItemSummary;\n });\n};\n\nexport const calculateWholeQtyLineItemTax = (\n itemSummary: LineItemSummary,\n taxRate: TaxRate,\n): Dinero<Cents> => {\n // Nothing to calculate\n if (taxRate === 0) {\n return getDinero(0, itemSummary.currency);\n }\n\n /**\n * FIXME: look into approach with multiply and different scale for control over rounding, currently we just inherit\n * what dinero gives us which cause unexpected rounding issue\n */\n const [taxAmount] = allocate(\n getDinero(itemSummary.taxableAmount, itemSummary.currency),\n taxRateToDineroAllocation(taxRate),\n );\n\n assert(taxAmount !== undefined, \"taxAmount is undefined\");\n return taxAmount;\n};\n\nexport const calculatePerQtyLineItemTax = (\n itemSummary: LineItemSummary,\n taxRate: TaxRate,\n): Dinero<Cents> => {\n // Nothing to calculate\n if (taxRate === 0) {\n return getDinero(0, itemSummary.currency);\n }\n\n // Split the item summary into per individual item qty summaries\n const singleItemSummaries = allocatePerItemTaxableAmount(itemSummary);\n\n // Calculate individual item qty tax and sum them up\n let totalTaxAmount = getDinero(0, itemSummary.currency);\n\n singleItemSummaries.forEach((itemSummary) => {\n // Probably a bit confusing at first, but calling the whole qty tax calculation function here as we can safely use\n // it as item summary is always a single item summary here\n const taxAmount = calculateWholeQtyLineItemTax(itemSummary, taxRate);\n totalTaxAmount = add(totalTaxAmount, taxAmount);\n });\n\n return totalTaxAmount;\n};\n"],"names":["taxRateToDineroAllocation","taxRate","RangeError","rateScale","String","split","length","assert","rateInt","Math","round","pow","allocatePerItemTaxableAmount","itemSummary","quantity","preDiscountAmount","add","getDinero","taxableAmount","currency","discountAmount","allocator","FixedAmountAllocator","initialAllocateAmount","initialTotalAmount","amount","Array","from","discountAllocatedAmount","allocate","itemPrice","subtract","calculateWholeQtyLineItemTax","taxAmount","calculatePerQtyLineItemTax","singleItemSummaries","totalTaxAmount","forEach"],"mappings":";;;;;;;AAwBa,MAAAA,yBAAA,GACXC,OACqB,IAAA;EACjB,IAAAA,OAAA,IAAW,CAAK,IAAAA,OAAA,IAAW,CAAG,EAAA;IAC1B,MAAA,IAAIC,WAAW,iDAAiD,CAAA;EACxE;EAGM,MAAAC,SAAA,GAAYC,OAAOH,OAAO,CAAA,CAAEI,MAAM,GAAG,CAAA,CAAE,CAAC,CAAG,EAAAC,MAAA;EAC1CC,MAAAA,CAAAA,MAAA,CAAAJ,SAAA,KAAc,QAAW,wBAAwB,CAAA;EAGlD,MAAAK,OAAA,GAAUC,KAAKC,KAAM,CAAAT,OAAA,GAAUQ,KAAKE,GAAI,CAAA,EAAA,EAAIR,SAAS,CAAC,CAAA;EAC5D,OAAO,CAACK,OAAS,EAAAC,IAAA,CAAKE,IAAI,EAAI,EAAAR,SAA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AAAgB,cAAA,CAAU,CAAG,EAAAJ,WAAA,CAAYM,QAAQ,CAAA;EAC1C;EAMM,MAAA,CAACc,SAAS,CAAI,GAAAJ,SAAA,CAAAA,QAAA,CAClBZ,IAAAA,CAAAA,SAAU,CAAAJ,WAAA,CAAYK,aAAe,EAAAL,WAAA,CAAYM,QAAQ,CAAA,EACzDnB,0BAA0BC,OAAO,CAAA,CACnC;EAEOM,MAAAA,CAAAA,MAAA,CAAA0B,SAAA,KAAc,QAAW,wBAAwB,CAAA;EACjD,OAAAA,SAAA;AACT,CAAA;AAEa,MAAAC,0BAAA,GAA6BA,CACxCrB,WAAA,EACAZ,OACkB,KAAA;EAElB,IAAIA,YAAY,CAAG,EAAA;IACV,OAAAgB,cAAA,CAAU,CAAG,EAAAJ,WAAA,CAAYM,QAAQ,CAAA;EAC1C;EAGM,MAAAgB,mBAAA,GAAsBvB,6BAA6BC,WAAW,CAAA;EAGpE,IAAIuB,cAAiB,GAAAnB,IAAA,CAAAA,SAAA,CAAU,CAAG,EAAAJ,WAAA,CAAYM,QAAQ,CAAA;EAElCgB,mBAAA,CAAAE,OAAA,CAASxB,YAAgB,IAAA;IAGrC,MAAAoB,SAAA,GAAYD,4BAA6BnB,CAAAA,YAAAA,EAAaZ,OAAO,CAAA;IAClDmC,cAAA,GAAApB,SAAA,CAAAA,GAAA,CAAIoB,gBAAgBH,SAAS,CAAA;EAAA,CAC/C,CAAA;EAEM,OAAAG,cAAA;AACT,CAAA;;;;"}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { add, subtract, allocate } from 'dinero.js';
|
|
2
|
+
import { assert } from '../errors.mjs';
|
|
3
|
+
import { FixedAmountAllocator } from './allocators/fixedAmount.mjs';
|
|
4
|
+
import { getDinero, amount } from './misc.mjs';
|
|
5
|
+
"use strict";
|
|
6
|
+
const taxRateToDineroAllocation = taxRate => {
|
|
7
|
+
if (taxRate <= 0 || taxRate >= 1) {
|
|
8
|
+
throw new RangeError("Unsupported rate, expected in range of 0>rate<1");
|
|
9
|
+
}
|
|
10
|
+
const rateScale = String(taxRate).split(".")[1]?.length;
|
|
11
|
+
assert(rateScale !== void 0, "rateScale is undefined");
|
|
12
|
+
const rateInt = Math.round(taxRate * Math.pow(10, rateScale));
|
|
13
|
+
return [rateInt, Math.pow(10, rateScale) - rateInt];
|
|
14
|
+
};
|
|
15
|
+
const allocatePerItemTaxableAmount = itemSummary => {
|
|
16
|
+
if (itemSummary.quantity === 1) {
|
|
17
|
+
return [itemSummary];
|
|
18
|
+
}
|
|
19
|
+
const preDiscountAmount = add(getDinero(itemSummary.taxableAmount, itemSummary.currency), getDinero(itemSummary.discountAmount, itemSummary.currency));
|
|
20
|
+
const allocator = new FixedAmountAllocator({
|
|
21
|
+
initialAllocateAmount: itemSummary.discountAmount,
|
|
22
|
+
initialTotalAmount: amount(preDiscountAmount),
|
|
23
|
+
currency: itemSummary.currency
|
|
24
|
+
});
|
|
25
|
+
return Array.from({
|
|
26
|
+
length: itemSummary.quantity
|
|
27
|
+
}, () => {
|
|
28
|
+
const discountAllocatedAmount = allocator.allocate(itemSummary.itemPrice, itemSummary.currency);
|
|
29
|
+
const taxableAmount = subtract(getDinero(itemSummary.itemPrice, itemSummary.currency), discountAllocatedAmount);
|
|
30
|
+
return {
|
|
31
|
+
itemPrice: itemSummary.itemPrice,
|
|
32
|
+
taxableAmount: amount(taxableAmount),
|
|
33
|
+
discountAmount: amount(discountAllocatedAmount),
|
|
34
|
+
currency: itemSummary.currency,
|
|
35
|
+
quantity: 1
|
|
36
|
+
};
|
|
37
|
+
});
|
|
38
|
+
};
|
|
39
|
+
const calculateWholeQtyLineItemTax = (itemSummary, taxRate) => {
|
|
40
|
+
if (taxRate === 0) {
|
|
41
|
+
return getDinero(0, itemSummary.currency);
|
|
42
|
+
}
|
|
43
|
+
const [taxAmount] = allocate(getDinero(itemSummary.taxableAmount, itemSummary.currency), taxRateToDineroAllocation(taxRate));
|
|
44
|
+
assert(taxAmount !== void 0, "taxAmount is undefined");
|
|
45
|
+
return taxAmount;
|
|
46
|
+
};
|
|
47
|
+
const calculatePerQtyLineItemTax = (itemSummary, taxRate) => {
|
|
48
|
+
if (taxRate === 0) {
|
|
49
|
+
return getDinero(0, itemSummary.currency);
|
|
50
|
+
}
|
|
51
|
+
const singleItemSummaries = allocatePerItemTaxableAmount(itemSummary);
|
|
52
|
+
let totalTaxAmount = getDinero(0, itemSummary.currency);
|
|
53
|
+
singleItemSummaries.forEach(itemSummary2 => {
|
|
54
|
+
const taxAmount = calculateWholeQtyLineItemTax(itemSummary2, taxRate);
|
|
55
|
+
totalTaxAmount = add(totalTaxAmount, taxAmount);
|
|
56
|
+
});
|
|
57
|
+
return totalTaxAmount;
|
|
58
|
+
};
|
|
59
|
+
export { allocatePerItemTaxableAmount, calculatePerQtyLineItemTax, calculateWholeQtyLineItemTax, taxRateToDineroAllocation };
|
|
60
|
+
//# sourceMappingURL=tax.private.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"tax.private.mjs","sources":["../../../src/utils/money/tax.private.ts"],"sourcesContent":["// #############################################################################\n// Internal helper functions not for consumption outside of store.core!\n// Wrote as separate files so that they can be easily unit tested without exposing them\n// #############################################################################\nimport type { Dinero } from \"dinero.js\";\nimport { add, allocate, subtract } from \"dinero.js\";\n\nimport { assert } from \"../errors\";\nimport { FixedAmountAllocator } from \"./allocators/fixedAmount\";\nimport { amount, getDinero } from \"./misc\";\nimport type {\n Cents,\n LineItemSummary,\n SingleLineItemSummary,\n TaxRate,\n} from \"./types\";\n\n/**\n * Private helper to convert a floating number to a dinero friendly allocation integer pair\n * @see https://v2.dinerojs.com/docs/api/mutations/allocate\n * @example\n * taxRateToDineroAllocation(0.1) // [1, 9]\n * taxRateToDineroAllocation(0.5) // [5, 5]\n */\nexport const taxRateToDineroAllocation = (\n taxRate: TaxRate,\n): [number, number] => {\n if (taxRate <= 0 || taxRate >= 1) {\n throw new RangeError(\"Unsupported rate, expected in range of 0>rate<1\");\n }\n\n // Get the scale of the rate\n const rateScale = String(taxRate).split(\".\")[1]?.length;\n assert(rateScale !== undefined, \"rateScale is undefined\");\n\n // Convert to integers\n const rateInt = Math.round(taxRate * Math.pow(10, rateScale));\n return [rateInt, Math.pow(10, rateScale) - rateInt];\n};\n\n/**\n * Reverse engineer the taxable amount from \"whole item\" taxable amount into \"per item\" taxable amount for custom tax\n * rate calculations\n */\nexport const allocatePerItemTaxableAmount = (\n itemSummary: LineItemSummary,\n): SingleLineItemSummary[] => {\n /**\n * If the item quantity is 1, we can just return the item summary as is\n */\n if (itemSummary.quantity === 1) {\n return [itemSummary as SingleLineItemSummary];\n }\n\n // Reverse engineer price before discount\n const preDiscountAmount = add(\n getDinero(itemSummary.taxableAmount, itemSummary.currency),\n getDinero(itemSummary.discountAmount, itemSummary.currency),\n );\n\n const allocator = new FixedAmountAllocator({\n initialAllocateAmount: itemSummary.discountAmount,\n initialTotalAmount: amount(preDiscountAmount),\n currency: itemSummary.currency,\n });\n\n // Iterate over the amount of qty to reverse engineer the discount price\n return Array.from({ length: itemSummary.quantity }, () => {\n const discountAllocatedAmount = allocator.allocate(\n itemSummary.itemPrice,\n itemSummary.currency,\n );\n\n const taxableAmount = subtract(\n getDinero(itemSummary.itemPrice, itemSummary.currency),\n discountAllocatedAmount,\n );\n\n return {\n itemPrice: itemSummary.itemPrice,\n taxableAmount: amount(taxableAmount),\n discountAmount: amount(discountAllocatedAmount),\n currency: itemSummary.currency,\n quantity: 1,\n } as SingleLineItemSummary;\n });\n};\n\nexport const calculateWholeQtyLineItemTax = (\n itemSummary: LineItemSummary,\n taxRate: TaxRate,\n): Dinero<Cents> => {\n // Nothing to calculate\n if (taxRate === 0) {\n return getDinero(0, itemSummary.currency);\n }\n\n /**\n * FIXME: look into approach with multiply and different scale for control over rounding, currently we just inherit\n * what dinero gives us which cause unexpected rounding issue\n */\n const [taxAmount] = allocate(\n getDinero(itemSummary.taxableAmount, itemSummary.currency),\n taxRateToDineroAllocation(taxRate),\n );\n\n assert(taxAmount !== undefined, \"taxAmount is undefined\");\n return taxAmount;\n};\n\nexport const calculatePerQtyLineItemTax = (\n itemSummary: LineItemSummary,\n taxRate: TaxRate,\n): Dinero<Cents> => {\n // Nothing to calculate\n if (taxRate === 0) {\n return getDinero(0, itemSummary.currency);\n }\n\n // Split the item summary into per individual item qty summaries\n const singleItemSummaries = allocatePerItemTaxableAmount(itemSummary);\n\n // Calculate individual item qty tax and sum them up\n let totalTaxAmount = getDinero(0, itemSummary.currency);\n\n singleItemSummaries.forEach((itemSummary) => {\n // Probably a bit confusing at first, but calling the whole qty tax calculation function here as we can safely use\n // it as item summary is always a single item summary here\n const taxAmount = calculateWholeQtyLineItemTax(itemSummary, taxRate);\n totalTaxAmount = add(totalTaxAmount, taxAmount);\n });\n\n return totalTaxAmount;\n};\n"],"names":["taxRateToDineroAllocation","taxRate","RangeError","rateScale","String","split","length","assert","rateInt","Math","round","pow","allocatePerItemTaxableAmount","itemSummary","quantity","preDiscountAmount","add","getDinero","taxableAmount","currency","discountAmount","allocator","FixedAmountAllocator","initialAllocateAmount","initialTotalAmount","amount","Array","from","discountAllocatedAmount","allocate","itemPrice","subtract","calculateWholeQtyLineItemTax","taxAmount","calculatePerQtyLineItemTax","singleItemSummaries","totalTaxAmount","forEach"],"mappings":";;;;;AAwBa,MAAAA,yBAAA,GACXC,OACqB,IAAA;EACjB,IAAAA,OAAA,IAAW,CAAK,IAAAA,OAAA,IAAW,CAAG,EAAA;IAC1B,MAAA,IAAIC,WAAW,iDAAiD,CAAA;EACxE;EAGM,MAAAC,SAAA,GAAYC,OAAOH,OAAO,CAAA,CAAEI,MAAM,GAAG,CAAA,CAAE,CAAC,CAAG,EAAAC,MAAA;EAC1CC,MAAA,CAAAJ,SAAA,KAAc,QAAW,wBAAwB,CAAA;EAGlD,MAAAK,OAAA,GAAUC,KAAKC,KAAM,CAAAT,OAAA,GAAUQ,KAAKE,GAAI,CAAA,EAAA,EAAIR,SAAS,CAAC,CAAA;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AAgB,SAAA,CAAU,CAAG,EAAAJ,WAAA,CAAYM,QAAQ,CAAA;EAC1C;EAMM,MAAA,CAACc,SAAS,CAAI,GAAAJ,QAAA,CAClBZ,SAAU,CAAAJ,WAAA,CAAYK,aAAe,EAAAL,WAAA,CAAYM,QAAQ,CAAA,EACzDnB,0BAA0BC,OAAO,CAAA,CACnC;EAEOM,MAAA,CAAA0B,SAAA,KAAc,QAAW,wBAAwB,CAAA;EACjD,OAAAA,SAAA;AACT,CAAA;AAEa,MAAAC,0BAAA,GAA6BA,CACxCrB,WAAA,EACAZ,OACkB,KAAA;EAElB,IAAIA,YAAY,CAAG,EAAA;IACV,OAAAgB,SAAA,CAAU,CAAG,EAAAJ,WAAA,CAAYM,QAAQ,CAAA;EAC1C;EAGM,MAAAgB,mBAAA,GAAsBvB,6BAA6BC,WAAW,CAAA;EAGpE,IAAIuB,cAAiB,GAAAnB,SAAA,CAAU,CAAG,EAAAJ,WAAA,CAAYM,QAAQ,CAAA;EAElCgB,mBAAA,CAAAE,OAAA,CAASxB,YAAgB,IAAA;IAGrC,MAAAoB,SAAA,GAAYD,4BAA6BnB,CAAAA,YAAAA,EAAaZ,OAAO,CAAA;IAClDmC,cAAA,GAAApB,GAAA,CAAIoB,gBAAgBH,SAAS,CAAA;EAAA,CAC/C,CAAA;EAEM,OAAAG,cAAA;AACT,CAAA;"}
|
|
@@ -1,10 +1,85 @@
|
|
|
1
|
-
import type { Currency as DineroCurrency } from "dinero.js";
|
|
1
|
+
import type { Dinero, Currency as DineroCurrency } from "dinero.js";
|
|
2
|
+
import type { Currency } from "../../types";
|
|
3
|
+
/**
|
|
4
|
+
* Floating point number of item tax rate
|
|
5
|
+
* Number in range of 0>rate<1 (non-inclusive boundaries)
|
|
6
|
+
* @example
|
|
7
|
+
* 0.09 // 9%
|
|
8
|
+
*/
|
|
9
|
+
export type TaxRate = number;
|
|
10
|
+
/**
|
|
11
|
+
* Shorthand for Dinero.js instance with number as the amount
|
|
12
|
+
*/
|
|
13
|
+
export type Dino = Dinero<number>;
|
|
14
|
+
/**
|
|
15
|
+
* Shorthand for Dinero.js currency instance representing either a currency or custom currency such as tax-rate or shipping
|
|
16
|
+
*/
|
|
17
|
+
export type DinoCurrency = DineroCurrency<number>;
|
|
18
|
+
/**
|
|
19
|
+
* Cents representation of specific currency
|
|
20
|
+
* As a helper to easily understand the code, we use `Cents` as a type alias for `number` as historically we have had
|
|
21
|
+
* mistakes by the team where we have used as dollar instead of `Cents` and vice versa.
|
|
22
|
+
* @example
|
|
23
|
+
* 100 // in USD this is $1.00
|
|
24
|
+
* 100 // in YEN this is ¥100
|
|
25
|
+
*/
|
|
26
|
+
export type Cents = number;
|
|
27
|
+
/**
|
|
28
|
+
* Type definition for a rounding function that can be used to round fractions
|
|
29
|
+
*/
|
|
30
|
+
export type RoundingFn = (float: number) => Cents;
|
|
31
|
+
/**
|
|
32
|
+
* Convenient way to represent a money on the API level
|
|
33
|
+
* When working with dinero you typically will convert this to a Dinero instance
|
|
34
|
+
*/
|
|
35
|
+
export interface Money {
|
|
36
|
+
amount: Cents;
|
|
37
|
+
currency: Currency;
|
|
38
|
+
}
|
|
2
39
|
/**
|
|
3
40
|
* Attempt to type out some locales `language-TERRITORY` which "we" as store care about
|
|
4
41
|
* @see https://unicode-org.github.io/cldr-staging/charts/38/supplemental/territory_language_information.html
|
|
5
42
|
*/
|
|
6
43
|
export type Locale = "ar-AE" | "ar-SA" | "bg-BG" | "bn-BD" | "bn-IN" | "cs-CZ" | "da-DK" | "de-AT" | "de-CH" | "de-DE" | "el-GR" | "en-AU" | "en-BR" | "en-CA" | "en-GB" | "en-IE" | "en-IN" | "en-JP" | "en-NZ" | "en-SG" | "en-US" | "en-ZA" | "es-AR" | "es-CL" | "es-CO" | "es-ES" | "es-MX" | "es-US" | "fi-FI" | "fr-BE" | "fr-CA" | "fr-CH" | "fr-FR" | "he-IL" | "hi-IN" | "hr-HR" | "hu-HU" | "id-ID" | "it-CH" | "it-IT" | "ja-JP" | "ko-KR" | "nl-BE" | "nl-NL" | "no-NO" | "pl-PL" | "pt-BR" | "pt-PT" | "ro-RO" | "ru-RU" | "sk-SK" | "sv-SE" | "ta-IN" | "ta-LK" | "th-TH" | "tr-TR" | "zh-CN" | "zh-HK" | "zh-TW" | string;
|
|
7
|
-
export type
|
|
8
|
-
currency:
|
|
44
|
+
export type LocalizedCurrency = {
|
|
45
|
+
currency: DinoCurrency;
|
|
9
46
|
locale: Locale;
|
|
10
47
|
};
|
|
48
|
+
export interface LineItemSummary {
|
|
49
|
+
itemPrice: Cents;
|
|
50
|
+
/**
|
|
51
|
+
* Item quantity that is always bigger than zero!
|
|
52
|
+
*/
|
|
53
|
+
quantity: number;
|
|
54
|
+
currency: Currency;
|
|
55
|
+
/**
|
|
56
|
+
* Total discount amount (only) applied for the given item quantity.
|
|
57
|
+
* No taxes are included. Calculated by `total_raw_price * <discount-allocation-percentage>`
|
|
58
|
+
*/
|
|
59
|
+
discountAmount: Cents;
|
|
60
|
+
/**
|
|
61
|
+
* subtotal - discountAmount
|
|
62
|
+
*/
|
|
63
|
+
taxableAmount: Cents;
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Pretty much the same as PreTaxItemSummary but with quantity always set to 1 (for clearer type hinting and reasoning
|
|
67
|
+
* around the topic)
|
|
68
|
+
*/
|
|
69
|
+
export interface SingleLineItemSummary extends LineItemSummary {
|
|
70
|
+
quantity: 1;
|
|
71
|
+
}
|
|
72
|
+
export interface FullLineItemSummary extends LineItemSummary {
|
|
73
|
+
/**
|
|
74
|
+
* Total price for the given item quantity. Calculated by `(itemPrice * quantity)`
|
|
75
|
+
*/
|
|
76
|
+
subtotal: Cents;
|
|
77
|
+
/**
|
|
78
|
+
* Total tax amount for the given item quantity.
|
|
79
|
+
*/
|
|
80
|
+
tax: Cents;
|
|
81
|
+
/**
|
|
82
|
+
* Total price for the given item quantity. Calculated by `subtotal - discountAmount + tax`
|
|
83
|
+
*/
|
|
84
|
+
total: Cents;
|
|
85
|
+
}
|
package/lib/utils/number.d.ts
CHANGED
|
@@ -31,7 +31,7 @@ export declare function isFloat(value: unknown, entity?: string, ErrorClass?: ne
|
|
|
31
31
|
* toFixed(1.2345, 0) // => "1"
|
|
32
32
|
* toFixed(1.23, 4) // => "1.2300"
|
|
33
33
|
*/
|
|
34
|
-
export declare function toFixedRate(rate: number, scale: number): string;
|
|
34
|
+
export declare function toFixedRate(rate: number, scale: number, asNumber?: boolean): string;
|
|
35
35
|
export declare function toFixedRate(rate: number, scale: number, asNumber: true): number;
|
|
36
36
|
/**
|
|
37
37
|
* Util to convert a float number to a string. Using this rather than simple template string to have a consistent way of
|
package/lib/utils/number.js
CHANGED
|
@@ -21,7 +21,7 @@ function isInt(value, entity = "Value", ErrorClass = TypeError) {
|
|
|
21
21
|
function isFloat(value, entity = "Value", ErrorClass = TypeError) {
|
|
22
22
|
isFiniteNumber(value, entity, ErrorClass);
|
|
23
23
|
}
|
|
24
|
-
function toFixedRate(rate, scale
|
|
24
|
+
function toFixedRate(rate, scale, asNumber) {
|
|
25
25
|
isFloat(rate, "Rate");
|
|
26
26
|
const fixedRate = rate.toFixed(scale);
|
|
27
27
|
return asNumber ? +fixedRate : fixedRate;
|
package/lib/utils/number.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"number.js","sources":["../../src/utils/number.ts"],"sourcesContent":["/**\n * Util to easily parse that any given input is a finite number (not NaN, Infinity, -Infinity, etc.)\n * @throws If given input is not a finite number\n */\nexport function isFiniteNumber(\n value: unknown,\n entity = \"Value\",\n ErrorClass: new (message: string) => void = TypeError,\n): asserts value is number {\n if (typeof value !== \"number\" || Number.isNaN(value)) {\n // eslint-disable-next-line @typescript-eslint/no-throw-literal\n throw new ErrorClass(`${entity} is not a number: ${value}`);\n }\n\n if (!Number.isFinite(value)) {\n // eslint-disable-next-line @typescript-eslint/no-throw-literal\n throw new ErrorClass(`${entity} is not a finite number: ${value}`);\n }\n}\n\n/**\n * Custom isInt validation function that returns a more fine-grained error message otherwise for type safety alone a\n * Number.isSafeInteger would be sufficient.\n *\n * Code inspired from graphql-scalars library.\n * @see\n * https://github.com/Urigo/graphql-scalars/blob/c4fa3d27b965e39ceda6c33fc18041f112ecce49/src/scalars/utilities.ts#L20-L32\n * @throws If given input is not a valid integer\n */\nexport function isInt(\n value: unknown,\n entity = \"Value\",\n ErrorClass: new (message: string) => void = TypeError,\n): asserts value is number {\n isFiniteNumber(value, entity, ErrorClass);\n\n if (!Number.isInteger(value)) {\n // eslint-disable-next-line @typescript-eslint/no-throw-literal\n throw new ErrorClass(`${entity} is not an integer: ${value}`);\n }\n\n if (!Number.isSafeInteger(value)) {\n // eslint-disable-next-line @typescript-eslint/no-throw-literal\n throw new ErrorClass(`${entity} is not a safe integer: ${value}`);\n }\n}\n\n/**\n * Custom isFloat validation function that returns a more fine-grained error message otherwise for type safety alone a\n * Number.parseFloat would be sufficient.\n *\n * Code inspired from graphql-scalars library.\n * @see\n * https://github.com/Urigo/graphql-scalars/blob/c4fa3d27b965e39ceda6c33fc18041f112ecce49/src/scalars/utilities.ts#L34-L38\n * @throws If given input is not a valid float\n */\nexport function isFloat(\n value: unknown,\n entity = \"Value\",\n ErrorClass: new (message: string) => void = TypeError,\n): asserts value is number {\n isFiniteNumber(value, entity, ErrorClass);\n}\n\n/**\n * Returns a string representing a number in fixed-point notation.\n * Overload: Returns the rate parsed as a number\n * @example\n * toFixed(1.2345, 2) // => \"1.23\"\n * toFixed(1.2345, 0) // => \"1\"\n * toFixed(1.23, 4) // => \"1.2300\"\n */\nexport function toFixedRate(rate: number
|
|
1
|
+
{"version":3,"file":"number.js","sources":["../../src/utils/number.ts"],"sourcesContent":["/**\n * Util to easily parse that any given input is a finite number (not NaN, Infinity, -Infinity, etc.)\n * @throws If given input is not a finite number\n */\nexport function isFiniteNumber(\n value: unknown,\n entity = \"Value\",\n ErrorClass: new (message: string) => void = TypeError,\n): asserts value is number {\n if (typeof value !== \"number\" || Number.isNaN(value)) {\n // eslint-disable-next-line @typescript-eslint/no-throw-literal\n throw new ErrorClass(`${entity} is not a number: ${value}`);\n }\n\n if (!Number.isFinite(value)) {\n // eslint-disable-next-line @typescript-eslint/no-throw-literal\n throw new ErrorClass(`${entity} is not a finite number: ${value}`);\n }\n}\n\n/**\n * Custom isInt validation function that returns a more fine-grained error message otherwise for type safety alone a\n * Number.isSafeInteger would be sufficient.\n *\n * Code inspired from graphql-scalars library.\n * @see\n * https://github.com/Urigo/graphql-scalars/blob/c4fa3d27b965e39ceda6c33fc18041f112ecce49/src/scalars/utilities.ts#L20-L32\n * @throws If given input is not a valid integer\n */\nexport function isInt(\n value: unknown,\n entity = \"Value\",\n ErrorClass: new (message: string) => void = TypeError,\n): asserts value is number {\n isFiniteNumber(value, entity, ErrorClass);\n\n if (!Number.isInteger(value)) {\n // eslint-disable-next-line @typescript-eslint/no-throw-literal\n throw new ErrorClass(`${entity} is not an integer: ${value}`);\n }\n\n if (!Number.isSafeInteger(value)) {\n // eslint-disable-next-line @typescript-eslint/no-throw-literal\n throw new ErrorClass(`${entity} is not a safe integer: ${value}`);\n }\n}\n\n/**\n * Custom isFloat validation function that returns a more fine-grained error message otherwise for type safety alone a\n * Number.parseFloat would be sufficient.\n *\n * Code inspired from graphql-scalars library.\n * @see\n * https://github.com/Urigo/graphql-scalars/blob/c4fa3d27b965e39ceda6c33fc18041f112ecce49/src/scalars/utilities.ts#L34-L38\n * @throws If given input is not a valid float\n */\nexport function isFloat(\n value: unknown,\n entity = \"Value\",\n ErrorClass: new (message: string) => void = TypeError,\n): asserts value is number {\n isFiniteNumber(value, entity, ErrorClass);\n}\n\n/**\n * Returns a string representing a number in fixed-point notation.\n * Overload: Returns the rate parsed as a number\n * @example\n * toFixed(1.2345, 2) // => \"1.23\"\n * toFixed(1.2345, 0) // => \"1\"\n * toFixed(1.23, 4) // => \"1.2300\"\n */\nexport function toFixedRate(\n rate: number,\n scale: number,\n asNumber?: boolean,\n): string;\nexport function toFixedRate(\n rate: number,\n scale: number,\n asNumber: true,\n): number;\nexport function toFixedRate(rate: number, scale: number, asNumber?: boolean) {\n // Verify rate\n isFloat(rate, \"Rate\");\n\n const fixedRate = rate.toFixed(scale);\n\n return asNumber ? +fixedRate : fixedRate;\n}\n\n/**\n * Util to convert a float number to a string. Using this rather than simple template string to have a consistent way of\n * doing it. Removes any redundant trailing zeros.\n * @example\n * numberToString(1.23) // => \"1.23\"\n * numberToString(10.00) // => \"10\"\n * @throws If given input is not a valid number\n */\nexport const numberToString = (num: number): string => {\n isFloat(num);\n return num.toString(10);\n};\n\n/**\n * @see https://stackoverflow.com/a/35000557\n */\nexport const findClosestNumber = <T extends number>(\n haystack: T[],\n needle: number,\n): T => {\n return haystack.reduce((prev, curr) =>\n Math.abs(curr - needle) < Math.abs(prev - needle) ? curr : prev,\n );\n};\n\n/**\n * Splits number into array by max value provided.\n * E.g. splitNumberBy(100, 30) => [30, 30, 30, 10]\n */\nexport const splitNumberBy = (value: number, by: number) => {\n const chunks = new Array(Math.floor(value / by)).fill(by);\n const remainder = value % by;\n\n if (remainder > 0) {\n chunks.push(remainder);\n }\n\n return chunks;\n};\n\n/**\n * Clamp value to a given range\n */\nexport const clamp = (val: number, min: number, max: number) => {\n const trueMin = Math.min(min, max);\n const trueMax = Math.max(min, max);\n\n return Math.min(trueMax, Math.max(trueMin, val));\n};\n\n/**\n * Interpolate a number to a new range\n *\n * @example\n * linearInterpolate(5, [0, 10], [0, 100]); // 50\n */\nexport const linearInterpolate = (\n inputY: number,\n [fromMin, fromMax]: [number, number],\n [toMin, toMax]: [number, number],\n clampResult = true,\n) => {\n const percent = (inputY - fromMin) / (fromMax - fromMin);\n const outputX = percent * (toMax - toMin) + toMin;\n\n return clampResult ? clamp(outputX, toMin, toMax) : outputX;\n};\n\n/**\n * Increase value and start over once the range is overflown\n */\nexport const incrementLoopValue = (\n value: number,\n [min, max]: [number, number],\n) => {\n if (value === max) {\n return min;\n }\n return value + 1;\n};\n"],"names":["isFiniteNumber","value","entity","ErrorClass","TypeError","Number","isNaN","isFinite","isInt","isInteger","isSafeInteger","isFloat","toFixedRate","rate","scale","asNumber","fixedRate","toFixed","numberToString","num","toString","findClosestNumber","haystack","needle","reduce","prev","curr","Math","abs","splitNumberBy","by","chunks","Array","floor","fill","remainder","push","clamp","val","min","max","trueMin","trueMax","linearInterpolate","inputY","fromMin","fromMax","toMin","toMax","clampResult","percent","outputX","incrementLoopValue"],"mappings":";;;AAIO,SAASA,cACdA,CAAAC,KAAA,EACAC,MAAS,GAAA,OAAA,EACTC,aAA4CC,SACnB,EAAA;EACzB,IAAI,OAAOH,KAAU,KAAA,QAAA,IAAYI,MAAO,CAAAC,KAAA,CAAML,KAAK,CAAG,EAAA;IAEpD,MAAM,IAAIE,UAAW,CAAA,GAAGD,MAAM,qBAAqBD,KAAO,EAAA,CAAA;EAC5D;EAEA,IAAI,CAACI,MAAA,CAAOE,QAAS,CAAAN,KAAK,CAAG,EAAA;IAE3B,MAAM,IAAIE,UAAW,CAAA,GAAGD,MAAM,4BAA4BD,KAAO,EAAA,CAAA;EACnE;AACF;AAWO,SAASO,KACdA,CAAAP,KAAA,EACAC,MAAS,GAAA,OAAA,EACTC,aAA4CC,SACnB,EAAA;EACVJ,cAAA,CAAAC,KAAA,EAAOC,QAAQC,UAAU,CAAA;EAExC,IAAI,CAACE,MAAA,CAAOI,SAAU,CAAAR,KAAK,CAAG,EAAA;IAE5B,MAAM,IAAIE,UAAW,CAAA,GAAGD,MAAM,uBAAuBD,KAAO,EAAA,CAAA;EAC9D;EAEA,IAAI,CAACI,MAAA,CAAOK,aAAc,CAAAT,KAAK,CAAG,EAAA;IAEhC,MAAM,IAAIE,UAAW,CAAA,GAAGD,MAAM,2BAA2BD,KAAO,EAAA,CAAA;EAClE;AACF;AAWO,SAASU,OACdA,CAAAV,KAAA,EACAC,MAAS,GAAA,OAAA,EACTC,aAA4CC,SACnB,EAAA;EACVJ,cAAA,CAAAC,KAAA,EAAOC,QAAQC,UAAU,CAAA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EAEjC,OAAOb,KAAKY,GAAI,CAAAG,OAAA,EAASf,KAAKa,GAAI,CAAAC,OAAA,EAASH,GAAG,CAAC,CAAA;AACjD,CAAA;AAQO,MAAMK,iBAAoB,GAAAA,CAC/BC,MACA,EAAA,CAACC,OAAS,EAAAC,OAAO,CACjB,EAAA,CAACC,KAAO,EAAAC,KAAK,CACb,EAAAC,WAAA,GAAc,IACX,KAAA;EACG,MAAAC,OAAA,GAAA,CAAWN,MAAS,GAAAC,OAAA,KAAYC,OAAU,GAAAD,OAAA,CAAA;EAC1C,MAAAM,OAAA,GAAUD,OAAW,IAAAF,KAAA,GAAQD,KAAS,CAAA,GAAAA,KAAA;EAE5C,OAAOE,WAAc,GAAAZ,KAAA,CAAMc,OAAS,EAAAJ,KAAA,EAAOC,KAAK,CAAI,GAAAG,OAAA;AACtD,CAAA;AAKO,MAAMC,qBAAqBA,CAChCnD,KAAA,EACA,CAACsC,GAAA,EAAKC,GAAG,CACN,KAAA;EACH,IAAIvC,UAAUuC,GAAK,EAAA;IACV,OAAAD,GAAA;EACT;EACA,OAAOtC,KAAQ,GAAA,CAAA;AACjB,CAAA;;;;;;;;;;"}
|
package/lib/utils/number.mjs
CHANGED
|
@@ -20,7 +20,7 @@ function isInt(value, entity = "Value", ErrorClass = TypeError) {
|
|
|
20
20
|
function isFloat(value, entity = "Value", ErrorClass = TypeError) {
|
|
21
21
|
isFiniteNumber(value, entity, ErrorClass);
|
|
22
22
|
}
|
|
23
|
-
function toFixedRate(rate, scale
|
|
23
|
+
function toFixedRate(rate, scale, asNumber) {
|
|
24
24
|
isFloat(rate, "Rate");
|
|
25
25
|
const fixedRate = rate.toFixed(scale);
|
|
26
26
|
return asNumber ? +fixedRate : fixedRate;
|
package/lib/utils/number.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"number.mjs","sources":["../../src/utils/number.ts"],"sourcesContent":["/**\n * Util to easily parse that any given input is a finite number (not NaN, Infinity, -Infinity, etc.)\n * @throws If given input is not a finite number\n */\nexport function isFiniteNumber(\n value: unknown,\n entity = \"Value\",\n ErrorClass: new (message: string) => void = TypeError,\n): asserts value is number {\n if (typeof value !== \"number\" || Number.isNaN(value)) {\n // eslint-disable-next-line @typescript-eslint/no-throw-literal\n throw new ErrorClass(`${entity} is not a number: ${value}`);\n }\n\n if (!Number.isFinite(value)) {\n // eslint-disable-next-line @typescript-eslint/no-throw-literal\n throw new ErrorClass(`${entity} is not a finite number: ${value}`);\n }\n}\n\n/**\n * Custom isInt validation function that returns a more fine-grained error message otherwise for type safety alone a\n * Number.isSafeInteger would be sufficient.\n *\n * Code inspired from graphql-scalars library.\n * @see\n * https://github.com/Urigo/graphql-scalars/blob/c4fa3d27b965e39ceda6c33fc18041f112ecce49/src/scalars/utilities.ts#L20-L32\n * @throws If given input is not a valid integer\n */\nexport function isInt(\n value: unknown,\n entity = \"Value\",\n ErrorClass: new (message: string) => void = TypeError,\n): asserts value is number {\n isFiniteNumber(value, entity, ErrorClass);\n\n if (!Number.isInteger(value)) {\n // eslint-disable-next-line @typescript-eslint/no-throw-literal\n throw new ErrorClass(`${entity} is not an integer: ${value}`);\n }\n\n if (!Number.isSafeInteger(value)) {\n // eslint-disable-next-line @typescript-eslint/no-throw-literal\n throw new ErrorClass(`${entity} is not a safe integer: ${value}`);\n }\n}\n\n/**\n * Custom isFloat validation function that returns a more fine-grained error message otherwise for type safety alone a\n * Number.parseFloat would be sufficient.\n *\n * Code inspired from graphql-scalars library.\n * @see\n * https://github.com/Urigo/graphql-scalars/blob/c4fa3d27b965e39ceda6c33fc18041f112ecce49/src/scalars/utilities.ts#L34-L38\n * @throws If given input is not a valid float\n */\nexport function isFloat(\n value: unknown,\n entity = \"Value\",\n ErrorClass: new (message: string) => void = TypeError,\n): asserts value is number {\n isFiniteNumber(value, entity, ErrorClass);\n}\n\n/**\n * Returns a string representing a number in fixed-point notation.\n * Overload: Returns the rate parsed as a number\n * @example\n * toFixed(1.2345, 2) // => \"1.23\"\n * toFixed(1.2345, 0) // => \"1\"\n * toFixed(1.23, 4) // => \"1.2300\"\n */\nexport function toFixedRate(rate: number
|
|
1
|
+
{"version":3,"file":"number.mjs","sources":["../../src/utils/number.ts"],"sourcesContent":["/**\n * Util to easily parse that any given input is a finite number (not NaN, Infinity, -Infinity, etc.)\n * @throws If given input is not a finite number\n */\nexport function isFiniteNumber(\n value: unknown,\n entity = \"Value\",\n ErrorClass: new (message: string) => void = TypeError,\n): asserts value is number {\n if (typeof value !== \"number\" || Number.isNaN(value)) {\n // eslint-disable-next-line @typescript-eslint/no-throw-literal\n throw new ErrorClass(`${entity} is not a number: ${value}`);\n }\n\n if (!Number.isFinite(value)) {\n // eslint-disable-next-line @typescript-eslint/no-throw-literal\n throw new ErrorClass(`${entity} is not a finite number: ${value}`);\n }\n}\n\n/**\n * Custom isInt validation function that returns a more fine-grained error message otherwise for type safety alone a\n * Number.isSafeInteger would be sufficient.\n *\n * Code inspired from graphql-scalars library.\n * @see\n * https://github.com/Urigo/graphql-scalars/blob/c4fa3d27b965e39ceda6c33fc18041f112ecce49/src/scalars/utilities.ts#L20-L32\n * @throws If given input is not a valid integer\n */\nexport function isInt(\n value: unknown,\n entity = \"Value\",\n ErrorClass: new (message: string) => void = TypeError,\n): asserts value is number {\n isFiniteNumber(value, entity, ErrorClass);\n\n if (!Number.isInteger(value)) {\n // eslint-disable-next-line @typescript-eslint/no-throw-literal\n throw new ErrorClass(`${entity} is not an integer: ${value}`);\n }\n\n if (!Number.isSafeInteger(value)) {\n // eslint-disable-next-line @typescript-eslint/no-throw-literal\n throw new ErrorClass(`${entity} is not a safe integer: ${value}`);\n }\n}\n\n/**\n * Custom isFloat validation function that returns a more fine-grained error message otherwise for type safety alone a\n * Number.parseFloat would be sufficient.\n *\n * Code inspired from graphql-scalars library.\n * @see\n * https://github.com/Urigo/graphql-scalars/blob/c4fa3d27b965e39ceda6c33fc18041f112ecce49/src/scalars/utilities.ts#L34-L38\n * @throws If given input is not a valid float\n */\nexport function isFloat(\n value: unknown,\n entity = \"Value\",\n ErrorClass: new (message: string) => void = TypeError,\n): asserts value is number {\n isFiniteNumber(value, entity, ErrorClass);\n}\n\n/**\n * Returns a string representing a number in fixed-point notation.\n * Overload: Returns the rate parsed as a number\n * @example\n * toFixed(1.2345, 2) // => \"1.23\"\n * toFixed(1.2345, 0) // => \"1\"\n * toFixed(1.23, 4) // => \"1.2300\"\n */\nexport function toFixedRate(\n rate: number,\n scale: number,\n asNumber?: boolean,\n): string;\nexport function toFixedRate(\n rate: number,\n scale: number,\n asNumber: true,\n): number;\nexport function toFixedRate(rate: number, scale: number, asNumber?: boolean) {\n // Verify rate\n isFloat(rate, \"Rate\");\n\n const fixedRate = rate.toFixed(scale);\n\n return asNumber ? +fixedRate : fixedRate;\n}\n\n/**\n * Util to convert a float number to a string. Using this rather than simple template string to have a consistent way of\n * doing it. Removes any redundant trailing zeros.\n * @example\n * numberToString(1.23) // => \"1.23\"\n * numberToString(10.00) // => \"10\"\n * @throws If given input is not a valid number\n */\nexport const numberToString = (num: number): string => {\n isFloat(num);\n return num.toString(10);\n};\n\n/**\n * @see https://stackoverflow.com/a/35000557\n */\nexport const findClosestNumber = <T extends number>(\n haystack: T[],\n needle: number,\n): T => {\n return haystack.reduce((prev, curr) =>\n Math.abs(curr - needle) < Math.abs(prev - needle) ? curr : prev,\n );\n};\n\n/**\n * Splits number into array by max value provided.\n * E.g. splitNumberBy(100, 30) => [30, 30, 30, 10]\n */\nexport const splitNumberBy = (value: number, by: number) => {\n const chunks = new Array(Math.floor(value / by)).fill(by);\n const remainder = value % by;\n\n if (remainder > 0) {\n chunks.push(remainder);\n }\n\n return chunks;\n};\n\n/**\n * Clamp value to a given range\n */\nexport const clamp = (val: number, min: number, max: number) => {\n const trueMin = Math.min(min, max);\n const trueMax = Math.max(min, max);\n\n return Math.min(trueMax, Math.max(trueMin, val));\n};\n\n/**\n * Interpolate a number to a new range\n *\n * @example\n * linearInterpolate(5, [0, 10], [0, 100]); // 50\n */\nexport const linearInterpolate = (\n inputY: number,\n [fromMin, fromMax]: [number, number],\n [toMin, toMax]: [number, number],\n clampResult = true,\n) => {\n const percent = (inputY - fromMin) / (fromMax - fromMin);\n const outputX = percent * (toMax - toMin) + toMin;\n\n return clampResult ? clamp(outputX, toMin, toMax) : outputX;\n};\n\n/**\n * Increase value and start over once the range is overflown\n */\nexport const incrementLoopValue = (\n value: number,\n [min, max]: [number, number],\n) => {\n if (value === max) {\n return min;\n }\n return value + 1;\n};\n"],"names":["isFiniteNumber","value","entity","ErrorClass","TypeError","Number","isNaN","isFinite","isInt","isInteger","isSafeInteger","isFloat","toFixedRate","rate","scale","asNumber","fixedRate","toFixed","numberToString","num","toString","findClosestNumber","haystack","needle","reduce","prev","curr","Math","abs","splitNumberBy","by","chunks","Array","floor","fill","remainder","push","clamp","val","min","max","trueMin","trueMax","linearInterpolate","inputY","fromMin","fromMax","toMin","toMax","clampResult","percent","outputX","incrementLoopValue"],"mappings":";;AAIO,SAASA,cACdA,CAAAC,KAAA,EACAC,MAAS,GAAA,OAAA,EACTC,aAA4CC,SACnB,EAAA;EACzB,IAAI,OAAOH,KAAU,KAAA,QAAA,IAAYI,MAAO,CAAAC,KAAA,CAAML,KAAK,CAAG,EAAA;IAEpD,MAAM,IAAIE,UAAW,CAAA,GAAGD,MAAM,qBAAqBD,KAAO,EAAA,CAAA;EAC5D;EAEA,IAAI,CAACI,MAAA,CAAOE,QAAS,CAAAN,KAAK,CAAG,EAAA;IAE3B,MAAM,IAAIE,UAAW,CAAA,GAAGD,MAAM,4BAA4BD,KAAO,EAAA,CAAA;EACnE;AACF;AAWO,SAASO,KACdA,CAAAP,KAAA,EACAC,MAAS,GAAA,OAAA,EACTC,aAA4CC,SACnB,EAAA;EACVJ,cAAA,CAAAC,KAAA,EAAOC,QAAQC,UAAU,CAAA;EAExC,IAAI,CAACE,MAAA,CAAOI,SAAU,CAAAR,KAAK,CAAG,EAAA;IAE5B,MAAM,IAAIE,UAAW,CAAA,G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,YAAY,CAAG,EAAA;IACjBJ,MAAA,CAAOK,KAAKD,SAAS,CAAA;EACvB;EAEO,OAAAJ,MAAA;AACT,CAAA;AAKO,MAAMM,KAAQ,GAAAA,CAACC,GAAa,EAAAC,GAAA,EAAaC,GAAgB,KAAA;EAC9D,MAAMC,OAAU,GAAAd,IAAA,CAAKY,GAAI,CAAAA,GAAA,EAAKC,GAAG,CAAA;EACjC,MAAME,OAAU,GAAAf,IAAA,CAAKa,GAAI,CAAAD,GAAA,EAAKC,GAAG,CAAA;EAEjC,OAAOb,KAAKY,GAAI,CAAAG,OAAA,EAASf,KAAKa,GAAI,CAAAC,OAAA,EAASH,GAAG,CAAC,CAAA;AACjD,CAAA;AAQO,MAAMK,iBAAoB,GAAAA,CAC/BC,MACA,EAAA,CAACC,OAAS,EAAAC,OAAO,CACjB,EAAA,CAACC,KAAO,EAAAC,KAAK,CACb,EAAAC,WAAA,GAAc,IACX,KAAA;EACG,MAAAC,OAAA,GAAA,CAAWN,MAAS,GAAAC,OAAA,KAAYC,OAAU,GAAAD,OAAA,CAAA;EAC1C,MAAAM,OAAA,GAAUD,OAAW,IAAAF,KAAA,GAAQD,KAAS,CAAA,GAAAA,KAAA;EAE5C,OAAOE,WAAc,GAAAZ,KAAA,CAAMc,OAAS,EAAAJ,KAAA,EAAOC,KAAK,CAAI,GAAAG,OAAA;AACtD,CAAA;AAKO,MAAMC,qBAAqBA,CAChCnD,KAAA,EACA,CAACsC,GAAA,EAAKC,GAAG,CACN,KAAA;EACH,IAAIvC,UAAUuC,GAAK,EAAA;IACV,OAAAD,GAAA;EACT;EACA,OAAOtC,KAAQ,GAAA,CAAA;AACjB,CAAA;"}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import type { Nullish, StorefrontProduct, StoreId, UnionToAllPossibleTuples } from "../types";
|
|
2
|
+
import { DisplayProductStatus } from "../types";
|
|
3
|
+
/**
|
|
4
|
+
* Narrow MinimalProProductFragment type even more to the union of fields that are needed to sort product
|
|
5
|
+
*/
|
|
6
|
+
type ProductMinFieldsForSorting = Pick<StorefrontProduct, "status" | "tags" | "minDisplayPrice"> & {
|
|
7
|
+
variants: {
|
|
8
|
+
availableInOtherStores: Array<{
|
|
9
|
+
__typename: string;
|
|
10
|
+
}>;
|
|
11
|
+
}[];
|
|
12
|
+
linkedProducts?: {
|
|
13
|
+
status: StorefrontProduct["status"];
|
|
14
|
+
}[];
|
|
15
|
+
};
|
|
16
|
+
export declare enum Sort {
|
|
17
|
+
HighestPrice = "highest-price",
|
|
18
|
+
LowestPrice = "lowest-price",
|
|
19
|
+
Availability = "availability"
|
|
20
|
+
}
|
|
21
|
+
export declare enum SortOrder {
|
|
22
|
+
Asc = "Asc",
|
|
23
|
+
Desc = "Desc"
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Variants statuses sorted in ascending order from least preferable to most, so if some new status comes in, then the
|
|
27
|
+
* lookup for `.indexOf()` would return `-1`, thus being "worse" then DisplayProductStatus.SoldOut and moved to the very
|
|
28
|
+
* back of list
|
|
29
|
+
*/
|
|
30
|
+
export declare const sortedStatusesByPriorityAsc: UnionToAllPossibleTuples<DisplayProductStatus>;
|
|
31
|
+
export declare const sortByAvailabilityDesc: (current: ProductMinFieldsForSorting, next: ProductMinFieldsForSorting) => number;
|
|
32
|
+
export declare const sortByPrice: (order: SortOrder) => (current: ProductMinFieldsForSorting, next: ProductMinFieldsForSorting) => number;
|
|
33
|
+
/**
|
|
34
|
+
* Sorts products based on the given sort type.
|
|
35
|
+
* @param currentSort - sort type
|
|
36
|
+
* @param sortByWeightTag - if false, then sort by weight tag is
|
|
37
|
+
* ignored (e.g. for `Related Add-ons` where
|
|
38
|
+
* we don't want to sort by weight tag)
|
|
39
|
+
*/
|
|
40
|
+
export declare const sortProducts: (currentSort: Nullish<string | Sort>, storeId?: StoreId, sortByWeightTag?: boolean) => (current: ProductMinFieldsForSorting, next: ProductMinFieldsForSorting) => number;
|
|
41
|
+
export {};
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
require('../types/index.js');
|
|
4
|
+
var array = require('./array.js');
|
|
5
|
+
var product$1 = require('./product.js');
|
|
6
|
+
var product = require('../types/product.js');
|
|
7
|
+
"use strict";
|
|
8
|
+
var Sort = /* @__PURE__ */(Sort2 => {
|
|
9
|
+
Sort2["HighestPrice"] = "highest-price";
|
|
10
|
+
Sort2["LowestPrice"] = "lowest-price";
|
|
11
|
+
Sort2["Availability"] = "availability";
|
|
12
|
+
return Sort2;
|
|
13
|
+
})(Sort || {});
|
|
14
|
+
var SortOrder = /* @__PURE__ */(SortOrder2 => {
|
|
15
|
+
SortOrder2["Asc"] = "Asc";
|
|
16
|
+
SortOrder2["Desc"] = "Desc";
|
|
17
|
+
return SortOrder2;
|
|
18
|
+
})(SortOrder || {});
|
|
19
|
+
const sortedStatusesByPriorityAsc = [product.DisplayProductStatus.Unavailable, product.DisplayProductStatus.ComingSoon, product.DisplayProductStatus.SoldOut, product.DisplayProductStatus.RegionNotAvailable, product.DisplayProductStatus.Backorder, product.DisplayProductStatus.Available];
|
|
20
|
+
const getProductStatusPriority = product$1 => {
|
|
21
|
+
let sortStatus = product$1.status;
|
|
22
|
+
if (product$1.variants.some(variant => variant.availableInOtherStores.length) || product$1.linkedProducts && product$1.linkedProducts.some(linkedProduct => linkedProduct.status === product.DisplayProductStatus.Available)) {
|
|
23
|
+
sortStatus = product.DisplayProductStatus.Available;
|
|
24
|
+
}
|
|
25
|
+
return sortedStatusesByPriorityAsc.indexOf(sortStatus);
|
|
26
|
+
};
|
|
27
|
+
const sortByAvailabilityDesc = (current, next) => {
|
|
28
|
+
return array.normalizeSortResult(getProductStatusPriority(next) - getProductStatusPriority(current));
|
|
29
|
+
};
|
|
30
|
+
const sortByPrice = order => (current, next) => {
|
|
31
|
+
const currentProductPrice = current.minDisplayPrice.amount;
|
|
32
|
+
const nextProductPrice = next.minDisplayPrice.amount;
|
|
33
|
+
if (order === "Desc" /* Desc */) {
|
|
34
|
+
return nextProductPrice - currentProductPrice;
|
|
35
|
+
} else {
|
|
36
|
+
return currentProductPrice - nextProductPrice;
|
|
37
|
+
}
|
|
38
|
+
};
|
|
39
|
+
const sortProducts = (currentSort, storeId, sortByWeightTag = true) => (current, next) => {
|
|
40
|
+
switch (currentSort) {
|
|
41
|
+
case "highest-price" /* HighestPrice */:
|
|
42
|
+
case "lowest-price" /* LowestPrice */:
|
|
43
|
+
{
|
|
44
|
+
return sortByPrice(currentSort === "highest-price" /* HighestPrice */ ? "Desc" /* Desc */ : "Asc" /* Asc */)(current, next);
|
|
45
|
+
}
|
|
46
|
+
case "availability" /* Availability */:
|
|
47
|
+
{
|
|
48
|
+
return sortByAvailabilityDesc(current, next);
|
|
49
|
+
}
|
|
50
|
+
default:
|
|
51
|
+
{
|
|
52
|
+
const sorts = [sortByAvailabilityDesc];
|
|
53
|
+
if (sortByWeightTag) {
|
|
54
|
+
sorts.push(product$1.sortByTagWeight(product$1.SortSiteId.Store, storeId));
|
|
55
|
+
}
|
|
56
|
+
return array.combineSorts(sorts)(current, next);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
};
|
|
60
|
+
exports.Sort = Sort;
|
|
61
|
+
exports.SortOrder = SortOrder;
|
|
62
|
+
exports.sortByAvailabilityDesc = sortByAvailabilityDesc;
|
|
63
|
+
exports.sortByPrice = sortByPrice;
|
|
64
|
+
exports.sortProducts = sortProducts;
|
|
65
|
+
exports.sortedStatusesByPriorityAsc = sortedStatusesByPriorityAsc;
|
|
66
|
+
//# sourceMappingURL=productSort.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"productSort.js","sources":["../../src/utils/productSort.ts"],"sourcesContent":["import type { Nullish, StorefrontProduct, StoreId, UnionToAllPossibleTuples } from \"../types\";\nimport { DisplayProductStatus } from \"../types\";\nimport { combineSorts, normalizeSortResult } from \"./array\";\nimport { sortByTagWeight, SortSiteId } from \"./product\";\n\n/**\n * Narrow MinimalProProductFragment type even more to the union of fields that are needed to sort product\n */\ntype ProductMinFieldsForSorting = Pick<\n StorefrontProduct,\n \"status\" | \"tags\" | \"minDisplayPrice\"\n> & {\n variants: {\n // We only need bare minimum from array to compare array length\n // FIXME: move it to backend as a boolean field so we don't need to fetch entire array to reduce it to a boolean ourselves\n availableInOtherStores: Array<{ __typename: string }>;\n }[];\n linkedProducts?: {\n status: StorefrontProduct[\"status\"];\n }[];\n};\n\nexport enum Sort {\n HighestPrice = \"highest-price\",\n LowestPrice = \"lowest-price\",\n Availability = \"availability\",\n}\n\nexport enum SortOrder {\n Asc = \"Asc\",\n Desc = \"Desc\",\n}\n\n/**\n * Variants statuses sorted in ascending order from least preferable to most, so if some new status comes in, then the\n * lookup for `.indexOf()` would return `-1`, thus being \"worse\" then DisplayProductStatus.SoldOut and moved to the very\n * back of list\n */\nexport const sortedStatusesByPriorityAsc: UnionToAllPossibleTuples<DisplayProductStatus> =\n [\n DisplayProductStatus.Unavailable,\n DisplayProductStatus.ComingSoon,\n DisplayProductStatus.SoldOut,\n DisplayProductStatus.RegionNotAvailable,\n DisplayProductStatus.Backorder,\n DisplayProductStatus.Available,\n ];\n/**\n * Due to currently variant status being nullable for historical reasons we need to have a fallback state\n * TODO: remove once display status is StorefrontProductVariant.status is non-nullable\n */\nconst getProductStatusPriority = (\n product: ProductMinFieldsForSorting,\n): number => {\n let sortStatus = product.status;\n\n // Virtually set sort status as available if it has alternative stores\n if (\n product.variants.some((variant) => variant.availableInOtherStores.length) ||\n (product.linkedProducts &&\n product.linkedProducts.some(\n (linkedProduct) =>\n linkedProduct.status === DisplayProductStatus.Available,\n ))\n ) {\n sortStatus = DisplayProductStatus.Available;\n }\n\n return sortedStatusesByPriorityAsc.indexOf(sortStatus);\n};\n\nexport const sortByAvailabilityDesc = (\n current: ProductMinFieldsForSorting,\n next: ProductMinFieldsForSorting,\n): number => {\n return normalizeSortResult(\n getProductStatusPriority(next) - getProductStatusPriority(current),\n );\n};\n\nexport const sortByPrice =\n (order: SortOrder) => (\n current: ProductMinFieldsForSorting,\n next: ProductMinFieldsForSorting,\n ): number => {\n const currentProductPrice = current.minDisplayPrice.amount;\n\n const nextProductPrice = next.minDisplayPrice.amount;\n\n if (order === SortOrder.Desc) {\n return nextProductPrice - currentProductPrice;\n } else {\n return currentProductPrice - nextProductPrice;\n }\n };\n\n\n/**\n * Sorts products based on the given sort type.\n * @param currentSort - sort type\n * @param sortByWeightTag - if false, then sort by weight tag is\n * ignored (e.g. for `Related Add-ons` where\n * we don't want to sort by weight tag)\n */\nexport const sortProducts = (\n currentSort: Nullish<string | Sort>,\n storeId?: StoreId,\n sortByWeightTag = true,\n ) =>\n (current: ProductMinFieldsForSorting, next: ProductMinFieldsForSorting) => {\n switch (currentSort) {\n // Sort by price\n case Sort.HighestPrice:\n case Sort.LowestPrice: {\n return sortByPrice(\n currentSort === Sort.HighestPrice ? SortOrder.Desc : SortOrder.Asc,\n )(current, next);\n }\n\n case Sort.Availability: {\n return sortByAvailabilityDesc(current, next);\n }\n\n default: {\n const sorts = [sortByAvailabilityDesc];\n\n // Add sorting by tag weight\n if (sortByWeightTag) {\n sorts.push(sortByTagWeight(SortSiteId.Store, storeId));\n }\n\n // Do additional sorting by tag weight only after initial sorting resulting in \"same group\"\n return combineSorts(sorts)(current, next);\n }\n }\n };"],"names":["Sort","SortOrder","sortedStatusesByPriorityAsc","DisplayProductStatus","Unavailable","ComingSoon","SoldOut","RegionNotAvailable","Backorder","Available","getProductStatusPriority","product","sortStatus","status","variants","some","variant","availableInOtherStores","length","linkedProducts","linkedProduct","indexOf","sortByAvailabilityDesc","current","next","normalizeSortResult","sortByPrice","order","currentProductPrice","minDisplayPrice","amount","nextProductPrice","sortProducts","currentSort","storeId","sortByWeightTag","sorts","push","sortByTagWeight","SortSiteId","Store","combineSorts"],"mappings":";;;;;;;AAsBY,IAAAA,IAAA,GAAA,gBAAAA,KAAL,IAAA;EACLA,MAAA,cAAe,CAAA,GAAA,eAAA;EACfA,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a,OAAO,CAAA,CACnE;AACF,CAAA;AAEO,MAAMG,WACX,GAACC,KAAqB,IAAA,CACpBJ,SACAC,IACW,KAAA;EACL,MAAAI,mBAAA,GAAsBL,QAAQM,eAAgB,CAAAC,MAAA;EAE9C,MAAAC,gBAAA,GAAmBP,KAAKK,eAAgB,CAAAC,MAAA;EAE9C,IAAIH,UAAU,MAAgB,CAAA,YAAA;IAC5B,OAAOI,gBAAmB,GAAAH,mBAAA;EAAA,CACrB,MAAA;IACL,OAAOA,mBAAsB,GAAAG,gBAAA;EAC/B;AACF,CAAA;AAUW,MAAAC,YAAA,GAAeA,CACxBC,WACA,EAAAC,OAAA,EACAC,kBAAkB,IAEpB,KAAA,CAACZ,SAAqCC,IAAqC,KAAA;EACzE,QAAQS,WAAa;IAEnB,KAAK,eAAA,CAAA;IACL,KAAK,cAAkB,CAAA;MAAA;QACd,OAAAP,WAAA,CACLO,WAAA,KAAgB,gBAAoB,qBAAA,MAAiB,cAAA,KAAA,CAAA,SAAA,CACvD,CAAEV,SAASC,IAAI,CAAA;MACjB;IAEA,KAAK,cAAmB,CAAA;MAAA;QACf,OAAAF,sBAAA,CAAuBC,SAASC,IAAI,CAAA;MAC7C;IAEA;MAAS;QACD,MAAAY,KAAA,GAAQ,CAACd,sBAAsB,CAAA;QAGrC,IAAIa,eAAiB,EAAA;UACnBC,KAAA,CAAMC,IAAK,CAAAC,SAAA,CAAAA,eAAA,CAAgBC,SAAW,CAAAA,UAAA,CAAAC,KAAA,EAAON,OAAO,CAAC,CAAA;QACvD;QAGA,OAAOO,KAAa,CAAAA,YAAA,CAAAL,KAAK,CAAE,CAAAb,OAAA,EAASC,IAAI,CAAA;MAC1C;EACF;AACF,CAAA;;;;;;"}
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import '../types/index.mjs';
|
|
2
|
+
import { normalizeSortResult, combineSorts } from './array.mjs';
|
|
3
|
+
import { sortByTagWeight, SortSiteId } from './product.mjs';
|
|
4
|
+
import { DisplayProductStatus } from '../types/product.mjs';
|
|
5
|
+
"use strict";
|
|
6
|
+
var Sort = /* @__PURE__ */(Sort2 => {
|
|
7
|
+
Sort2["HighestPrice"] = "highest-price";
|
|
8
|
+
Sort2["LowestPrice"] = "lowest-price";
|
|
9
|
+
Sort2["Availability"] = "availability";
|
|
10
|
+
return Sort2;
|
|
11
|
+
})(Sort || {});
|
|
12
|
+
var SortOrder = /* @__PURE__ */(SortOrder2 => {
|
|
13
|
+
SortOrder2["Asc"] = "Asc";
|
|
14
|
+
SortOrder2["Desc"] = "Desc";
|
|
15
|
+
return SortOrder2;
|
|
16
|
+
})(SortOrder || {});
|
|
17
|
+
const sortedStatusesByPriorityAsc = [DisplayProductStatus.Unavailable, DisplayProductStatus.ComingSoon, DisplayProductStatus.SoldOut, DisplayProductStatus.RegionNotAvailable, DisplayProductStatus.Backorder, DisplayProductStatus.Available];
|
|
18
|
+
const getProductStatusPriority = product => {
|
|
19
|
+
let sortStatus = product.status;
|
|
20
|
+
if (product.variants.some(variant => variant.availableInOtherStores.length) || product.linkedProducts && product.linkedProducts.some(linkedProduct => linkedProduct.status === DisplayProductStatus.Available)) {
|
|
21
|
+
sortStatus = DisplayProductStatus.Available;
|
|
22
|
+
}
|
|
23
|
+
return sortedStatusesByPriorityAsc.indexOf(sortStatus);
|
|
24
|
+
};
|
|
25
|
+
const sortByAvailabilityDesc = (current, next) => {
|
|
26
|
+
return normalizeSortResult(getProductStatusPriority(next) - getProductStatusPriority(current));
|
|
27
|
+
};
|
|
28
|
+
const sortByPrice = order => (current, next) => {
|
|
29
|
+
const currentProductPrice = current.minDisplayPrice.amount;
|
|
30
|
+
const nextProductPrice = next.minDisplayPrice.amount;
|
|
31
|
+
if (order === "Desc" /* Desc */) {
|
|
32
|
+
return nextProductPrice - currentProductPrice;
|
|
33
|
+
} else {
|
|
34
|
+
return currentProductPrice - nextProductPrice;
|
|
35
|
+
}
|
|
36
|
+
};
|
|
37
|
+
const sortProducts = (currentSort, storeId, sortByWeightTag = true) => (current, next) => {
|
|
38
|
+
switch (currentSort) {
|
|
39
|
+
case "highest-price" /* HighestPrice */:
|
|
40
|
+
case "lowest-price" /* LowestPrice */:
|
|
41
|
+
{
|
|
42
|
+
return sortByPrice(currentSort === "highest-price" /* HighestPrice */ ? "Desc" /* Desc */ : "Asc" /* Asc */)(current, next);
|
|
43
|
+
}
|
|
44
|
+
case "availability" /* Availability */:
|
|
45
|
+
{
|
|
46
|
+
return sortByAvailabilityDesc(current, next);
|
|
47
|
+
}
|
|
48
|
+
default:
|
|
49
|
+
{
|
|
50
|
+
const sorts = [sortByAvailabilityDesc];
|
|
51
|
+
if (sortByWeightTag) {
|
|
52
|
+
sorts.push(sortByTagWeight(SortSiteId.Store, storeId));
|
|
53
|
+
}
|
|
54
|
+
return combineSorts(sorts)(current, next);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
};
|
|
58
|
+
export { Sort, SortOrder, sortByAvailabilityDesc, sortByPrice, sortProducts, sortedStatusesByPriorityAsc };
|
|
59
|
+
//# sourceMappingURL=productSort.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"productSort.mjs","sources":["../../src/utils/productSort.ts"],"sourcesContent":["import type { Nullish, StorefrontProduct, StoreId, UnionToAllPossibleTuples } from \"../types\";\nimport { DisplayProductStatus } from \"../types\";\nimport { combineSorts, normalizeSortResult } from \"./array\";\nimport { sortByTagWeight, SortSiteId } from \"./product\";\n\n/**\n * Narrow MinimalProProductFragment type even more to the union of fields that are needed to sort product\n */\ntype ProductMinFieldsForSorting = Pick<\n StorefrontProduct,\n \"status\" | \"tags\" | \"minDisplayPrice\"\n> & {\n variants: {\n // We only need bare minimum from array to compare array length\n // FIXME: move it to backend as a boolean field so we don't need to fetch entire array to reduce it to a boolean ourselves\n availableInOtherStores: Array<{ __typename: string }>;\n }[];\n linkedProducts?: {\n status: StorefrontProduct[\"status\"];\n }[];\n};\n\nexport enum Sort {\n HighestPrice = \"highest-price\",\n LowestPrice = \"lowest-price\",\n Availability = \"availability\",\n}\n\nexport enum SortOrder {\n Asc = \"Asc\",\n Desc = \"Desc\",\n}\n\n/**\n * Variants statuses sorted in ascending order from least preferable to most, so if some new status comes in, then the\n * lookup for `.indexOf()` would return `-1`, thus being \"worse\" then DisplayProductStatus.SoldOut and moved to the very\n * back of list\n */\nexport const sortedStatusesByPriorityAsc: UnionToAllPossibleTuples<DisplayProductStatus> =\n [\n DisplayProductStatus.Unavailable,\n DisplayProductStatus.ComingSoon,\n DisplayProductStatus.SoldOut,\n DisplayProductStatus.RegionNotAvailable,\n DisplayProductStatus.Backorder,\n DisplayProductStatus.Available,\n ];\n/**\n * Due to currently variant status being nullable for historical reasons we need to have a fallback state\n * TODO: remove once display status is StorefrontProductVariant.status is non-nullable\n */\nconst getProductStatusPriority = (\n product: ProductMinFieldsForSorting,\n): number => {\n let sortStatus = product.status;\n\n // Virtually set sort status as available if it has alternative stores\n if (\n product.variants.some((variant) => variant.availableInOtherStores.length) ||\n (product.linkedProducts &&\n product.linkedProducts.some(\n (linkedProduct) =>\n linkedProduct.status === DisplayProductStatus.Available,\n ))\n ) {\n sortStatus = DisplayProductStatus.Available;\n }\n\n return sortedStatusesByPriorityAsc.indexOf(sortStatus);\n};\n\nexport const sortByAvailabilityDesc = (\n current: ProductMinFieldsForSorting,\n next: ProductMinFieldsForSorting,\n): number => {\n return normalizeSortResult(\n getProductStatusPriority(next) - getProductStatusPriority(current),\n );\n};\n\nexport const sortByPrice =\n (order: SortOrder) => (\n current: ProductMinFieldsForSorting,\n next: ProductMinFieldsForSorting,\n ): number => {\n const currentProductPrice = current.minDisplayPrice.amount;\n\n const nextProductPrice = next.minDisplayPrice.amount;\n\n if (order === SortOrder.Desc) {\n return nextProductPrice - currentProductPrice;\n } else {\n return currentProductPrice - nextProductPrice;\n }\n };\n\n\n/**\n * Sorts products based on the given sort type.\n * @param currentSort - sort type\n * @param sortByWeightTag - if false, then sort by weight tag is\n * ignored (e.g. for `Related Add-ons` where\n * we don't want to sort by weight tag)\n */\nexport const sortProducts = (\n currentSort: Nullish<string | Sort>,\n storeId?: StoreId,\n sortByWeightTag = true,\n ) =>\n (current: ProductMinFieldsForSorting, next: ProductMinFieldsForSorting) => {\n switch (currentSort) {\n // Sort by price\n case Sort.HighestPrice:\n case Sort.LowestPrice: {\n return sortByPrice(\n currentSort === Sort.HighestPrice ? SortOrder.Desc : SortOrder.Asc,\n )(current, next);\n }\n\n case Sort.Availability: {\n return sortByAvailabilityDesc(current, next);\n }\n\n default: {\n const sorts = [sortByAvailabilityDesc];\n\n // Add sorting by tag weight\n if (sortByWeightTag) {\n sorts.push(sortByTagWeight(SortSiteId.Store, storeId));\n }\n\n // Do additional sorting by tag weight only after initial sorting resulting in \"same group\"\n return combineSorts(sorts)(current, next);\n }\n }\n };"],"names":["Sort","SortOrder","sortedStatusesByPriorityAsc","DisplayProductStatus","Unavailable","ComingSoon","SoldOut","RegionNotAvailable","Backorder","Available","getProductStatusPriority","product","sortStatus","status","variants","some","variant","availableInOtherStores","length","linkedProducts","linkedProduct","indexOf","sortByAvailabilityDesc","current","next","normalizeSortResult","sortByPrice","order","currentProductPrice","minDisplayPrice","amount","nextProductPrice","sortProducts","currentSort","storeId","sortByWeightTag","sorts","push","sortByTagWeight","SortSiteId","Store","combineSorts"],"mappings":";;;;;AAsBY,IAAAA,IAAA,GAAA,gBAAAA,KAAL,IAAA;EACLA,MAAA,cAAe,CAAA,GAAA,eAAA;EACfA,MAAA,aAAc,CAAA,GAAA,cAAA;EACdA,MAAA,cAAe,CAAA,GAAA,cAAA;EAHLA,OAAAA,KAAAA;AAAA,CAAA,EAAAA,IAAA,IAAA,CAAA,CAAA,CAAA;AAMA,IAAAC,SAAA,GAAA,gBAAAA,UAAL,IAAA;EACLA,WAAA,KAAM,CAAA,GAAA,KAAA;EACNA,WAAA,MAAO,CAAA,GAAA,MAAA;EAFGA,OAAAA,UAAAA;AAAA,CAAA,EAAAA,SAAA,IAAA,CAAA,CAAA,C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