UNPKG

@ecomplus/utils

Version:

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

45 lines (44 loc) 2.18 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; var _specValues = _interopRequireDefault(require("./spec-values")); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } /** * @method * @memberof ecomUtils * @name specTextValue * @description Parse specifications array of nested objects to string. * @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 * @param {string} [delimiter=', '] - Delimiter between each specification * @returns {Array|null} * * @example * const product = { 'name': 'Cruzeiro 2019', 'variations': [ { 'name': 'Cruzeiro 2019 / P / azul', 'specifications': { 'colors': [ { 'text': 'azul', 'value': '#3300ff' }, { 'text': 'vermelho', 'value': '#ff0000' } ] } } ] } * const gridId = 'colors' * const delimiter = ',' * 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, delimiter) * // => [{text: 'vermelho', value: '#ff0000'}, {text: 'azul', value: '#3300ff'}] */ var specTextValue = function specTextValue(product, gridId, grids) { var delimiter = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : ', '; // using text property of each spec object var values = (0, _specValues.default)(product, gridId, grids); if (values.length) { var valuesString = values[0].text; for (var i = 1; i < values.length; i++) { valuesString += delimiter + values[i].text; } return valuesString; } // specification not found return null; }; var _default = exports.default = specTextValue;