UNPKG

@uportal/portlet-registry-to-array

Version:

A utility that flattens the portlet registry tree into an array

57 lines (42 loc) 1.6 kB
'use strict'; Object.defineProperty(exports, '__esModule', { value: true }); var uniqBy = require('lodash/uniqBy'); 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;