forked from NatLabRockies/nodehaystack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHGrid.js
More file actions
154 lines (136 loc) · 3.66 KB
/
HGrid.js
File metadata and controls
154 lines (136 loc) · 3.66 KB
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
//
// Copyright (c) 2015, Shawn Jacobson
// Licensed under the Academic Free License version 3.0
//
// Ported from @see {@link https://bitbucket.org/brianfrank/haystack-java|Haystack Java Toolkit}
//
// History:
// 21 Mar 2015 Shawn Jacobson Creation
//
var HRow = require('./HRow');
/**
* HGrid is an immutable two dimension data structure of cols and rows.
* Use HGridBuilder to construct a HGrid instance.
* @see {@link http://project-haystack.org/doc/Grids|Project Haystack}
*
* @constructor
* @param {HDict} dict
* @param {HCol[]} cols
* @param {array} rowList
*/
function HGrid(dict, cols, rowList) {
this.dict = dict;
this.cols = cols;
if (typeof(dict) === 'undefined' || dict === null) throw new Error("metadata cannot be null");
var i;
this.rows = [];
for (i = 0; i < rowList.length; ++i) {
var cells = rowList[i];
if (cols.length !== cells.length) throw new Error("Row cells size != cols size");
this.rows[i] = new HRow(this, cells);
}
this.colsByName = {};
for (i = 0; i < cols.length; ++i) {
var col = cols[i];
var colName = col.name();
var cn = this.colsByName[colName];
if (typeof(cn) !== 'undefined' && cn !== null) throw new Error("Duplicate col name: " + colName);
this.colsByName[colName] = col;
}
}
module.exports = HGrid;
var HCol = require('./HCol'),
HDict = require('./HDict'),
HZincWriter = require('./io/HZincWriter');
/**
* Empty grid with one column called "empty" and zero rows
*/
HGrid.EMPTY = new HGrid(HDict.EMPTY, [new HCol(0, "empty", HDict.EMPTY)], []);
//////////////////////////////////////////////////////////////////////////
// Access
//////////////////////////////////////////////////////////////////////////
/**
* Return grid level meta
* @return {HDict}
*/
HGrid.prototype.meta = function() {
return this.dict;
};
/**
* Error grid have the dict.err marker tag
* @return {boolean}
*/
HGrid.prototype.isErr = function() {
return this.dict.has("err");
};
/**
* Return if number of rows is zero
* @return {boolean}
*/
HGrid.prototype.isEmpty = function() {
return this.numRows() === 0;
};
/**
* Return number of rows
* @return {int}
*/
HGrid.prototype.numRows = function() {
return this.rows.length;
};
/**
* Get a row by its zero based index
* @return {HRow}
*/
HGrid.prototype.row = function(row) {
return this.rows[row];
};
/**
* Get number of columns
* @return {int}
*/
HGrid.prototype.numCols = function() {
return this.cols.length;
};
/**
* Get a column by name. If not found and checked if false then
* return null, otherwise throw UnknownNameException
* @return {HCol}
*/
HGrid.prototype.col = function(name, checked) {
// Get a column by its index
if (typeof(name) === 'number') return this.cols[name];
var _checked = checked;
if (typeof(_checked) === 'undefined') _checked = true;
var col = this.colsByName[name];
if (typeof(col) !== 'undefined' && col !== null) return col;
if (_checked) throw new Error(name);
return null;
};
/**
* Create iteratator to walk each row
* @return {iterator}
*/
HGrid.prototype.iterator = function() {
var pos = 0;
var r = this.rows;
return {
next: function() {
if (this.hasNext()) return r[pos++];
throw new Error("No Such Element");
},
hasNext: function() {
return pos < r.length;
}
};
};
//////////////////////////////////////////////////////////////////////////
// Debug
//////////////////////////////////////////////////////////////////////////
/** Debug dump - this is Zinc right now. */
HGrid.prototype.dump = function(out) {
var _out = out;
if (typeof(_out) === 'undefined') _out = console;
HZincWriter.gridToString(this, function(err, str) {
_out.log(str);
});
};