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> <!DOCTYPE html>
<html> <html>
<head> <head>
<script src="./index.js"></script> <script src="./index.ts"></script>
</head> </head>
<body> <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": "", "description": "",
"main": "index.js", "main": "index.js",
"scripts": { "scripts": {
"test": "echo \"Error: no test specified\" && exit 1" "test": "jest --watch"
}, },
"author": "", "author": "",
"license": "ISC", "license": "ISC",
"devDependencies": { "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" "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 export class Table
{ {
Name: string; Name: string;
Rows: Array<Array<string>>; Rows: Array<Array<string>>;
Columns: Array<Sum>;
Parent?: Table; Parent?: Table;
Children: Array<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.Name = inName;
this.Rows = inRows; this.Rows = inRows;
this.Parent = null; this.Parent = null;
this.Children = []; this.Children = [];
this.Columns = inColumns.map( (inIndex: number) => new Sum(inIndex, this) );
} }
Pivot(inColumn: number) Pivot(inColumnPivot: number)
{ {
var cell: string; var cell: string;
var child: Table; var child: Table;
var context: Table;
context = this; this.Rows.forEach((inRow: Array<string>)=>
this.Rows.forEach((inRow)=>
{ {
cell = inRow[inColumn]; cell = inRow[inColumnPivot];
child = this.Children.find( inChild => inChild.Name == cell ); child = this.Children.find( (inChild: Table) => inChild.Name == cell );
if(!child) if(!child)
{ {
child = new Table(cell, []); child = new Table(cell, [], this.Columns.map( (inColumn: Sum) => inColumn.ColumnIndex ));
child.Parent = context; child.Parent = this;
context.Children.push(child); this.Children.push(child);
} }
child.Rows.push(inRow); child.Rows.push(inRow);
}); });
this.Rows = []; this.Rows = [];
} }
PivotTree(inColumnsPivot: Array<number>)
PivotTree(inColumns: Array<number>)
{ {
var columns: Array<number>; var pivotsRemaining: Array<number>;
if(inColumns.length == 0) if(inColumnsPivot.length == 0)
{ {
return; return;
} }
columns = [...inColumns]; pivotsRemaining = [...inColumnsPivot];
this.Pivot(columns.shift()); this.Pivot(pivotsRemaining.shift());
this.Children.forEach( (inChild) => this.Children.forEach( (inChild: Table) => inChild.PivotTree(pivotsRemaining) );
}
ItrParents(inFunction: Function)
{ {
inChild.PivotTree(columns); if(this.Parent)
{
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) );
}
}
} }