forked from NatLabRockies/nodehaystack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHRow.js
More file actions
102 lines (95 loc) · 2.55 KB
/
HRow.js
File metadata and controls
102 lines (95 loc) · 2.55 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
//
// 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 HDict = require('./HDict'),
HCol = require('./HCol');
/**
* HRow is a row in a HGrid. It implements the HDict interface also.
* @see <a href='http://project-haystack.org/doc/Grids'>Project Haystack</a>
*
* @constructor
* @extends {HDict}
* @param {HGrid} grid
* @param {HVal[]} cells
*/
function HRow(grid, cells) {
this.ugrid = grid;
this.cells = cells;
}
HRow.prototype = Object.create(HDict.prototype);
module.exports = HRow;
/**
* Get the grid associated with this row
* @return {HGrid}
*/
HRow.prototype.grid = function() {
return this.ugrid;
};
/**
* Number of columns in grid (which may map to null cells)
* @return {int}
*/
HRow.prototype.size = function() {
return this.ugrid.cols.length;
};
/**
* Get a cell by column. If cell is null then raise
* Error or return null based on checked flag.
* @param {string|HCol} col
* @param {boolean} checked
* @return {HVal}
*/
HRow.prototype.get = function(col, checked) {
var _checked = checked;
if (typeof(_checked) === 'undefined') _checked = true;
if (col instanceof HCol) {
var val = this.cells[col.index];
if (typeof(val) !== 'undefined' && val !== null) return val;
if (_checked) throw new Error(col.name());
return null;
} else {
// Get a cell by column name
var name = col;
var col = this.ugrid.col(name, false);
if (col !== null) {
var val = this.cells[col.index];
if (typeof(val) !== 'undefined' && val !== null) return val;
}
if (_checked) throw new Error(name);
return null;
}
};
/**
* Return Map.Entry name/value iterator which only includes non-null cells
* @return {iterator}
*/
HRow.prototype.iterator = function() {
var col = 0;
for (; col < this.ugrid.cols.length; ++col) {
if (typeof(this.cells[col]) !== 'undefined' && this.cells[col] !== null)
break;
}
var grid = this.ugrid;
var cells = this.cells;
return {
next: function() {
if (col >= grid.cols.length) throw new Error("No Such Element");
var name = grid.col(col).name();
var val = cells[col];
for (col++; col < grid.cols.length; ++col) {
if (typeof(cells[col]) !== 'undefined' && cells[col] !== null)
break;
}
return new HDict.MapEntry(name, val);
},
hasNext: function() {
return col < grid.cols.length;
}
};
};