Skip to content

Commit 9e296df

Browse files
committed
Merge pull request #3 from possibly/master
forEach, getRow, getColumn, README updates!
2 parents af1b549 + 2e92742 commit 9e296df

File tree

3 files changed

+129
-1
lines changed

3 files changed

+129
-1
lines changed

README.md

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@ If the coordinates are *not* valid an `Error` is thrown.
2121
### get(x, y)
2222
Returns the value of the given coordinate. The coordinate is checked using `validateCoords`.
2323

24+
### getRow(rowNumber)
25+
Returns an array of the requested row.
26+
27+
### getColumn(colNumber)
28+
Returns an array of the requested column.
29+
2430
### set(x, y, value)
2531
Sets the value of the given coordinate to `value`. The coordinate is checked using `validateCoords`.
2632

@@ -30,5 +36,42 @@ Returns the height of the array.
3036
### getWidth()
3137
Returns the width of the array.
3238

33-
### getNeighbours(x, y)
39+
### getNeighbours(x, y, [, distance])
3440
Returns an array containing all values of the cells next to the given coordinate.
41+
42+
For example, distance not set:
43+
```
44+
[ ][ ][ ][ ][ ]
45+
[ ][*][*][*][ ]
46+
[ ][*][X][*][ ]
47+
[ ][*][*][*][ ]
48+
[ ][ ][ ][ ][ ]
49+
```
50+
51+
The given coordinate is marked with an `X`. The function will return an array containing the values for the fields marked with an `*`.
52+
53+
Example, distance = 2:
54+
```
55+
[*][*][*][*][*]
56+
[*][*][*][*][*]
57+
[*][*][X][*][*]
58+
[*][*][*][*][*]
59+
[*][*][*][*][*]
60+
```
61+
62+
The function will return an array containing the values for the fields marked with an '*'. Notice that distance will change what cells count as neighbors.
63+
64+
### forEach(fn, [, thisArg])
65+
Executes a provided function once per array element.
66+
67+
`fn` is the function to execute for each element, taking three arguments:
68+
69+
currentValue
70+
The current element being processed in the array.
71+
index
72+
The object index, {x: x, y: y}, of the current element being processed in the 2d array.
73+
array
74+
The Fixed2DArray that forEach is being applied to.
75+
76+
thisArg
77+
Optional. Value to use as this when executing callback.

lib/fixed-2d-array.js

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,65 @@ Fixed2DArray.prototype.getWidth = function getWidth() {
3232
return this._width;
3333
};
3434

35+
/**
36+
* Returns the requested row from the grid.
37+
*
38+
* @param rowNumber {number} number of the row where top left is (0,0).
39+
*/
40+
Fixed2DArray.prototype.getRow = function getRow(rowNumber){
41+
try{
42+
this.validateCoords(0,rowNumber);
43+
} catch(err){
44+
throw new Error('fixed-2d-array: row number '+rowNumber+' is not valid.' +
45+
' The size of this array is ('+this._width+'/'+this._height+')');
46+
}
47+
48+
return this._grid[rowNumber];
49+
};
50+
51+
/**
52+
* Returns the requested column from the grid.
53+
*
54+
* @param colNumber {number} number of the column where top left is (0,0).
55+
*/
56+
Fixed2DArray.prototype.getColumn = function getRow(colNumber){
57+
try{
58+
this.validateCoords(colNumber,0);
59+
} catch(err){
60+
throw new Error('fixed-2d-array: row number '+colNumber+' is not valid.' +
61+
' The size of this array is ('+this._width+'/'+this._height+')');
62+
}
63+
64+
var returnArray = [];
65+
66+
for (var i = 0; i < this._height; i++) {
67+
returnArray.push(this._grid[i][colNumber]);
68+
}
69+
return returnArray;
70+
};
71+
72+
/**
73+
* Executes a provided function once per array element.
74+
*
75+
* @param fn {function} Function to execute for each element, taking three arguments:
76+
*
77+
* currentValue
78+
* The current element being processed in the array.
79+
* index
80+
* The object index, {x: x, y: y}, of the current element being processed in the 2d array.
81+
* array
82+
* The Fixed2DArray that forEach is being applied to.
83+
*
84+
* @param thisArg Optional. Value to use as this when executing callback.
85+
*/
86+
Fixed2DArray.prototype.forEach = function forEach(fn, thisArg){
87+
for (var i = 0; i < this._width; i++) {
88+
for (var j = 0; j < this._height; j++) {
89+
fn.call(thisArg, this._grid[i][j], {x: i, y: j}, this);
90+
}
91+
}
92+
};
93+
3594
/**
3695
* Throws an Error if the given coordinate is invalid.
3796
*

test/test.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,32 @@ test('get and set', function (t) {
2323
t.equal(fa.get(9,7),'This is a string!');
2424
});
2525

26+
test('get correct row and column',function (t) {
27+
t.plan(6);
28+
var fa = new fixedArray(10,10);
29+
30+
t.equal(fa.getRow(0).length,10);
31+
t.equal(fa.getColumn(1).length,10);
32+
33+
fa.set(0,1,'This is a string!');
34+
t.equal(fa.getRow(0)[1],'This is a string!');
35+
t.equal(fa.getColumn(1)[0],'This is a string!');
36+
t.throws(function(){fa.getRow(-1);});
37+
t.throws(function(){fa.getColumn(11);});
38+
});
39+
40+
test('forEach',function (t) {
41+
t.plan(1);
42+
var fa = new fixedArray(10,10,0);
43+
44+
function fun(currentValue, index, array){
45+
array.set(index.x, index.y, currentValue+1);
46+
}
47+
48+
fa.forEach(fun);
49+
t.equal(fa.get(0,0),1);
50+
});
51+
2652
test('exception on index out of bounds', function (t) {
2753
t.plan(3);
2854
var fa = new fixedArray(10,10);

0 commit comments

Comments
 (0)