UNPKG

@uportal/portlet-registry-to-array

Version:

A utility that flattens the portlet registry tree into an array

61 lines (46 loc) 2.06 kB
(function (global, factory) { typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('lodash/uniqBy')) : typeof define === 'function' && define.amd ? define(['exports', 'lodash/uniqBy'], factory) : (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.portletRegistryToArray = {}, global.uniqBy)); }(this, (function (exports, uniqBy) { 'use strict'; function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; } var uniqBy__default = /*#__PURE__*/_interopDefaultLegacy(uniqBy); /** * Combines a array of arrays into a single level array * @param {Array<Portlet>} acc - accululator that combines all the arrays * @param {Array<Portlet>} arr - new array to add to the accumulator * @return {Array<Portlet>} merged arrays */ function flatten(acc, arr) { return acc.concat(arr); } /** * Takes the returned array from treeWalker and removes duplicates * based on "fname" * @param {Object} registryJson Portlet Registry Tree * @return {Array<Portlet>} list of portlets */ function portletRegistryToArray(registryJson) { return uniqBy__default['default'](treeWalker(registryJson), 'fname'); } /** * Walks the portlet registry tree * @param {Object} registryJson Portlet Registry Tree * @return {Array<Portlet>} list of portlets */ function treeWalker(registryJson) { if (registryJson.registry) { return treeWalker(registryJson.registry); } var portlets = registryJson.portlets || []; if (registryJson.categories) { return portlets.concat(registryJson.categories.map(portletRegistryToArray)).reduce(flatten, []); } if (registryJson.subcategories) { return portlets.concat(registryJson.subcategories.map(portletRegistryToArray)).reduce(flatten, []); } return portlets; } exports.portletRegistryToArray = portletRegistryToArray; Object.defineProperty(exports, '__esModule', { value: true }); })));