Compare commits

..

No commits in common. "feature/test-framework" and "master" have entirely different histories.

7 changed files with 39 additions and 6526 deletions

View File

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

16
index.js Normal file
View File

@ -0,0 +1,16 @@
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);

View File

@ -1,31 +0,0 @@
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,25 +4,11 @@
"description": "",
"main": "index.js",
"scripts": {
"test": "jest --watch"
"test": "echo \"Error: no test specified\" && exit 1"
},
"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": {}
}
}

View File

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