UNPKG

@ecomplus/utils

Version:

JS utility functions to E-Com Plus (not only) related apps

101 lines (99 loc) 4.25 kB
"use strict"; 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.array.concat.js"); require("core-js/modules/es.array.find.js"); require("core-js/modules/es.array.map.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 specValues * @description Returns array of spec objects for specified grid. * @param {Object.<string, *>|Array} product - Product body or array of variation objects * @param {string} gridId - Grid ID string such as 'color' * @param {Array} [grids] - List of grid objects * @returns {Array} * * @example * const product = { 'name': 'Cruzeiro 2019', 'variations': [ { 'name': 'Cruzeiro 2019 / P / azul', 'specifications': { 'colors': [ { 'text': 'azul', 'value': '#3300ff' } ] } } ] } * const gridId = 'colors' * const grid1 = { 'grid_id': 'size', 'title': 'Tamanho', 'options': [ { 'text': 'P', 'option_id': 'pp' } ] } * const grid2 = { 'title': 'Cores', 'grid_id': 'colors', 'options': [ { 'text': 'vermelho', 'option_id': 'vermelho', 'colors': [ '#ff0000' ] }, { 'text': 'azul', 'option_id': 'azul', 'colors': [ '#3300ff' ] } ] } * const grid3 = { 'title': 'Conector', 'grid_id': 'conector', 'options': [ { 'text': 'USB', 'option_id': 'usb' } ] } * const grids = [ grid1, grid2, grid3 ] * ecomUtils.specValues(product, gridId, grids) * // => [{text: "vermelho", value: "#ff0000"}, {text: "azul", value: "#3300ff"}] */ var specValues = function specValues(product, gridId, grids) { if (_typeof(product) !== 'object' || product === null) { // nothing to do // returns empty array by default return []; } var values = []; if (_typeof(product) === 'object' && product !== null) { if (Array.isArray(product)) { if (product.length) { if (product[0].specifications) { // variations array sent as product param product.forEach(function (variation) { values = values.concat(specValues(variation, gridId, grids)); }); } else if (product[0].text) { // spec values list sent as product param values = product; } } } else { // probably the product object from instance data // work for product or specific variation product var specifications = product.specifications; if (specifications) { for (var id in specifications) { if (specifications[id] && id === gridId) { // specification found values = specifications[gridId]; } } } if (values && !values.length && Array.isArray(grids) && grids.length) { // try with grids list from preloaded data var specs, grid; // match the grid by ID if (grids && (grid = grids.find(function (grid) { return grid.grid_id === gridId; })) && grid.options) { // mounts specs array from grid options list specs = grid.options.map(function (option) { // try color RGB value or use default option ID var value; if (option.colors && option.colors.length) { value = option.colors[0]; } else { value = option.option_id; } // spec options with text and value return { text: option.text, value: value }; }); } return specValues(specs, gridId); } } } return values; }; var _default = exports.default = specValues;