methods/get-price-range.js

  1. export default (self, result) => {
  2. // get price aggregations from result object
  3. if (!result) {
  4. result = self.result || {}
  5. }
  6. const { aggregations } = result
  7. return aggregations
  8. ? {
  9. min: (aggregations.min_price && aggregations.min_price.value) || 0,
  10. avg: (aggregations.avg_price && aggregations.avg_price.value) || 0,
  11. max: (aggregations.max_price && aggregations.max_price.value) || 0
  12. }
  13. : {
  14. min: 0,
  15. avg: 0,
  16. max: 0
  17. }
  18. }
  19. /**
  20. * @method
  21. * @name EcomSearch#getPriceRange
  22. * @description Get minimum, maximum and average item prices
  23. * from search result object.
  24. *
  25. * @param {result} [result=self.result] - Search result object
  26. * @returns {prices}
  27. *
  28. * @example
  29. // Run search request and get resultant price range
  30. search.fetch().then(() => {
  31. const { min, avg, max } = search.getPriceRange()
  32. console.log(`prices => min: ${min}; max: ${max}; avg: ${avg}`)
  33. })
  34. */