From 3dad9c1013d1aa14d5974922e165fd37ff756520 Mon Sep 17 00:00:00 2001 From: unknown Date: Tue, 10 Sep 2019 21:12:52 -0400 Subject: [PATCH] summation columns created in pivoting --- index.js | 6 ++---- table.ts | 44 ++++++++++++++++++++++++-------------------- 2 files changed, 26 insertions(+), 24 deletions(-) diff --git a/index.js b/index.js index 9b7b620..910ba38 100644 --- a/index.js +++ b/index.js @@ -12,8 +12,6 @@ rows = [ ["b","3"], ]; -table = new Table("Root", rows); +table = new Table("Root", rows, [1]); table.PivotTree([0, 1]); -table.ItrLeaves( inLeaf => console.log(inLeaf) ); - -console.log(table, leaves); \ No newline at end of file +table.ItrLeaves( inLeaf => console.log(inLeaf) ); \ No newline at end of file diff --git a/table.ts b/table.ts index 1f3f196..674af9f 100644 --- a/table.ts +++ b/table.ts @@ -8,9 +8,9 @@ export class Sum FromChildren: number; FromOutside: number; - constructor() + constructor(inIndex: number) { - this.ColumnIndex = 0; + this.ColumnIndex = inIndex; } } @@ -18,52 +18,49 @@ export class Table { Name: string; Rows: Array>; + Columns: Array; Parent?: Table; Children: Array; - constructor(inName: string, inRows: Array>) + constructor(inName: string, inRows: Array>, inColumns: Array) { this.Name = inName; this.Rows = inRows; this.Parent = null; this.Children = []; + this.Columns = inColumns.map( (inIndex: number) => new Sum(inIndex) ); } - Pivot(inColumn: number) + Pivot(inColumnPivot: number) { var cell: string; var child: Table; - var context: Table; - context = this; this.Rows.forEach((inRow: Array)=> { - cell = inRow[inColumn]; + cell = inRow[inColumnPivot]; child = this.Children.find( (inChild: Table) => inChild.Name == cell ); if(!child) { - child = new Table(cell, []); - child.Parent = context; - context.Children.push(child); + child = new Table(cell, [], this.Columns.map( (inColumn: Sum) => inColumn.ColumnIndex )); + child.Parent = this; + this.Children.push(child); } child.Rows.push(inRow); }); this.Rows = []; } - PivotTree(inColumns: Array) + PivotTree(inColumnsPivot: Array) { - var columns: Array; + var pivotsRemaining: Array; - if(inColumns.length == 0) + if(inColumnsPivot.length == 0) { return; } - columns = [...inColumns]; - this.Pivot(columns.shift()); - this.Children.forEach( (inChild: Table) => - { - inChild.PivotTree(columns); - }); + pivotsRemaining = [...inColumnsPivot]; + this.Pivot(pivotsRemaining.shift()); + this.Children.forEach( (inChild: Table) => inChild.PivotTree(pivotsRemaining) ); } ItrParents(inFunction: Function) @@ -93,4 +90,11 @@ export class Table this.Children.forEach( (inChild: Table) => inChild.ItrLeaves(inFunction) ); } } -} \ No newline at end of file +} + +export const Iterators = { + SumRows: (inTable: Table) => + { + + } +}; \ No newline at end of file