UNPKG

@wordpress/components

Version:
8 lines (7 loc) 2.34 kB
{ "version": 3, "sources": ["../../src/query-controls/terms.ts"], "sourcesContent": ["/**\n * Internal dependencies\n */\n\nconst ensureParentsAreDefined = terms => {\n return terms.every(term => term.parent !== null);\n};\n/**\n * Returns terms in a tree form.\n *\n * @param flatTerms Array of terms in flat format.\n *\n * @return Terms in tree format.\n */\nexport function buildTermsTree(flatTerms) {\n const flatTermsWithParentAndChildren = flatTerms.map(term => ({\n children: [],\n parent: null,\n ...term,\n id: String(term.id)\n }));\n\n // We use a custom type guard here to ensure that the parent property is\n // defined on all terms. The type of the `parent` property is `number | null`\n // and we need to ensure that it is `number`. This is because we use the\n // `parent` property as a key in the `termsByParent` object.\n if (!ensureParentsAreDefined(flatTermsWithParentAndChildren)) {\n return flatTermsWithParentAndChildren;\n }\n const termsByParent = flatTermsWithParentAndChildren.reduce((acc, term) => {\n const {\n parent\n } = term;\n if (!acc[parent]) {\n acc[parent] = [];\n }\n acc[parent].push(term);\n return acc;\n }, {});\n const fillWithChildren = terms => {\n return terms.map(term => {\n const children = termsByParent[term.id];\n return {\n ...term,\n children: children && children.length ? fillWithChildren(children) : []\n };\n });\n };\n return fillWithChildren(termsByParent['0'] || []);\n}"], "mappings": ";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAIA,IAAM,0BAA0B,WAAS;AACvC,SAAO,MAAM,MAAM,UAAQ,KAAK,WAAW,IAAI;AACjD;AAQO,SAAS,eAAe,WAAW;AACxC,QAAM,iCAAiC,UAAU,IAAI,WAAS;AAAA,IAC5D,UAAU,CAAC;AAAA,IACX,QAAQ;AAAA,IACR,GAAG;AAAA,IACH,IAAI,OAAO,KAAK,EAAE;AAAA,EACpB,EAAE;AAMF,MAAI,CAAC,wBAAwB,8BAA8B,GAAG;AAC5D,WAAO;AAAA,EACT;AACA,QAAM,gBAAgB,+BAA+B,OAAO,CAAC,KAAK,SAAS;AACzE,UAAM;AAAA,MACJ;AAAA,IACF,IAAI;AACJ,QAAI,CAAC,IAAI,MAAM,GAAG;AAChB,UAAI,MAAM,IAAI,CAAC;AAAA,IACjB;AACA,QAAI,MAAM,EAAE,KAAK,IAAI;AACrB,WAAO;AAAA,EACT,GAAG,CAAC,CAAC;AACL,QAAM,mBAAmB,WAAS;AAChC,WAAO,MAAM,IAAI,UAAQ;AACvB,YAAM,WAAW,cAAc,KAAK,EAAE;AACtC,aAAO;AAAA,QACL,GAAG;AAAA,QACH,UAAU,YAAY,SAAS,SAAS,iBAAiB,QAAQ,IAAI,CAAC;AAAA,MACxE;AAAA,IACF,CAAC;AAAA,EACH;AACA,SAAO,iBAAiB,cAAc,GAAG,KAAK,CAAC,CAAC;AAClD;", "names": [] }