UNPKG

@woocommerce/data

Version:
51 lines (50 loc) 1.83 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.createIdFromOptions = exports.getTotalProductCountResourceName = exports.getProductResourceName = void 0; /** * Internal dependencies */ const utils_1 = require("../utils"); const PRODUCT_PREFIX = 'product'; /** * Generate a resource name for products. * * @param {Object} query Query for products. * @return {string} Resource name for products. */ function getProductResourceName(query) { return (0, utils_1.getResourceName)(PRODUCT_PREFIX, query); } exports.getProductResourceName = getProductResourceName; /** * 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. */ function getTotalProductCountResourceName(query) { const { _fields, page, per_page, ...totalsQuery } = query; return getProductResourceName(totalsQuery); } exports.getTotalProductCountResourceName = getTotalProductCountResourceName; /** * Create a unique string ID based the options object. * * @param {GetSuggestedProductsOptions} options - Options to create the ID from. * @return {string} Unique ID. */ 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); } exports.createIdFromOptions = createIdFromOptions;