From fd835a4d87e8ebeae1010192b47c7ad37708686c Mon Sep 17 00:00:00 2001 From: unknown Date: Sat, 7 Sep 2019 13:05:47 -0400 Subject: [PATCH] iterators started --- index.js | 15 +++++++++------ table.ts | 41 +++++++++++++++++++++++++++++++++++++---- 2 files changed, 46 insertions(+), 10 deletions(-) diff --git a/index.js b/index.js index 3f1cb2b..9b7b620 100644 --- a/index.js +++ b/index.js @@ -1,16 +1,19 @@ import {Table} from './table.ts'; +var table; +var leaves; +var rows; -var rows = [ +rows = [ ["a","1"], ["b","2"], ["a","3"], ["b","1"], ["a","2"], ["b","3"], -] +]; -var t; -t = new Table("Root", rows); -t.PivotTree([0, 1]); +table = new Table("Root", rows); +table.PivotTree([0, 1]); +table.ItrLeaves( inLeaf => console.log(inLeaf) ); -console.log(t); \ No newline at end of file +console.log(table, leaves); \ No newline at end of file diff --git a/table.ts b/table.ts index a0ffd21..7d3b2aa 100644 --- a/table.ts +++ b/table.ts @@ -1,3 +1,17 @@ +export class Sum +{ + ColumnIndex: number; + Original: number; + FromParents: number; + FromChildren: number; + FromOutside: number; + + constructor() + { + this.ColumnIndex = 0; + } +} + export class Table { Name: string; @@ -20,10 +34,10 @@ export class Table var context: Table; context = this; - this.Rows.forEach((inRow)=> + this.Rows.forEach((inRow: Array)=> { cell = inRow[inColumn]; - child = this.Children.find( inChild => inChild.Name == cell ); + child = this.Children.find( (inChild: Table) => inChild.Name == cell ); if(!child) { child = new Table(cell, []); @@ -34,7 +48,6 @@ export class Table }); this.Rows = []; } - PivotTree(inColumns: Array) { var columns: Array; @@ -45,9 +58,29 @@ export class Table } columns = [...inColumns]; this.Pivot(columns.shift()); - this.Children.forEach( (inChild) => + this.Children.forEach( (inChild: Table) => { inChild.PivotTree(columns); }); } + + ItrParents(inFunction: Function) + { + if(this.Parent) + { + inFunction(this.Parent); + this.Parent.ItrParents(inFunction); + } + } + ItrLeaves(inFunction:Function) + { + if(this.Children.length == 0) + { + inFunction(this); + } + else + { + this.Children.forEach( (inChild: Table) => inChild.ItrLeaves(inFunction) ); + } + } } \ No newline at end of file