Skip to content

Commit 64b1005

Browse files
author
Josh Lory
committed
Smart defaults for flowerType and startDirection
Is there a benefit to requiring these to be specified every time? Otherwise the API is more friendly if we have sensible default values.
1 parent 41f2a54 commit 64b1005

File tree

3 files changed

+10
-36
lines changed

3 files changed

+10
-36
lines changed

src/bee.js

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,12 @@ const HONEY_SOUND = 'honey';
1313
const NECTAR_SOUND = 'nectar';
1414

1515
module.exports = class Bee extends Gatherer {
16-
constructor(maze, config) {
16+
constructor(maze, config = {}) {
1717
super(maze, config);
18+
const {level} = config;
1819

19-
this.defaultFlowerColor_ = (config.level.flowerType === 'redWithNectar' ?
20-
'red' : 'purple');
21-
if (this.defaultFlowerColor_ === 'purple' &&
22-
config.level.flowerType !== 'purpleNectarHidden') {
23-
throw new Error(`bad flowerType for Bee: ${config.level.flowerType}`);
24-
}
20+
this.defaultFlowerColor_ = level && level.flowerType === 'redWithNectar' ?
21+
'red' : 'purple';
2522

2623
// at each location, tracks whether user checked to see if it was a flower or
2724
// honeycomb using an if block

src/subtype.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,12 @@ const TILE_SHAPES = {
3131
};
3232

3333
module.exports = class Subtype extends EventEmitter {
34-
constructor(maze, config) {
34+
constructor(maze, {skin, level} = {}) {
3535
super();
3636

3737
this.maze_ = maze;
38-
this.skin_ = config.skin;
39-
this.level_ = config.level;
40-
this.startDirection = config.level.startDirection;
38+
this.skin_ = skin;
39+
this.startDirection = level ? level.startDirection : 0;
4140
}
4241

4342
/**

test/unit/bee.test.js

Lines changed: 3 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1+
/* global describe, it, expect */
2+
13
import Bee from '../../src/bee';
24
import BeeCell from '../../src/beeCell';
35
import MazeMap from '../../src/mazeMap';
46

5-
var baseLevel = {
7+
const baseLevel = {
68
honeyGoal: 1,
79
map: [
810
[0]
@@ -15,30 +17,6 @@ var baseLevel = {
1517
};
1618

1719
describe("Bee", function () {
18-
it("fails if no flowerType", function () {
19-
var maze = {};
20-
var config = {
21-
level: baseLevel
22-
};
23-
delete config.level.flowerType;
24-
expect(() => {
25-
new Bee(maze, config);
26-
}).toThrowError(/bad flowerType for Bee/);
27-
});
28-
29-
30-
it("fails if invalid flowerType", function () {
31-
var maze = {};
32-
var config = {
33-
level: Object.assign(baseLevel, {
34-
flowerType: 'invalid'
35-
})
36-
};
37-
expect(() => {
38-
new Bee(maze, config);
39-
}).toThrowError(/bad flowerType for Bee/);
40-
});
41-
4220
describe("isRedFlower", function () {
4321
/**
4422
* Shim a 1x1 maze with the given values and validate that we get the

0 commit comments

Comments
 (0)