-
Notifications
You must be signed in to change notification settings - Fork 0
/
dataset.js
114 lines (94 loc) · 2.74 KB
/
dataset.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
var TempTable = require('./temptable.js'),
debug = require('debug'),
log = debug('n4p:dataset');
function Dataset(iDatasetNm,iJsonObj,iDateFormat) {
//log( "ds create" );
this.dateFormat = iDateFormat;
this.dataset = null;
this.metaSchema = null;
this.name = "";
this.rootName = "";
this.tempTables = {};
this.getDataset(iDatasetNm,iJsonObj);
if( this.name === "" ){
throw new Error( "Dataset " + iDatasetNm + " not found" );
}
}
Dataset.prototype.$ = function(ttName){
log( "ds:$", ttName );
var targetTable = null;
//var datasetContents = this.dataset[this.name];
var datasetContents=null;
for(var prop in this.dataset){
datasetContents=this.dataset[prop];
break;
}
var ttNm = "";
for(var tt in datasetContents){
if(tt.toString().toLowerCase() == ttName.toLowerCase()){
targetTable = datasetContents[tt];
ttNm=tt;
break;
}
}
if(! this.tempTables[ttNm]){
this.tempTables[ttNm]=new TempTable(this,ttNm,targetTable,this.metaSchema[ttNm],this.dateFormat);
}
return this.tempTables[ttNm];
};
Dataset.prototype.copyDataset = function(empty){
log( "ds:copyDataset" );
var copyDatasetJsonObj = {},
copyDataset;
copyDatasetJsonObj[ this.rootName ] = JSON.parse( this.writeJson() );
copyDatasetJsonObj[ this.rootName ][ this.name + "MetaSchema" ] = JSON.parse( JSON.stringify( this.metaSchema ) );
copyDataset = new Dataset( this.name, copyDatasetJsonObj,this.dateFormat );
if(empty){
copyDataset.emptyDataset();
}
return copyDataset;
};
Dataset.prototype.emptyDataset = function(){
log( "ds:emptyDataset" );
for(var prop in this.dataset[this.rootName]){
this.$(prop).emptyTempTable();
}
/*
this.dataset[this.rootName].forEach( function( item, prop ) {
this.$( prop ).emptyTemptable();
}, this );
*/
};
Dataset.prototype.getDataset = function(iDatasetNm,iJsonObj){
log( "ds:getDataset", iDatasetNm );
var prop,
prop2;
for(prop in iJsonObj){
if( iJsonObj[ prop ][ iDatasetNm ] && iJsonObj[ prop ][ iDatasetNm + "MetaSchema" ] ) {
this.dataset = iJsonObj[ prop ];
this.metaSchema = iJsonObj[ prop ][ iDatasetNm + "MetaSchema" ];
this.name = prop.toString();
for(prop2 in this.dataset){
this.rootName = prop2;
break;
}
break;
}
if( typeof iJsonObj[ prop ] == "object" ){
this.getDataset( iDatasetNm, iJsonObj[ prop ] );
}
}
};
Dataset.prototype.writeJson = function(){
log( "ds:writeJson" );
var writeJson = "",
jsonObj = {};
if( this.dataset ){
jsonObj[ this.rootName ] = this.dataset[ this.rootName ];
writeJson = JSON.stringify( jsonObj );
}
return writeJson;
};
module.exports = function( name, obj,dateFormat ) {
return new Dataset( name, obj, dateFormat );
};