Skip to content

Commit e08bbed

Browse files
committed
set up simple dependency injection
1 parent a4ff20f commit e08bbed

File tree

5 files changed

+147
-102
lines changed

5 files changed

+147
-102
lines changed

src/hypercube/ArrayCube.js

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
class ArrayCube {
2+
/**
3+
* Create an ArrayCube
4+
* @param {Object} config
5+
* @param {Array} config.dimensions
6+
* @param {Array} config.members
7+
*/
8+
constructor(config) {
9+
this.config = {
10+
dimensions: [],
11+
members: [],
12+
...config,
13+
};
14+
this.cube = this.createNDimArray(config.dimensions);
15+
}
16+
17+
/**
18+
* helper to recursively create an n-dimensional array implementation of a cube
19+
* @param {Array} dimensions
20+
* @returns {Array} n-dimensional array cube
21+
*/
22+
createCube(dimensions) {
23+
var dim = dimensions[0];
24+
var rest = dimensions.slice(1);
25+
var cube = new Array();
26+
for (var i = 0; i < dim; i++) {
27+
cube[i] = createNDimArray(rest);
28+
}
29+
return cube;
30+
}
31+
32+
/**
33+
* helper to recursively retrieve an element from the cube
34+
* @param {Array} indices
35+
* @returns {Array} n-dimensional cube slice
36+
*/
37+
getElement(indices) {
38+
if (!indices || indices.length == 0) {
39+
return this.cube;
40+
} else {
41+
return getElement(this.cube[indices[0]], indices.slice(1));
42+
}
43+
}
44+
45+
/**
46+
* slice - analyze data for a single dimension
47+
* example: what were the sales in June of last year?
48+
*/
49+
slice(dimension, filter) {
50+
let points = [];
51+
let data = [];
52+
53+
const dimensionIndex = this.config.dimensions.indexOf(dimension);
54+
55+
if (dimensionIndex === -1) {
56+
throw new TypeError('dimension not found', dimension);
57+
}
58+
59+
this.points.forEach((point, i) => {
60+
// Add slice if it matches given filter.
61+
if (point[dimensionIndex] === filter) {
62+
data.push(this.data[i]);
63+
points.push(this.points[i]);
64+
}
65+
});
66+
67+
return new Table(Object.assign({}, structure, { points, data }));
68+
}
69+
70+
/**
71+
* dice - analyize data for more than one dimension
72+
* example: what were our sales in June of last year in New York state?
73+
*/
74+
dice() {}
75+
76+
/**
77+
* drillUp - analyze data at most summarized level
78+
* example: what were our sales last year?
79+
*/
80+
drillUp() {}
81+
82+
/**
83+
* drillDown - analyze data at most detailed level
84+
* example: what were our sales last year?
85+
*/
86+
drillDown() {}
87+
88+
/**
89+
* rollUp - analyze summary information for one or more dimensions
90+
* example: what were our monthly totals?
91+
*/
92+
rollUp() {}
93+
94+
/**
95+
* pivot - rotate data to provide alternative presentation
96+
* example: what were our monthly totals for each product category?
97+
*/
98+
pivot() {}
99+
}
100+
101+
module.exports = ArrayCube;

src/hypercube/HyperCube.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
const InputValidator = require('./InputValidator');
2+
3+
/**
4+
* Create a HyperCube with dependencies
5+
* @param {Class} CubeImpl
6+
*/
7+
const HyperCube = CubeImpl => {
8+
return class HyperCube {
9+
/**
10+
* Construct the HyperCube
11+
* @param {Object} config
12+
* @param {Array} config.dimensions
13+
* @param {Array} config.members
14+
*/
15+
constructor(config) {
16+
InputValidator.validateConfig(config);
17+
this.cube = new CubeImpl(config);
18+
}
19+
};
20+
};
21+
22+
module.exports = HyperCube;

src/hypercube/InputValidator.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class InputValidator {
2+
/**
3+
* Validates cube constructor configuration
4+
* @param {Object} config
5+
* @param {Array} config.dimensions
6+
* @param {Array} config.members
7+
* @throws {Error} if no dimensions are specified
8+
* @throws {Error} if no members are specified
9+
*/
10+
static validateConfig(config) {
11+
const { dimensions, members } = config;
12+
if (!dimensions || dimensions.length === 0) {
13+
throw new Error('Cannot construct ArrayCube without dimensions.');
14+
}
15+
if (!members || members.length === 0) {
16+
throw new Error('Cannot construct ArrayCube without members.');
17+
}
18+
}
19+
}
20+
21+
module.exports = InputValidator;

src/hypercube/index.js

Lines changed: 3 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -1,77 +1,4 @@
1-
const { createNDimArray } = require('./util');
1+
const HyperCube = require('./HyperCube');
2+
const ArrayCube = require('./ArrayCube');
23

3-
class Hypercube {
4-
/**
5-
* Create a hypercube.
6-
* @param {Object} config
7-
* @param {Array} config.dimensions
8-
* @param {Array} config.members
9-
*/
10-
constructor(config) {
11-
this.config = Object.assign(
12-
{
13-
dimensions: [],
14-
members: [],
15-
},
16-
config,
17-
);
18-
this.cube = createNDimArray();
19-
}
20-
21-
/**
22-
* slice - analyze data for a single dimension
23-
* example: what were the sales in June of last year?
24-
*/
25-
slice(dimension, filter) {
26-
let points = [];
27-
let data = [];
28-
29-
const dimensionIndex = this.config.dimensions.indexOf(dimension);
30-
31-
if (dimensionIndex === -1) {
32-
throw new TypeError('dimension not found', dimension);
33-
}
34-
35-
this.points.forEach((point, i) => {
36-
// Add slice if it matches given filter.
37-
if (point[dimensionIndex] === filter) {
38-
data.push(this.data[i]);
39-
points.push(this.points[i]);
40-
}
41-
});
42-
43-
return new Table(Object.assign({}, structure, { points, data }));
44-
}
45-
46-
/**
47-
* dice - analyize data for more than one dimension
48-
* example: what were our sales in June of last year in New York state?
49-
*/
50-
dice() {}
51-
52-
/**
53-
* drillUp - analyze data at most summarized level
54-
* example: what were our sales last year?
55-
*/
56-
drillUp() {}
57-
58-
/**
59-
* drillDown - analyze data at most detailed level
60-
* example: what were our sales last year?
61-
*/
62-
drillDown() {}
63-
64-
/**
65-
* rollUp - analyze summary information for one or more dimensions
66-
* example: what were our monthly totals?
67-
*/
68-
rollUp() {}
69-
70-
/**
71-
* pivot - rotate data to provide alternative presentation
72-
* example: what were our monthly totals for each product category?
73-
*/
74-
pivot() {}
75-
}
76-
77-
module.exports = Hypercube;
4+
module.exports = HyperCube(ArrayCube);

src/hypercube/nDimArray.js

Lines changed: 0 additions & 26 deletions
This file was deleted.

0 commit comments

Comments
 (0)