Compare commits

..

6 Commits

Author SHA1 Message Date
unknown
bc218ceecd typescript, es6, jest working 2019-10-30 22:43:25 -04:00
unknown
03bc94ff81 Sum adjustment values started 2019-10-30 20:26:25 -04:00
unknown
49d82eaf86 initialize summation working 2019-09-10 21:52:01 -04:00
unknown
3dad9c1013 summation columns created in pivoting 2019-09-10 21:12:52 -04:00
unknown
d9c05ba321 child iterator 2019-09-10 20:31:41 -04:00
unknown
fd835a4d87 iterators started 2019-09-07 13:05:47 -04:00
7 changed files with 6525 additions and 38 deletions

View File

@ -1,7 +1,7 @@
<!DOCTYPE html>
<html>
<head>
<script src="./index.js"></script>
<script src="./index.ts"></script>
</head>
<body>

View File

@ -1,16 +0,0 @@
import {Table} from './table.ts';
var rows = [
["a","1"],
["b","2"],
["a","3"],
["b","1"],
["a","2"],
["b","3"],
]
var t;
t = new Table("Root", rows);
t.PivotTree([0, 1]);
console.log(t);

31
index.ts Normal file
View File

@ -0,0 +1,31 @@
import {Table, Sum} from './table';
var table: Table;
var rows: Array<Array<string>>;
rows = [
["a","1"],
["b","2"],
["a","3"],
["b","1"],
["a","2"],
["b","3"],
];
table = new Table("Root", rows, [1]);
table.PivotTree([0, 1]);
table.ItrLeaves( (inTable: Table) =>
{
inTable.Columns.forEach( (inColumn: Sum, inIndex: number) =>
{
inTable.Rows.forEach( (inRow: Array<string>) =>
{
inColumn.Original += parseFloat(inRow[inColumn.ColumnIndex]);
});
inTable.ItrParents( (inParent: Table)=>
{
inParent.Columns[inIndex].Original += inColumn.Original;
});
});
} );
table.ItrChildren( inTable => console.log(inTable) );

6387
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -4,11 +4,25 @@
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
"test": "jest --watch"
},
"author": "",
"license": "ISC",
"devDependencies": {
"@babel/core": "^7.6.4",
"@babel/preset-env": "^7.6.3",
"@babel/preset-typescript": "^7.6.0",
"@types/jest": "^24.0.21",
"babel-jest": "^24.9.0",
"jest": "^24.9.0",
"ts-jest": "^24.1.0",
"typescript": "^3.6.2"
}
},
"babel": {
"presets": [
"@babel/preset-env",
"@babel/preset-typescript"
]
},
"dependencies": {}
}

26
table.test.ts Normal file
View File

@ -0,0 +1,26 @@
import {Table, Sum} from './table';
describe("table library", ()=>
{
var table: Table;
var rows: Array<Array<string>>;
var summationColumns: Array<number>;
rows = [
["a","1"],
["b","2"],
["a","3"],
["b","1"],
["a","2"],
["b","3"],
];
summationColumns = [1];
table = new Table("Root", rows, summationColumns);
it("should create a table", ()=>
{
expect(table.Name).toEqual("Root");
expect(table.Rows).toEqual(rows);
});
})

View File

@ -1,53 +1,98 @@
export class Sum
{
ColumnIndex: number;
Table: Table;
Original: number;
Adjustment: number;
FromParents: number;
FromChildren: number;
FromOutside: number;
constructor(inIndex: number, inTable: Table)
{
this.ColumnIndex = inIndex;
this.Table = inTable;
this.Original = 0;
this.Adjustment = 1;
this.FromParents = 1;
}
}
export class Table
{
Name: string;
Rows: Array<Array<string>>;
Columns: Array<Sum>;
Parent?: Table;
Children: Array<Table>;
constructor(inName: string, inRows: Array<Array<string>>)
constructor(inName: string, inRows: Array<Array<string>>, inColumns: Array<number>)
{
this.Name = inName;
this.Rows = inRows;
this.Parent = null;
this.Children = [];
this.Columns = inColumns.map( (inIndex: number) => new Sum(inIndex, this) );
}
Pivot(inColumn: number)
Pivot(inColumnPivot: number)
{
var cell: string;
var child: Table;
var context: Table;
context = this;
this.Rows.forEach((inRow)=>
this.Rows.forEach((inRow: Array<string>)=>
{
cell = inRow[inColumn];
child = this.Children.find( inChild => inChild.Name == cell );
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<number>)
PivotTree(inColumnsPivot: Array<number>)
{
var columns: Array<number>;
var pivotsRemaining: Array<number>;
if(inColumns.length == 0)
if(inColumnsPivot.length == 0)
{
return;
}
columns = [...inColumns];
this.Pivot(columns.shift());
this.Children.forEach( (inChild) =>
pivotsRemaining = [...inColumnsPivot];
this.Pivot(pivotsRemaining.shift());
this.Children.forEach( (inChild: Table) => inChild.PivotTree(pivotsRemaining) );
}
ItrParents(inFunction: Function)
{
if(this.Parent)
{
inChild.PivotTree(columns);
inFunction(this.Parent, this);
this.Parent.ItrParents(inFunction);
}
}
ItrChildren(inFunction: Function)
{
this.Children.forEach( (inChild: Table) =>
{
inFunction(inChild, this);
inChild.ItrChildren(inFunction);
});
}
ItrLeaves(inFunction:Function)
{
if(this.Children.length == 0)
{
inFunction(this);
}
else
{
this.Children.forEach( (inChild: Table) => inChild.ItrLeaves(inFunction) );
}
}
}