UNPKG

@shopgate/pwa-common

Version:

Common library for the Shopgate Connect PWA.

11 lines 1.89 kB
function _extends(){_extends=Object.assign||function(target){for(var i=1;i<arguments.length;i++){var source=arguments[i];for(var key in source){if(Object.prototype.hasOwnProperty.call(source,key)){target[key]=source[key];}}}return target;};return _extends.apply(this,arguments);}import{logger}from'@shopgate/pwa-core/helpers';import*as pipelines from"../../constants/Pipelines";import{generateSortedHash}from"./generateSortedHash";/** * Generates a hash for product collection results. * @param {Object} params The request parameters. * @param {boolean} [includeSort=true] Whether to include the sorting in the hash. * @param {boolean} [includeFilters=true] Whether to include the filters in the hash. * @param {string} [defaultSort=null] A default value for the sort parameter * @return {string} The generated hash. */export function generateResultHash(params){var includeSort=arguments.length>1&&arguments[1]!==undefined?arguments[1]:true;var includeFilters=arguments.length>2&&arguments[2]!==undefined?arguments[2]:true;var defaultSort=arguments.length>3&&arguments[3]!==undefined?arguments[3]:null;var defaultParams=_extends({pipeline:pipelines.SHOPGATE_CATALOG_GET_PRODUCTS},includeFilters&&{filters:{}},{},includeSort&&defaultSort&&{sort:defaultSort});var mergedParams=_extends({},defaultParams,{},params);if(includeSort&&!mergedParams.sort){logger.error('generateResultHash() needs to be called with a sort parameter when the sort is supposed to be included');}if(!includeSort&&mergedParams.sort){delete mergedParams.sort;}var searchPhrase=mergedParams.searchPhrase;if(searchPhrase){/** * We trim the search phrase here, because otherwise we will create different hash and cache * entries for the same search term (because whitespaces don't influence the search results) */mergedParams.searchPhrase=searchPhrase.trim();}return generateSortedHash(mergedParams);}