@woocommerce/data
Version:
WooCommerce Admin data store and utilities
45 lines (44 loc) • 1.47 kB
JavaScript
/**
* Internal dependencies
*/
import { getResourceName } from '../utils';
const PRODUCT_PREFIX = 'product';
/**
* Generate a resource name for products.
*
* @param {Object} query Query for products.
* @return {string} Resource name for products.
*/
export function getProductResourceName(query) {
return getResourceName(PRODUCT_PREFIX, query);
}
/**
* Generate a resource name for product totals count.
*
* It omits query parameters from the identifier that don't affect
* totals values like pagination and response field filtering.
*
* @param {Object} query Query for product totals count.
* @return {string} Resource name for product totals.
*/
export function getTotalProductCountResourceName(query) {
const { _fields, page, per_page, ...totalsQuery } = query;
return getProductResourceName(totalsQuery);
}
/**
* Create a unique string ID based the options object.
*
* @param {GetSuggestedProductsOptions} options - Options to create the ID from.
* @return {string} Unique ID.
*/
export function createIdFromOptions(options = {}) {
var _a, _b, _c;
if (!Object.keys(options).length) {
return 'default';
}
const optionsForKey = { ...options };
(_a = options.categories) === null || _a === void 0 ? void 0 : _a.sort();
(_b = options.tags) === null || _b === void 0 ? void 0 : _b.sort();
(_c = options.attributes) === null || _c === void 0 ? void 0 : _c.sort();
return JSON.stringify(optionsForKey);
}