Compare commits
6 Commits
master
...
feature/te
Author | SHA1 | Date | |
---|---|---|---|
|
bc218ceecd | ||
|
03bc94ff81 | ||
|
49d82eaf86 | ||
|
3dad9c1013 | ||
|
d9c05ba321 | ||
|
fd835a4d87 |
@ -1,7 +1,7 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<script src="./index.js"></script>
|
||||
<script src="./index.ts"></script>
|
||||
</head>
|
||||
<body>
|
||||
|
||||
|
16
index.js
16
index.js
@ -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
31
index.ts
Normal 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
6387
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
18
package.json
18
package.json
@ -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
26
table.test.ts
Normal 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);
|
||||
});
|
||||
})
|
81
table.ts
81
table.ts
@ -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)
|
||||
{
|
||||
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) );
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user