From cd20dc93d07bde8d1e1221595cad8823860a3e1e Mon Sep 17 00:00:00 2001 From: "Toro_Unit (Hiroshi Urabe)" Date: Sat, 22 Dec 2018 20:58:00 +0900 Subject: [PATCH 1/5] add parent property, for no hierarchical term. --- packages/components/src/query-controls/terms.js | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/packages/components/src/query-controls/terms.js b/packages/components/src/query-controls/terms.js index cbeb9d0bb4b7a..5401db0d669e4 100644 --- a/packages/components/src/query-controls/terms.js +++ b/packages/components/src/query-controls/terms.js @@ -11,7 +11,13 @@ import { groupBy } from 'lodash'; * @return {Array} Array of terms in tree format. */ export function buildTermsTree( flatTerms ) { - const termsByParent = groupBy( flatTerms, 'parent' ); + const flatTermsWithParent = flatTerms.map( ( term ) => { + return { + parent: 0, + ...term, + }; + } ); + const termsByParent = groupBy( flatTermsWithParent, 'parent' ); const fillWithChildren = ( terms ) => { return terms.map( ( term ) => { const children = termsByParent[ term.id ]; From 58bf3dd399c4ff96807f2f3a75ed403b784bc50b Mon Sep 17 00:00:00 2001 From: "Toro_Unit (Hiroshi Urabe)" Date: Thu, 27 Dec 2018 15:25:58 +0900 Subject: [PATCH 2/5] update buildTermsTree in @wordpresss/editor --- packages/editor/src/utils/terms.js | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/packages/editor/src/utils/terms.js b/packages/editor/src/utils/terms.js index cbeb9d0bb4b7a..5401db0d669e4 100644 --- a/packages/editor/src/utils/terms.js +++ b/packages/editor/src/utils/terms.js @@ -11,7 +11,13 @@ import { groupBy } from 'lodash'; * @return {Array} Array of terms in tree format. */ export function buildTermsTree( flatTerms ) { - const termsByParent = groupBy( flatTerms, 'parent' ); + const flatTermsWithParent = flatTerms.map( ( term ) => { + return { + parent: 0, + ...term, + }; + } ); + const termsByParent = groupBy( flatTermsWithParent, 'parent' ); const fillWithChildren = ( terms ) => { return terms.map( ( term ) => { const children = termsByParent[ term.id ]; From 768c925a2845e6a2d52abccb159905238eb86616 Mon Sep 17 00:00:00 2001 From: "Toro_Unit (Hiroshi Urabe)" Date: Sun, 30 Dec 2018 19:09:51 +0900 Subject: [PATCH 3/5] update BuildTermsTree Test --- packages/editor/src/utils/test/terms.js | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/packages/editor/src/utils/test/terms.js b/packages/editor/src/utils/test/terms.js index 361cc4b414b7a..574615e1be1fc 100644 --- a/packages/editor/src/utils/test/terms.js +++ b/packages/editor/src/utils/test/terms.js @@ -4,10 +4,17 @@ import { buildTermsTree } from '../terms'; describe( 'buildTermsTree()', () => { - it( 'Should return empty array if parent is never specified.', () => { - const input = Object.freeze( [ { term: 2232 }, { term: 2245 } ] ); + it( 'Should return same array as input with zero parentand empty children added if parent is never specified.', () => { + const input = Object.freeze( [ + { id: 2232, dummy: true }, + { id: 2245, dummy: true }, + ] ); + const output = Object.freeze( [ + { id: 2232, parent: 0, children: [], dummy: true }, + { id: 2245, parent: 0, children: [], dummy: true }, + ] ); const termsTreem = buildTermsTree( input ); - expect( termsTreem ).toEqual( [] ); + expect( termsTreem ).toEqual( output ); } ); it( 'Should return same array as input with empty children added if all the elements are top level', () => { const input = Object.freeze( [ From c88c1dea8f4d4295e6861d82bed7edc50264bbef Mon Sep 17 00:00:00 2001 From: "Toro_Unit (Hiroshi Urabe)" Date: Sun, 30 Dec 2018 19:10:58 +0900 Subject: [PATCH 4/5] add test buildTermsTree in query-controls --- .../src/query-controls/test/terms.js | 61 +++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 packages/components/src/query-controls/test/terms.js diff --git a/packages/components/src/query-controls/test/terms.js b/packages/components/src/query-controls/test/terms.js new file mode 100644 index 0000000000000..574615e1be1fc --- /dev/null +++ b/packages/components/src/query-controls/test/terms.js @@ -0,0 +1,61 @@ +/** + * Internal dependencies + */ +import { buildTermsTree } from '../terms'; + +describe( 'buildTermsTree()', () => { + it( 'Should return same array as input with zero parentand empty children added if parent is never specified.', () => { + const input = Object.freeze( [ + { id: 2232, dummy: true }, + { id: 2245, dummy: true }, + ] ); + const output = Object.freeze( [ + { id: 2232, parent: 0, children: [], dummy: true }, + { id: 2245, parent: 0, children: [], dummy: true }, + ] ); + const termsTreem = buildTermsTree( input ); + expect( termsTreem ).toEqual( output ); + } ); + it( 'Should return same array as input with empty children added if all the elements are top level', () => { + const input = Object.freeze( [ + { id: 2232, parent: 0, dummy: true }, + { id: 2245, parent: 0, dummy: false }, + ] ); + const output = [ + { id: 2232, parent: 0, children: [], dummy: true }, + { id: 2245, parent: 0, children: [], dummy: false }, + ]; + const termsTreem = buildTermsTree( input ); + expect( termsTreem ).toEqual( output ); + } ); + it( 'Should return element with its child if a child exists', () => { + const input = Object.freeze( [ + { id: 2232, parent: 0 }, + { id: 2245, parent: 2232 }, + ] ); + const output = [ + { id: 2232, parent: 0, children: [ + { id: 2245, parent: 2232, children: [] }, + ] }, + ]; + const termsTreem = buildTermsTree( input ); + expect( termsTreem ).toEqual( output ); + } ); + it( 'Should return elements with multiple children and elements with no children', () => { + const input = Object.freeze( [ + { id: 2232, parent: 0 }, + { id: 2245, parent: 2232 }, + { id: 2249, parent: 0 }, + { id: 2246, parent: 2232 }, + ] ); + const output = [ + { id: 2232, parent: 0, children: [ + { id: 2245, parent: 2232, children: [] }, + { id: 2246, parent: 2232, children: [] }, + ] }, + { id: 2249, parent: 0, children: [] }, + ]; + const termsTreem = buildTermsTree( input ); + expect( termsTreem ).toEqual( output ); + } ); +} ); From 8f1002aad826d6219ee394b2b9b9894648cf3b7a Mon Sep 17 00:00:00 2001 From: "Toro_Unit (Hiroshi Urabe)" Date: Fri, 4 Jan 2019 08:23:56 +0900 Subject: [PATCH 5/5] If parent does not exist, set null instead of 0. --- packages/components/src/query-controls/terms.js | 11 ++++++++--- packages/components/src/query-controls/test/terms.js | 6 +++--- packages/editor/src/utils/terms.js | 11 ++++++++--- packages/editor/src/utils/test/terms.js | 6 +++--- 4 files changed, 22 insertions(+), 12 deletions(-) diff --git a/packages/components/src/query-controls/terms.js b/packages/components/src/query-controls/terms.js index 5401db0d669e4..ba76e68980184 100644 --- a/packages/components/src/query-controls/terms.js +++ b/packages/components/src/query-controls/terms.js @@ -11,13 +11,18 @@ import { groupBy } from 'lodash'; * @return {Array} Array of terms in tree format. */ export function buildTermsTree( flatTerms ) { - const flatTermsWithParent = flatTerms.map( ( term ) => { + const flatTermsWithParentAndChildren = flatTerms.map( ( term ) => { return { - parent: 0, + children: [], + parent: null, ...term, }; } ); - const termsByParent = groupBy( flatTermsWithParent, 'parent' ); + + const termsByParent = groupBy( flatTermsWithParentAndChildren, 'parent' ); + if ( termsByParent.null && termsByParent.null.length ) { + return flatTermsWithParentAndChildren; + } const fillWithChildren = ( terms ) => { return terms.map( ( term ) => { const children = termsByParent[ term.id ]; diff --git a/packages/components/src/query-controls/test/terms.js b/packages/components/src/query-controls/test/terms.js index 574615e1be1fc..11cf1505d4f27 100644 --- a/packages/components/src/query-controls/test/terms.js +++ b/packages/components/src/query-controls/test/terms.js @@ -4,14 +4,14 @@ import { buildTermsTree } from '../terms'; describe( 'buildTermsTree()', () => { - it( 'Should return same array as input with zero parentand empty children added if parent is never specified.', () => { + it( 'Should return same array as input with null parent and empty children added if parent is never specified.', () => { const input = Object.freeze( [ { id: 2232, dummy: true }, { id: 2245, dummy: true }, ] ); const output = Object.freeze( [ - { id: 2232, parent: 0, children: [], dummy: true }, - { id: 2245, parent: 0, children: [], dummy: true }, + { id: 2232, parent: null, children: [], dummy: true }, + { id: 2245, parent: null, children: [], dummy: true }, ] ); const termsTreem = buildTermsTree( input ); expect( termsTreem ).toEqual( output ); diff --git a/packages/editor/src/utils/terms.js b/packages/editor/src/utils/terms.js index 5401db0d669e4..ba76e68980184 100644 --- a/packages/editor/src/utils/terms.js +++ b/packages/editor/src/utils/terms.js @@ -11,13 +11,18 @@ import { groupBy } from 'lodash'; * @return {Array} Array of terms in tree format. */ export function buildTermsTree( flatTerms ) { - const flatTermsWithParent = flatTerms.map( ( term ) => { + const flatTermsWithParentAndChildren = flatTerms.map( ( term ) => { return { - parent: 0, + children: [], + parent: null, ...term, }; } ); - const termsByParent = groupBy( flatTermsWithParent, 'parent' ); + + const termsByParent = groupBy( flatTermsWithParentAndChildren, 'parent' ); + if ( termsByParent.null && termsByParent.null.length ) { + return flatTermsWithParentAndChildren; + } const fillWithChildren = ( terms ) => { return terms.map( ( term ) => { const children = termsByParent[ term.id ]; diff --git a/packages/editor/src/utils/test/terms.js b/packages/editor/src/utils/test/terms.js index 574615e1be1fc..11cf1505d4f27 100644 --- a/packages/editor/src/utils/test/terms.js +++ b/packages/editor/src/utils/test/terms.js @@ -4,14 +4,14 @@ import { buildTermsTree } from '../terms'; describe( 'buildTermsTree()', () => { - it( 'Should return same array as input with zero parentand empty children added if parent is never specified.', () => { + it( 'Should return same array as input with null parent and empty children added if parent is never specified.', () => { const input = Object.freeze( [ { id: 2232, dummy: true }, { id: 2245, dummy: true }, ] ); const output = Object.freeze( [ - { id: 2232, parent: 0, children: [], dummy: true }, - { id: 2245, parent: 0, children: [], dummy: true }, + { id: 2232, parent: null, children: [], dummy: true }, + { id: 2245, parent: null, children: [], dummy: true }, ] ); const termsTreem = buildTermsTree( input ); expect( termsTreem ).toEqual( output );