From d35c4b73ded67892eb270c45150905c17b9370d5 Mon Sep 17 00:00:00 2001 From: v-janwil Date: Fri, 27 Apr 2018 15:56:22 -0700 Subject: [PATCH 1/5] add depth check to oneNoteItemUtils Add two methods to check depth of notebooks and sectinos in oneNoteItemUtils --- src/oneNoteDataStructures/oneNoteItemUtils.ts | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/src/oneNoteDataStructures/oneNoteItemUtils.ts b/src/oneNoteDataStructures/oneNoteItemUtils.ts index 991a9e4..31a5324 100644 --- a/src/oneNoteDataStructures/oneNoteItemUtils.ts +++ b/src/oneNoteDataStructures/oneNoteItemUtils.ts @@ -6,6 +6,8 @@ import { Polyfills } from '../polyfills'; Polyfills.find(); +export type noteBookOrSectionGroup = Notebook | SectionGroup; + export class OneNoteItemUtils { /** * Given the id of the OneNoteItem, and a notebook or sectionGroup list, returns @@ -98,6 +100,38 @@ export class OneNoteItemUtils { return ancestry; } + + /** + * Finds the maximum depth of notebooks list, including sections + */ + getDepthOfNotebooks(notebooks: Notebook[]): number { + if (!notebooks || notebooks.length === 0) { + return 0; + } + return notebooks.map((notebook) => this.getDepthOfParent(notebook)).reduce((d1, d2) => Math.max(d1, d2)); + } + + /** + * Finds the maximum depth of SectionGroup or Notebook, + * includes the number of sections below it + */ + getDepthOfParent(parent: noteBookOrSectionGroup ): number { + if (!parent) { + return 0; + } + + let containsAtLeastOneSection = parent.sections && parent.sections.length > 0; + let maxDepth = containsAtLeastOneSection ? 1 : 0; + + if (parent.sectionGroups) { + for (let i = 0; i < parent.sections.length; i++) { + maxDepth = Math.max(this.getDepthOfParent(parent.sectionGroups[i]), maxDepth); + } + } + + // Include the parent itself + return maxDepth + 1; + } } export * from './oneNoteApiResponseTransformer'; From e82640157ef636db30da5d723baf87587514b604 Mon Sep 17 00:00:00 2001 From: v-janwil Date: Sat, 28 Apr 2018 12:18:29 -0700 Subject: [PATCH 2/5] Add tests, fix variable name Add tests, fix variable name --- src/oneNoteDataStructures/oneNoteItemUtils.ts | 6 +- .../oneNoteItemUtils.spec.ts | 126 ++++++++++++++++++ 2 files changed, 129 insertions(+), 3 deletions(-) diff --git a/src/oneNoteDataStructures/oneNoteItemUtils.ts b/src/oneNoteDataStructures/oneNoteItemUtils.ts index 31a5324..79e1e61 100644 --- a/src/oneNoteDataStructures/oneNoteItemUtils.ts +++ b/src/oneNoteDataStructures/oneNoteItemUtils.ts @@ -6,7 +6,7 @@ import { Polyfills } from '../polyfills'; Polyfills.find(); -export type noteBookOrSectionGroup = Notebook | SectionGroup; +export type notebookOrSectionGroup = Notebook | SectionGroup; export class OneNoteItemUtils { /** @@ -104,7 +104,7 @@ export class OneNoteItemUtils { /** * Finds the maximum depth of notebooks list, including sections */ - getDepthOfNotebooks(notebooks: Notebook[]): number { + static getDepthOfNotebooks(notebooks: Notebook[]): number { if (!notebooks || notebooks.length === 0) { return 0; } @@ -115,7 +115,7 @@ export class OneNoteItemUtils { * Finds the maximum depth of SectionGroup or Notebook, * includes the number of sections below it */ - getDepthOfParent(parent: noteBookOrSectionGroup ): number { + private static getDepthOfParent(parent: notebookOrSectionGroup ): number { if (!parent) { return 0; } diff --git a/test/oneNoteDataStructures/oneNoteItemUtils.spec.ts b/test/oneNoteDataStructures/oneNoteItemUtils.spec.ts index 6fbcaf4..71c7f24 100644 --- a/test/oneNoteDataStructures/oneNoteItemUtils.spec.ts +++ b/test/oneNoteDataStructures/oneNoteItemUtils.spec.ts @@ -1,9 +1,82 @@ import { Notebook } from '../../src/oneNoteDataStructures/notebook'; import { OneNoteItem } from '../../src/oneNoteDataStructures/oneNoteItem'; +import { SectionGroup } from '../../src/oneNoteDataStructures/sectionGroup'; import { OneNoteItemUtils } from '../../src/oneNoteDataStructures/oneNoteItemUtils'; import { Section } from '../../src/oneNoteDataStructures/section'; describe('OneNoteItemUtils', () => { + const notebook1: Notebook = { + parent: undefined, + id: 'notebook', + name: 'Notebook', + expanded: false, + sectionGroups: [], + sections: [], + apiUrl: '', + webUrl: '', + }; + + const notebook2: Notebook = { + parent: undefined, + id: 'notebook', + name: 'Notebook', + expanded: false, + sectionGroups: [], + sections: [], + apiUrl: '', + webUrl: '', + }; + + const section1: Section = { + parent: undefined, + id: 'child', + name: 'Child', + expanded: false, + pages: undefined, + apiUrl: '', + }; + + const section2: Section = { + parent: undefined, + id: 'child', + name: 'Child', + expanded: false, + pages: undefined, + apiUrl: '', + }; + + const sectionGroup: SectionGroup = { + parent: notebook2, + id: 'child', + name: 'Child', + expanded: false, + sectionGroups: [], + sections: [], + apiUrl: '' + }; + + const sectionGroupWithTwoSections: SectionGroup = { + parent: notebook2, + id: 'child', + name: 'Child', + expanded: false, + sectionGroups: [], + sections: [], + apiUrl: '' + }; + sectionGroupWithTwoSections.sections.push(section1, section2); + + const stackedSectionGroup: SectionGroup = { + parent: notebook2, + id: 'child', + name: 'Child', + expanded: false, + sectionGroups: [], + sections: [], + apiUrl: '' + }; + stackedSectionGroup.sectionGroups.push(sectionGroup); + it('find should return the item that matches the predicate if it is found early in the hierarchy', () => { const notebook: Notebook = { parent: undefined, @@ -217,4 +290,57 @@ describe('OneNoteItemUtils', () => { const ancestry = OneNoteItemUtils.getAncestry(item); expect(ancestry).toEqual([grandparent, parent, item]); }); + + it('getDepthOfNotebooks should return a max depth of 1 if there is one notebook with no other children', () => { + const depthOfNotebooks = OneNoteItemUtils.getDepthOfNotebooks([notebook1]); + expect(depthOfNotebooks).toEqual(1); + }); + + it('getDepthOfNotebooks should return a max depth of 1 if there are two notebooks with no other children', () => { + const depthOfNotebooks = OneNoteItemUtils.getDepthOfNotebooks([notebook1, notebook2]); + expect(depthOfNotebooks).toEqual(1); + }); + + it('getDepthOfNotebooks returns a max depth of 2 when there are two notebooks where one has sections', () => { + section1.parent = notebook1; + notebook1.sections.push(section1); + + const depthOfNotebooks = OneNoteItemUtils.getDepthOfNotebooks([notebook1, notebook2]); + expect(depthOfNotebooks).toEqual(2); + }); + + it('getDepthOfNotebook should return a max depth of 2 when there are two notebooks where one has an empty section group', () => { + notebook1.sectionGroups.push(sectionGroup); + + const depthOfNotebooks = OneNoteItemUtils.getDepthOfNotebooks([notebook1, notebook2]); + expect(depthOfNotebooks).toEqual(2); + }); + + it('getDepthOfNotebooks returns a max depth of 3 when there are two notebooks where one has a section group with a section', () => { + sectionGroup.sections.push(section1); + notebook1.sectionGroups.push(sectionGroup); + + const depthOfNotebooks = OneNoteItemUtils.getDepthOfNotebooks([notebook1, notebook2]); + expect(depthOfNotebooks).toEqual(3); + }); + + it('getDepthOfNotebooks returns a max depth of three when there are two notebooks where one has a section group with multiple sections', () => { + notebook1.sectionGroups.push(sectionGroupWithTwoSections); + + const depthOfNotebooks = OneNoteItemUtils.getDepthOfNotebooks([notebook1, notebook2]); + expect(depthOfNotebooks).toEqual(3); + }); + + it('getDepthOfNotebooks returns the correct depth when there are two notebooks with a more complicated tree structure', () => { + notebook1.sectionGroups.push(sectionGroupWithTwoSections); + notebook2.sectionGroups.push(stackedSectionGroup); + + const depthOfNotebooks = OneNoteItemUtils.getDepthOfNotebooks([notebook1, notebook2]); + expect(depthOfNotebooks).toEqual(3); + }); + + it('getDepthOfNotebooks returns a max depth of 0 if it is given an empty array of Notebooks', () => { + expect(OneNoteItemUtils.getDepthOfNotebooks([])).toEqual(0); + }); + }); From b249c398362850693e9fe2a7d9644328632e7dc4 Mon Sep 17 00:00:00 2001 From: v-janwil Date: Sat, 28 Apr 2018 17:25:06 -0700 Subject: [PATCH 3/5] Add getPathFromNotebooksToSection and tests Add getPathFromNotebooksToSection and tests --- src/oneNoteDataStructures/oneNoteItemUtils.ts | 53 +++++++++ .../oneNoteItemUtils.spec.ts | 103 +++++++++++++++--- 2 files changed, 139 insertions(+), 17 deletions(-) diff --git a/src/oneNoteDataStructures/oneNoteItemUtils.ts b/src/oneNoteDataStructures/oneNoteItemUtils.ts index 79e1e61..8d5cf7c 100644 --- a/src/oneNoteDataStructures/oneNoteItemUtils.ts +++ b/src/oneNoteDataStructures/oneNoteItemUtils.ts @@ -7,6 +7,7 @@ import { Polyfills } from '../polyfills'; Polyfills.find(); export type notebookOrSectionGroup = Notebook | SectionGroup; +export type SectionPathElement = notebookOrSectionGroup | Section; export class OneNoteItemUtils { /** @@ -132,6 +133,58 @@ export class OneNoteItemUtils { // Include the parent itself return maxDepth + 1; } + + /** + * Retrieves the path starting from the notebook to the first ancestor section found that + * meets a given criteria. + */ + static getPathFromNotebooksToSection(notebooks: Notebook[], filter: (s: Section) => boolean): SectionPathElement[] | undefined { + if (!notebooks || !filter) { + return undefined; + } + + for (let i = 0; i < notebooks.length; i++) { + let notebook = notebooks[i]; + let notebookSearchResult = this.getPathFromParentToSection(notebook, filter); + if (notebookSearchResult) { + return notebookSearchResult; + } + } + + return undefined; + } + + /** + * Recursively retrieves the path starting from the specified parent to the first ancestor + * section found that meets a given criteria. + */ + static getPathFromParentToSection(parent: notebookOrSectionGroup, filter: (s: Section) => boolean): SectionPathElement[] | undefined{ + if (!parent || !filter) { + return undefined; + } + + if (parent.sections) { + for (let i = 0; i < parent.sections.length; i++) { + let section = parent.sections[i]; + if (filter(section)) { + return [parent, section]; + } + } + } + + if (parent.sectionGroups) { + for (let i = 0; i < parent.sectionGroups.length; i++) { + let sectionGroup = parent.sectionGroups[i]; + let sectionGroupSearchResult = this.getPathFromParentToSection(sectionGroup, filter); + if (sectionGroupSearchResult) { + sectionGroupSearchResult.unshift(parent); + return sectionGroupSearchResult; + } + } + } + + return undefined; + } } export * from './oneNoteApiResponseTransformer'; diff --git a/test/oneNoteDataStructures/oneNoteItemUtils.spec.ts b/test/oneNoteDataStructures/oneNoteItemUtils.spec.ts index 71c7f24..803243a 100644 --- a/test/oneNoteDataStructures/oneNoteItemUtils.spec.ts +++ b/test/oneNoteDataStructures/oneNoteItemUtils.spec.ts @@ -1,15 +1,15 @@ import { Notebook } from '../../src/oneNoteDataStructures/notebook'; import { OneNoteItem } from '../../src/oneNoteDataStructures/oneNoteItem'; import { SectionGroup } from '../../src/oneNoteDataStructures/sectionGroup'; -import { OneNoteItemUtils } from '../../src/oneNoteDataStructures/oneNoteItemUtils'; +import { OneNoteItemUtils, SectionPathElement } from '../../src/oneNoteDataStructures/oneNoteItemUtils'; import { Section } from '../../src/oneNoteDataStructures/section'; describe('OneNoteItemUtils', () => { const notebook1: Notebook = { parent: undefined, - id: 'notebook', - name: 'Notebook', - expanded: false, + id: 'notebook1', + name: 'Notebook1', + expanded: true, sectionGroups: [], sections: [], apiUrl: '', @@ -18,8 +18,8 @@ describe('OneNoteItemUtils', () => { const notebook2: Notebook = { parent: undefined, - id: 'notebook', - name: 'Notebook', + id: 'notebook2', + name: 'Notebook2', expanded: false, sectionGroups: [], sections: [], @@ -29,8 +29,8 @@ describe('OneNoteItemUtils', () => { const section1: Section = { parent: undefined, - id: 'child', - name: 'Child', + id: 'section1', + name: 'Section1', expanded: false, pages: undefined, apiUrl: '', @@ -38,17 +38,26 @@ describe('OneNoteItemUtils', () => { const section2: Section = { parent: undefined, - id: 'child', - name: 'Child', + id: 'section1', + name: 'Section1', expanded: false, pages: undefined, apiUrl: '', }; + + const expandedSection: Section = { + parent: undefined, + id: 'expandedSection', + name: 'ExpandedSection', + expanded: true, + pages: undefined, + apiUrl: '', + }; const sectionGroup: SectionGroup = { parent: notebook2, - id: 'child', - name: 'Child', + id: 'sg1', + name: 'sg1', expanded: false, sectionGroups: [], sections: [], @@ -57,8 +66,8 @@ describe('OneNoteItemUtils', () => { const sectionGroupWithTwoSections: SectionGroup = { parent: notebook2, - id: 'child', - name: 'Child', + id: 'sg2', + name: 'Sg2', expanded: false, sectionGroups: [], sections: [], @@ -68,8 +77,8 @@ describe('OneNoteItemUtils', () => { const stackedSectionGroup: SectionGroup = { parent: notebook2, - id: 'child', - name: 'Child', + id: 'sg3', + name: 'sg3', expanded: false, sectionGroups: [], sections: [], @@ -77,6 +86,14 @@ describe('OneNoteItemUtils', () => { }; stackedSectionGroup.sectionGroups.push(sectionGroup); + const idPath = (path: SectionPathElement[] | undefined): string => { + if (path === undefined) { + return ''; + } else { + return path.map((elem) => elem.id).join(); + } + }; + it('find should return the item that matches the predicate if it is found early in the hierarchy', () => { const notebook: Notebook = { parent: undefined, @@ -304,7 +321,7 @@ describe('OneNoteItemUtils', () => { it('getDepthOfNotebooks returns a max depth of 2 when there are two notebooks where one has sections', () => { section1.parent = notebook1; notebook1.sections.push(section1); - + const depthOfNotebooks = OneNoteItemUtils.getDepthOfNotebooks([notebook1, notebook2]); expect(depthOfNotebooks).toEqual(2); }); @@ -343,4 +360,56 @@ describe('OneNoteItemUtils', () => { expect(OneNoteItemUtils.getDepthOfNotebooks([])).toEqual(0); }); + it('getPathFromNotebooksToSection generates the correct path when there is one notebook and one section', () => { + notebook1.sections.push(expandedSection); + + const path = OneNoteItemUtils.getPathFromNotebooksToSection([notebook1], s => s.expanded); + expect(idPath(path)).toEqual('notebook1,expandedSection'); + }); + + it('getPathFromNotebooksToSection generates correct path when there is one notebook, one section group and one section', () => { + sectionGroup.sections.push(expandedSection); + notebook1.sectionGroups.push(sectionGroup); + + const path = OneNoteItemUtils.getPathFromNotebooksToSection([notebook1], s => s.expanded); + expect(idPath(path)).toEqual('notebook1,sectionGroup,expandedSection'); + }); + + it('getPathFromNotebooksToSection generates correct path when there is one notebook, one section group, and one section that is not in the group', () => { + notebook1.sectionGroups.push(sectionGroup); + notebook1.sections.push(expandedSection); + + const path = OneNoteItemUtils.getPathFromNotebooksToSection([notebook1], s => s.expanded); + expect(idPath(path)).toEqual('notebook1,expandedSection'); + }); + + it('getPathFromNotebooksToSection generates correct path when the section is in a section group that is in another section group and in a notebook', () => { + const sgInside: SectionGroup = { + parent: notebook2, + id: 'sgInside', + name: 'SgInside', + expanded: false, + sectionGroups: [], + sections: [], + apiUrl: '' + }; + sgInside.sections.push(expandedSection); + + const sgOutside: SectionGroup = { + parent: notebook2, + id: 'sgOutside', + name: 'sgOutside', + expanded: false, + sectionGroups: [], + sections: [], + apiUrl: '' + }; + sgOutside.sectionGroups.push(sgInside); + + notebook1.sectionGroups.push(sgOutside); + + const path = OneNoteItemUtils.getPathFromNotebooksToSection([notebook1], s => s.expanded); + expect(idPath(path)).toEqual('notebook1,sgOutside,sgInside,expandedSection'); + }); + }); From 64f4fa33a905576416b52785bd3a8aa3c9c0fa44 Mon Sep 17 00:00:00 2001 From: janehazen Date: Mon, 30 Apr 2018 15:05:35 -0700 Subject: [PATCH 4/5] Change oneNoteItemUtils and tests Use GetAncestry instead of NotebookParentPath, fix tests --- src/oneNoteDataStructures/oneNoteItemUtils.ts | 54 +----------- .../oneNoteItemUtils.spec.ts | 82 +++---------------- 2 files changed, 13 insertions(+), 123 deletions(-) diff --git a/src/oneNoteDataStructures/oneNoteItemUtils.ts b/src/oneNoteDataStructures/oneNoteItemUtils.ts index 8d5cf7c..64d71b2 100644 --- a/src/oneNoteDataStructures/oneNoteItemUtils.ts +++ b/src/oneNoteDataStructures/oneNoteItemUtils.ts @@ -125,7 +125,7 @@ export class OneNoteItemUtils { let maxDepth = containsAtLeastOneSection ? 1 : 0; if (parent.sectionGroups) { - for (let i = 0; i < parent.sections.length; i++) { + for (let i = 0; i < parent.sectionGroups.length; i++) { maxDepth = Math.max(this.getDepthOfParent(parent.sectionGroups[i]), maxDepth); } } @@ -133,58 +133,6 @@ export class OneNoteItemUtils { // Include the parent itself return maxDepth + 1; } - - /** - * Retrieves the path starting from the notebook to the first ancestor section found that - * meets a given criteria. - */ - static getPathFromNotebooksToSection(notebooks: Notebook[], filter: (s: Section) => boolean): SectionPathElement[] | undefined { - if (!notebooks || !filter) { - return undefined; - } - - for (let i = 0; i < notebooks.length; i++) { - let notebook = notebooks[i]; - let notebookSearchResult = this.getPathFromParentToSection(notebook, filter); - if (notebookSearchResult) { - return notebookSearchResult; - } - } - - return undefined; - } - - /** - * Recursively retrieves the path starting from the specified parent to the first ancestor - * section found that meets a given criteria. - */ - static getPathFromParentToSection(parent: notebookOrSectionGroup, filter: (s: Section) => boolean): SectionPathElement[] | undefined{ - if (!parent || !filter) { - return undefined; - } - - if (parent.sections) { - for (let i = 0; i < parent.sections.length; i++) { - let section = parent.sections[i]; - if (filter(section)) { - return [parent, section]; - } - } - } - - if (parent.sectionGroups) { - for (let i = 0; i < parent.sectionGroups.length; i++) { - let sectionGroup = parent.sectionGroups[i]; - let sectionGroupSearchResult = this.getPathFromParentToSection(sectionGroup, filter); - if (sectionGroupSearchResult) { - sectionGroupSearchResult.unshift(parent); - return sectionGroupSearchResult; - } - } - } - - return undefined; - } } export * from './oneNoteApiResponseTransformer'; diff --git a/test/oneNoteDataStructures/oneNoteItemUtils.spec.ts b/test/oneNoteDataStructures/oneNoteItemUtils.spec.ts index 803243a..c9a86f1 100644 --- a/test/oneNoteDataStructures/oneNoteItemUtils.spec.ts +++ b/test/oneNoteDataStructures/oneNoteItemUtils.spec.ts @@ -27,6 +27,17 @@ describe('OneNoteItemUtils', () => { webUrl: '', }; + const expandedNotebook: Notebook = { + parent: undefined, + id: 'notebook2', + name: 'Notebook2', + expanded: false, + sectionGroups: [], + sections: [], + apiUrl: '', + webUrl: '', + }; + const section1: Section = { parent: undefined, id: 'section1', @@ -44,15 +55,6 @@ describe('OneNoteItemUtils', () => { pages: undefined, apiUrl: '', }; - - const expandedSection: Section = { - parent: undefined, - id: 'expandedSection', - name: 'ExpandedSection', - expanded: true, - pages: undefined, - apiUrl: '', - }; const sectionGroup: SectionGroup = { parent: notebook2, @@ -86,14 +88,6 @@ describe('OneNoteItemUtils', () => { }; stackedSectionGroup.sectionGroups.push(sectionGroup); - const idPath = (path: SectionPathElement[] | undefined): string => { - if (path === undefined) { - return ''; - } else { - return path.map((elem) => elem.id).join(); - } - }; - it('find should return the item that matches the predicate if it is found early in the hierarchy', () => { const notebook: Notebook = { parent: undefined, @@ -352,7 +346,7 @@ describe('OneNoteItemUtils', () => { notebook1.sectionGroups.push(sectionGroupWithTwoSections); notebook2.sectionGroups.push(stackedSectionGroup); - const depthOfNotebooks = OneNoteItemUtils.getDepthOfNotebooks([notebook1, notebook2]); + const depthOfNotebooks = OneNoteItemUtils.getDepthOfNotebooks([notebook1, expandedNotebook]); expect(depthOfNotebooks).toEqual(3); }); @@ -360,56 +354,4 @@ describe('OneNoteItemUtils', () => { expect(OneNoteItemUtils.getDepthOfNotebooks([])).toEqual(0); }); - it('getPathFromNotebooksToSection generates the correct path when there is one notebook and one section', () => { - notebook1.sections.push(expandedSection); - - const path = OneNoteItemUtils.getPathFromNotebooksToSection([notebook1], s => s.expanded); - expect(idPath(path)).toEqual('notebook1,expandedSection'); - }); - - it('getPathFromNotebooksToSection generates correct path when there is one notebook, one section group and one section', () => { - sectionGroup.sections.push(expandedSection); - notebook1.sectionGroups.push(sectionGroup); - - const path = OneNoteItemUtils.getPathFromNotebooksToSection([notebook1], s => s.expanded); - expect(idPath(path)).toEqual('notebook1,sectionGroup,expandedSection'); - }); - - it('getPathFromNotebooksToSection generates correct path when there is one notebook, one section group, and one section that is not in the group', () => { - notebook1.sectionGroups.push(sectionGroup); - notebook1.sections.push(expandedSection); - - const path = OneNoteItemUtils.getPathFromNotebooksToSection([notebook1], s => s.expanded); - expect(idPath(path)).toEqual('notebook1,expandedSection'); - }); - - it('getPathFromNotebooksToSection generates correct path when the section is in a section group that is in another section group and in a notebook', () => { - const sgInside: SectionGroup = { - parent: notebook2, - id: 'sgInside', - name: 'SgInside', - expanded: false, - sectionGroups: [], - sections: [], - apiUrl: '' - }; - sgInside.sections.push(expandedSection); - - const sgOutside: SectionGroup = { - parent: notebook2, - id: 'sgOutside', - name: 'sgOutside', - expanded: false, - sectionGroups: [], - sections: [], - apiUrl: '' - }; - sgOutside.sectionGroups.push(sgInside); - - notebook1.sectionGroups.push(sgOutside); - - const path = OneNoteItemUtils.getPathFromNotebooksToSection([notebook1], s => s.expanded); - expect(idPath(path)).toEqual('notebook1,sgOutside,sgInside,expandedSection'); - }); - }); From 408466cd6b6c11193136df6f25679ffdf7a3fdfd Mon Sep 17 00:00:00 2001 From: janehazen Date: Mon, 30 Apr 2018 16:36:28 -0700 Subject: [PATCH 5/5] Take out depthCheck method Edit oneNoteItemUtils --- src/oneNoteDataStructures/oneNoteItemUtils.ts | 35 ----- .../oneNoteItemUtils.spec.ts | 137 +----------------- 2 files changed, 1 insertion(+), 171 deletions(-) diff --git a/src/oneNoteDataStructures/oneNoteItemUtils.ts b/src/oneNoteDataStructures/oneNoteItemUtils.ts index 64d71b2..991a9e4 100644 --- a/src/oneNoteDataStructures/oneNoteItemUtils.ts +++ b/src/oneNoteDataStructures/oneNoteItemUtils.ts @@ -6,9 +6,6 @@ import { Polyfills } from '../polyfills'; Polyfills.find(); -export type notebookOrSectionGroup = Notebook | SectionGroup; -export type SectionPathElement = notebookOrSectionGroup | Section; - export class OneNoteItemUtils { /** * Given the id of the OneNoteItem, and a notebook or sectionGroup list, returns @@ -101,38 +98,6 @@ export class OneNoteItemUtils { return ancestry; } - - /** - * Finds the maximum depth of notebooks list, including sections - */ - static getDepthOfNotebooks(notebooks: Notebook[]): number { - if (!notebooks || notebooks.length === 0) { - return 0; - } - return notebooks.map((notebook) => this.getDepthOfParent(notebook)).reduce((d1, d2) => Math.max(d1, d2)); - } - - /** - * Finds the maximum depth of SectionGroup or Notebook, - * includes the number of sections below it - */ - private static getDepthOfParent(parent: notebookOrSectionGroup ): number { - if (!parent) { - return 0; - } - - let containsAtLeastOneSection = parent.sections && parent.sections.length > 0; - let maxDepth = containsAtLeastOneSection ? 1 : 0; - - if (parent.sectionGroups) { - for (let i = 0; i < parent.sectionGroups.length; i++) { - maxDepth = Math.max(this.getDepthOfParent(parent.sectionGroups[i]), maxDepth); - } - } - - // Include the parent itself - return maxDepth + 1; - } } export * from './oneNoteApiResponseTransformer'; diff --git a/test/oneNoteDataStructures/oneNoteItemUtils.spec.ts b/test/oneNoteDataStructures/oneNoteItemUtils.spec.ts index c9a86f1..a01a7da 100644 --- a/test/oneNoteDataStructures/oneNoteItemUtils.spec.ts +++ b/test/oneNoteDataStructures/oneNoteItemUtils.spec.ts @@ -1,92 +1,10 @@ import { Notebook } from '../../src/oneNoteDataStructures/notebook'; import { OneNoteItem } from '../../src/oneNoteDataStructures/oneNoteItem'; import { SectionGroup } from '../../src/oneNoteDataStructures/sectionGroup'; -import { OneNoteItemUtils, SectionPathElement } from '../../src/oneNoteDataStructures/oneNoteItemUtils'; +import { OneNoteItemUtils } from '../../src/oneNoteDataStructures/oneNoteItemUtils'; import { Section } from '../../src/oneNoteDataStructures/section'; describe('OneNoteItemUtils', () => { - const notebook1: Notebook = { - parent: undefined, - id: 'notebook1', - name: 'Notebook1', - expanded: true, - sectionGroups: [], - sections: [], - apiUrl: '', - webUrl: '', - }; - - const notebook2: Notebook = { - parent: undefined, - id: 'notebook2', - name: 'Notebook2', - expanded: false, - sectionGroups: [], - sections: [], - apiUrl: '', - webUrl: '', - }; - - const expandedNotebook: Notebook = { - parent: undefined, - id: 'notebook2', - name: 'Notebook2', - expanded: false, - sectionGroups: [], - sections: [], - apiUrl: '', - webUrl: '', - }; - - const section1: Section = { - parent: undefined, - id: 'section1', - name: 'Section1', - expanded: false, - pages: undefined, - apiUrl: '', - }; - - const section2: Section = { - parent: undefined, - id: 'section1', - name: 'Section1', - expanded: false, - pages: undefined, - apiUrl: '', - }; - - const sectionGroup: SectionGroup = { - parent: notebook2, - id: 'sg1', - name: 'sg1', - expanded: false, - sectionGroups: [], - sections: [], - apiUrl: '' - }; - - const sectionGroupWithTwoSections: SectionGroup = { - parent: notebook2, - id: 'sg2', - name: 'Sg2', - expanded: false, - sectionGroups: [], - sections: [], - apiUrl: '' - }; - sectionGroupWithTwoSections.sections.push(section1, section2); - - const stackedSectionGroup: SectionGroup = { - parent: notebook2, - id: 'sg3', - name: 'sg3', - expanded: false, - sectionGroups: [], - sections: [], - apiUrl: '' - }; - stackedSectionGroup.sectionGroups.push(sectionGroup); it('find should return the item that matches the predicate if it is found early in the hierarchy', () => { const notebook: Notebook = { @@ -301,57 +219,4 @@ describe('OneNoteItemUtils', () => { const ancestry = OneNoteItemUtils.getAncestry(item); expect(ancestry).toEqual([grandparent, parent, item]); }); - - it('getDepthOfNotebooks should return a max depth of 1 if there is one notebook with no other children', () => { - const depthOfNotebooks = OneNoteItemUtils.getDepthOfNotebooks([notebook1]); - expect(depthOfNotebooks).toEqual(1); - }); - - it('getDepthOfNotebooks should return a max depth of 1 if there are two notebooks with no other children', () => { - const depthOfNotebooks = OneNoteItemUtils.getDepthOfNotebooks([notebook1, notebook2]); - expect(depthOfNotebooks).toEqual(1); - }); - - it('getDepthOfNotebooks returns a max depth of 2 when there are two notebooks where one has sections', () => { - section1.parent = notebook1; - notebook1.sections.push(section1); - - const depthOfNotebooks = OneNoteItemUtils.getDepthOfNotebooks([notebook1, notebook2]); - expect(depthOfNotebooks).toEqual(2); - }); - - it('getDepthOfNotebook should return a max depth of 2 when there are two notebooks where one has an empty section group', () => { - notebook1.sectionGroups.push(sectionGroup); - - const depthOfNotebooks = OneNoteItemUtils.getDepthOfNotebooks([notebook1, notebook2]); - expect(depthOfNotebooks).toEqual(2); - }); - - it('getDepthOfNotebooks returns a max depth of 3 when there are two notebooks where one has a section group with a section', () => { - sectionGroup.sections.push(section1); - notebook1.sectionGroups.push(sectionGroup); - - const depthOfNotebooks = OneNoteItemUtils.getDepthOfNotebooks([notebook1, notebook2]); - expect(depthOfNotebooks).toEqual(3); - }); - - it('getDepthOfNotebooks returns a max depth of three when there are two notebooks where one has a section group with multiple sections', () => { - notebook1.sectionGroups.push(sectionGroupWithTwoSections); - - const depthOfNotebooks = OneNoteItemUtils.getDepthOfNotebooks([notebook1, notebook2]); - expect(depthOfNotebooks).toEqual(3); - }); - - it('getDepthOfNotebooks returns the correct depth when there are two notebooks with a more complicated tree structure', () => { - notebook1.sectionGroups.push(sectionGroupWithTwoSections); - notebook2.sectionGroups.push(stackedSectionGroup); - - const depthOfNotebooks = OneNoteItemUtils.getDepthOfNotebooks([notebook1, expandedNotebook]); - expect(depthOfNotebooks).toEqual(3); - }); - - it('getDepthOfNotebooks returns a max depth of 0 if it is given an empty array of Notebooks', () => { - expect(OneNoteItemUtils.getDepthOfNotebooks([])).toEqual(0); - }); - });