UNPKG

@wordpress/components

Version:
46 lines (39 loc) 1.05 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.buildTermsTree = buildTermsTree; var _lodash = require("lodash"); /** * External dependencies */ /** * Returns terms in a tree form. * * @param {Array} flatTerms Array of terms in flat format. * * @return {Array} Array of terms in tree format. */ function buildTermsTree(flatTerms) { const flatTermsWithParentAndChildren = flatTerms.map(term => { return { children: [], parent: null, ...term }; }); const termsByParent = (0, _lodash.groupBy)(flatTermsWithParentAndChildren, 'parent'); if (termsByParent.null && termsByParent.null.length) { return flatTermsWithParentAndChildren; } const fillWithChildren = terms => { return terms.map(term => { const children = termsByParent[term.id]; return { ...term, children: children && children.length ? fillWithChildren(children) : [] }; }); }; return fillWithChildren(termsByParent['0'] || []); } //# sourceMappingURL=terms.js.map