@ecomplus/utils
Version:
JS utility functions to E-Com Plus (not only) related apps
63 lines (61 loc) • 2.47 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
require("core-js/modules/es.object.to-string.js");
require("core-js/modules/web.dom-collections.for-each.js");
require("core-js/modules/es.object.assign.js");
require("core-js/modules/es.symbol.js");
require("core-js/modules/es.symbol.description.js");
require("core-js/modules/es.symbol.iterator.js");
require("core-js/modules/es.array.iterator.js");
require("core-js/modules/es.string.iterator.js");
require("core-js/modules/web.dom-collections.iterator.js");
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
/**
* @method
* @memberof ecomUtils
* @name searchedItems
* @description Returns array of items (products) from Search API response.
* @param {Object.<string, *>|Array} result - Search response body or ELS hits array
* @returns {Array}
*
* @example
* // Full Search API response samples:
* // https://developers.e-com.plus/docs/api/#/search/items/items
* const result = { took: 6, hits: { total: 2, hits: [] } }
* result.hits.hits.push({ _id: '123', _source: { sku: 'TEST', name: 'Test' } })
* result.hits.hits.push({ _id: '456', _source: { sku: 'SMP', name: 'Smp' } })
* ecomUtils.searchedItems(result)
* // => [ { _id: '123', sku: 'TEST', name: 'Test' }, { _id: '456', sku: 'SMP', name: 'Smp' } ]
* // Same passing the `hits` array as param
* ecomUtils.searchedItems(result.hits.hits)
* // => [ { _id: '123', sku: 'TEST', name: 'Test' }, { _id: '456', sku: 'SMP', name: 'Smp' } ]
*/
var searchedItems = function searchedItems(result) {
var hits;
if (_typeof(result) === 'object' && result !== null) {
if (Array.isArray(result)) {
hits = result;
} else if (result.hits) {
// ELS response body
hits = result.hits.hits || result.hits;
}
}
// setup items list
// array of nested objects
var items = [];
if (Array.isArray(hits)) {
// map items array from ELS hits list
hits.forEach(function (_ref) {
var _id = _ref._id,
_source = _ref._source;
items.push(Object.assign({}, _source, {
_id: _id
}));
});
}
return items;
};
var _default = exports.default = searchedItems;