@ecomplus/utils
Version:
JS utility functions to E-Com Plus (not only) related apps
40 lines (39 loc) • 1.37 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
require("core-js/modules/es.array.filter.js");
require("core-js/modules/es.object.to-string.js");
/**
* @method
* @memberof ecomUtils
* @name filterByParentSlug
* @description Filter categories list by parent category slug.
* @param {Array} categories - List of category objects
* @param {string} slug - Parent category slug value
* @returns {Array}
*
* @example
* // Full object ref.: https://developers.e-com.plus/docs/api/#/store/categories/
* const categories = []
* categories.push({ name: 'PCs', slug: 'pcs', parent: { name: 'Info', slug: 'info' } })
* categories.push({ name: 'Shirts', slug: 'shirts', parent: { name: 'Clothes', slug: 'clothes' } })
* categories.push({ name: 'Info', slug: 'info' })
* ecomUtils.filterByParentSlug(categories, 'info')
* // => [ { name: 'PCs', slug: 'pcs', parent: { name: 'Info', slug: 'info' } } ]
*/
var filterByParentSlug = function filterByParentSlug(categories, slug) {
// for categories
// returns array of macthed category objects
try {
return categories.filter(function (category) {
return category.parent && category.parent.slug === slug;
});
} catch (err) {
// not an array ?
console.error(err);
return [];
}
};
var _default = exports.default = filterByParentSlug;