@wordpress/editor
Version:
Enhanced block editor for WordPress posts.
8 lines (7 loc) • 3.01 kB
Source Map (JSON)
{
"version": 3,
"sources": ["../../src/utils/terms.js"],
"sourcesContent": ["import { decodeEntities } from '@wordpress/html-entities';\n\n/**\n * Returns terms in a tree form.\n *\n * @param {Array} flatTerms Array of terms in flat format.\n *\n * @return {Array} Array of terms in tree format.\n */\nexport function buildTermsTree( flatTerms ) {\n\tconst flatTermsWithParentAndChildren = flatTerms.map( ( term ) => {\n\t\treturn {\n\t\t\tchildren: [],\n\t\t\tparent: undefined,\n\t\t\t...term,\n\t\t};\n\t} );\n\n\t// All terms should have a `parent` because we're about to index them by it.\n\tif (\n\t\tflatTermsWithParentAndChildren.some(\n\t\t\t( { parent } ) => parent === undefined\n\t\t)\n\t) {\n\t\treturn flatTermsWithParentAndChildren;\n\t}\n\n\tconst termsById = Object.create( null );\n\tfor ( const term of flatTermsWithParentAndChildren ) {\n\t\t// The tree owns `children`, so drop whatever the input carried.\n\t\tterm.children = [];\n\t\ttermsById[ term.id ] = term;\n\t}\n\n\t// Terms whose parent is missing are unreachable, so they are dropped.\n\tconst tree = [];\n\tfor ( const term of flatTermsWithParentAndChildren ) {\n\t\tconst parent = termsById[ term.parent ];\n\t\tif ( parent ) {\n\t\t\tparent.children.push( term );\n\t\t} else if ( `${ term.parent }` === '0' ) {\n\t\t\ttree.push( term );\n\t\t}\n\t}\n\n\treturn tree;\n}\n\nexport const unescapeString = ( arg ) => {\n\treturn decodeEntities( arg );\n};\n\n/**\n * Returns a term object with name unescaped.\n *\n * @param {Object} term The term object to unescape.\n *\n * @return {Object} Term object with name property unescaped.\n */\nexport const unescapeTerm = ( term ) => {\n\treturn {\n\t\t...term,\n\t\tname: unescapeString( term.name ),\n\t};\n};\n\n/**\n * Returns an array of term objects with names unescaped.\n * The unescape of each term is performed using the unescapeTerm function.\n *\n * @param {Object[]} terms Array of term objects to unescape.\n *\n * @return {Object[]} Array of term objects unescaped.\n */\nexport const unescapeTerms = ( terms ) => {\n\treturn ( terms ?? [] ).map( unescapeTerm );\n};\n"],
"mappings": ";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,2BAA+B;AASxB,SAAS,eAAgB,WAAY;AAC3C,QAAM,iCAAiC,UAAU,IAAK,CAAE,SAAU;AACjE,WAAO;AAAA,MACN,UAAU,CAAC;AAAA,MACX,QAAQ;AAAA,MACR,GAAG;AAAA,IACJ;AAAA,EACD,CAAE;AAGF,MACC,+BAA+B;AAAA,IAC9B,CAAE,EAAE,OAAO,MAAO,WAAW;AAAA,EAC9B,GACC;AACD,WAAO;AAAA,EACR;AAEA,QAAM,YAAY,uBAAO,OAAQ,IAAK;AACtC,aAAY,QAAQ,gCAAiC;AAEpD,SAAK,WAAW,CAAC;AACjB,cAAW,KAAK,EAAG,IAAI;AAAA,EACxB;AAGA,QAAM,OAAO,CAAC;AACd,aAAY,QAAQ,gCAAiC;AACpD,UAAM,SAAS,UAAW,KAAK,MAAO;AACtC,QAAK,QAAS;AACb,aAAO,SAAS,KAAM,IAAK;AAAA,IAC5B,WAAY,GAAI,KAAK,MAAO,OAAO,KAAM;AACxC,WAAK,KAAM,IAAK;AAAA,IACjB;AAAA,EACD;AAEA,SAAO;AACR;AAEO,IAAM,iBAAiB,CAAE,QAAS;AACxC,aAAO,qCAAgB,GAAI;AAC5B;AASO,IAAM,eAAe,CAAE,SAAU;AACvC,SAAO;AAAA,IACN,GAAG;AAAA,IACH,MAAM,eAAgB,KAAK,IAAK;AAAA,EACjC;AACD;AAUO,IAAM,gBAAgB,CAAE,UAAW;AACzC,UAAS,SAAS,CAAC,GAAI,IAAK,YAAa;AAC1C;",
"names": []
}