UNPKG

@wordpress/editor

Version:
86 lines (70 loc) 2.15 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.buildTermsTree = buildTermsTree; exports.unescapeTerms = exports.unescapeTerm = exports.unescapeString = void 0; 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'] || []); } // Lodash unescape function handles ' but not ' which may be return in some API requests. const unescapeString = arg => { return (0, _lodash.unescape)(arg.replace(''', "'")); }; /** * Returns a term object with name unescaped. * The unescape of the name property is done using lodash unescape function. * * @param {Object} term The term object to unescape. * * @return {Object} Term object with name property unescaped. */ exports.unescapeString = unescapeString; const unescapeTerm = term => { return { ...term, name: unescapeString(term.name) }; }; /** * Returns an array of term objects with names unescaped. * The unescape of each term is performed using the unescapeTerm function. * * @param {Object[]} terms Array of term objects to unescape. * * @return {Object[]} Array of term objects unescaped. */ exports.unescapeTerm = unescapeTerm; const unescapeTerms = terms => { return (0, _lodash.map)(terms, unescapeTerm); }; exports.unescapeTerms = unescapeTerms; //# sourceMappingURL=terms.js.map