forked from hgwood/hash-code-tooling
-
Notifications
You must be signed in to change notification settings - Fork 0
/
grid-utils.test.js
63 lines (56 loc) · 1.7 KB
/
grid-utils.test.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
/* eslint-env mocha */
const assert = require("assert");
const _ = require("lodash");
const gridUtils = require("./grid-utils");
describe("grid utils", function() {
describe("cells", function() {
it("returns all cells of the grid, with coordinates", function() {
const cells = gridUtils.cells([[1, 2, 3], [4, 5, 6]]);
assert.deepEqual(cells, [
{ x: 0, y: 0, value: 1 },
{ x: 1, y: 0, value: 2 },
{ x: 2, y: 0, value: 3 },
{ x: 0, y: 1, value: 4 },
{ x: 1, y: 1, value: 5 },
{ x: 2, y: 1, value: 6 }
]);
});
});
describe("each", function() {
it("iterates", function() {
const iterated = [];
gridUtils.forEach([[1, 2], [3, 4]], (value, x, y) =>
iterated.push({ x, y, value })
);
assert.deepEqual(iterated, [
{ x: 0, y: 0, value: 1 },
{ x: 1, y: 0, value: 2 },
{ x: 0, y: 1, value: 3 },
{ x: 1, y: 1, value: 4 }
]);
});
});
describe("map", function() {
it("produces a new grid", function() {
const grid = [];
assert.notStrictEqual(gridUtils.map(grid, _.identity), grid);
});
it("transforms every value in the grid using f", function() {
const grid = [[1, 2], [3, 4]];
const newGrid = gridUtils.map(grid, i => i + 1);
assert.deepEqual(newGrid, [[2, 3], [4, 5]]);
});
});
describe("transpose", function() {
it("transposes a square", function() {
assert.deepEqual(gridUtils.transpose([[1, 2], [3, 4]]), [[1, 3], [2, 4]]);
});
it("transposes a rectangle", function() {
assert.deepEqual(gridUtils.transpose([[1, 2, 3], [4, 5, 6]]), [
[1, 4],
[2, 5],
[3, 6]
]);
});
});
});