Skip to content

Commit

Permalink
Refactored deprecated random shape selection
Browse files Browse the repository at this point in the history
Improved random shape selection fixing an issue where deprecated shapes were being returned when randomly selecting a shape.
  • Loading branch information
ashiguruma committed Dec 19, 2016
1 parent e4ae5d9 commit e8a2a28
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 17 deletions.
2 changes: 1 addition & 1 deletion examples/single.html
Expand Up @@ -57,7 +57,7 @@
pattern.draw('cross-dash', '#2ca02c', undefined, patternSize),
pattern.draw('dot-dash', '#bcbd22', undefined, patternSize),
pattern.draw('dot', '#d62728', undefined, patternSize),
pattern.draw('disc', '#17becf', undefined, patternSize),
pattern.draw('circle', '#17becf', undefined, patternSize),
pattern.draw('ring', '#9467bd', undefined, patternSize),
pattern.draw('line', '#7f7f7f', undefined, patternSize),
pattern.draw('line-vertical', '#8c564b', undefined, patternSize),
Expand Down
15 changes: 6 additions & 9 deletions src/shapes/index.js
@@ -1,14 +1,9 @@
import { shapes, deprecatedShapes } from './shapes-list';

export function getRandomShape(excludedShapeTypes = []) {
const deprecatedShapesList = Object.keys(deprecatedShapes);
let shapesList = Object.keys(shapes);
const completeShapesList = [];

shapesList = shapesList.filter((shape) => {
if (deprecatedShapesList.indexOf(shape) === -1) {
return shape;
}
});
export function getRandomShape(excludedShapeTypes = []) {
const shapesList = Object.keys(shapes);

excludedShapeTypes.forEach(shapeType => {
shapesList.splice(shapesList.indexOf(shapeType), 1);
Expand All @@ -19,4 +14,6 @@ export function getRandomShape(excludedShapeTypes = []) {
return shapesList[randomIndex];
}

export default shapes;
Object.assign(completeShapesList, shapes, deprecatedShapes);

export default completeShapesList;
2 changes: 1 addition & 1 deletion test/patterns.js
Expand Up @@ -31,7 +31,7 @@ function generateColors (total) {
describe('pattern', () => {

describe('#draw', () => {
it('should return a canvas element', () => {
it('should return a canvas pattern', () => {
const testPattern = pattern.draw();

expect(testPattern.toString()).to.equal('[object CanvasPattern]');
Expand Down
10 changes: 4 additions & 6 deletions test/shapes/index.js
Expand Up @@ -8,12 +8,10 @@ global.document = jsdom.jsdom('<html></html>');
describe('shapes', () => {
describe('#getRandomShape', () => {
it('should NOT generate the specified excluded shape types', () => {
let containsExcludedShape = false;
const randomShapes = [];
const excludedShapes = Object.keys(shapes);
const removedShape = excludedShapes[0];

excludedShapes.splice(0, 1);
const removedShape = excludedShapes.splice(0, 1)[0];
let containsExcludedShape = false;

for (let i = 0; i < 30; i++) {
randomShapes.push(getRandomShape(excludedShapes));
Expand All @@ -25,11 +23,11 @@ describe('shapes', () => {
});

it('should NOT return a list that includes any deprecated patterns', () => {
let containsDeprecatedShapes = false;
const deprecatedShapesList = Object.keys(deprecatedShapes);
const randomShapes = [];
let containsDeprecatedShapes = false;

for (let i = 0; i < 100; i++) {
for (let i = 0; i < 30; i++) {
randomShapes.push(getRandomShape());
}

Expand Down

0 comments on commit e8a2a28

Please sign in to comment.