Skip to content

Commit

Permalink
♻️ Refactoring code.
Browse files Browse the repository at this point in the history
Remove Object.assign in favor of spread operator.
  • Loading branch information
Guillaume Martigny committed Dec 19, 2018
1 parent 17e1cb3 commit 600648c
Show file tree
Hide file tree
Showing 31 changed files with 159 additions and 109 deletions.
Binary file modified drawingTests/allInputs/expected.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
19 changes: 11 additions & 8 deletions modules/arc/arc.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,14 @@ export default class Arc extends Component {
* @inheritDoc
*/
toJSON () {
return Object.assign(super.toJSON(), {
horizontalRadius: this.horizontalRadius,
verticalRadius: this.verticalRadius,
startAngle: this.startAngle,
endAngle: this.endAngle,
});
const { horizontalRadius, verticalRadius, startAngle, endAngle } = this;
return {
...super.toJSON(),
horizontalRadius,
verticalRadius,
startAngle,
endAngle,
};
}

/**
Expand All @@ -68,9 +70,10 @@ export default class Arc extends Component {
* @return {LineOptions}
*/
static get defaultOptions () {
return Object.assign(Line.defaultOptions, {
return {
...Line.defaultOptions,
join: null,
});
};
}

/**
Expand Down
6 changes: 4 additions & 2 deletions modules/button/button.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,11 @@ export default class Button extends Input {
* @return {ButtonOptions}
*/
static get defaultOptions () {
return Object.assign(super.defaultOptions, Text.defaultOptions, {
return {
...Text.defaultOptions,
...super.defaultOptions,
value: "",
});
};
}

/**
Expand Down
5 changes: 3 additions & 2 deletions modules/checkbox/checkbox.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,11 @@ export default class Checkbox extends Input {
* @return {CheckboxOptions}
*/
static get defaultOptions () {
return Object.assign(super.defaultOptions, {
return {
...super.defaultOptions,
size: 20,
value: false,
});
};
}

/**
Expand Down
8 changes: 5 additions & 3 deletions modules/circle/circle.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,11 @@ export default class Circle extends Ellipse {
* @inheritDoc
*/
toJSON () {
const json = Object.assign(super.toJSON(), {
radius: this.radius,
});
const { radius } = this;
const json = {
...super.toJSON(),
radius,
};
delete json.horizontalRadius;
delete json.verticalRadius;
return json;
Expand Down
5 changes: 3 additions & 2 deletions modules/component/component.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,13 +99,14 @@ export default class Component extends Container {
* @return {ComponentOptions}
*/
static get defaultOptions () {
return Object.assign(super.defaultOptions, {
return {
...super.defaultOptions,
fill: "#000",
stroke: null,
strokeWidth: 2,
cursor: Component.cursors.default,
join: Component.joins.miter,
});
};
}

/**
Expand Down
16 changes: 10 additions & 6 deletions modules/container/container.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@ export default class Container extends EventEmitter {
/**
* @type {ContainerOptions}
*/
this.options = Object.assign(this.constructor.defaultOptions, options);
this.options = {
...this.constructor.defaultOptions,
...options,
};
this.options.rotationAnchor = Position.from(this.options.rotationAnchor);
/**
* @type {Array<Container>}
Expand Down Expand Up @@ -370,14 +373,14 @@ export default class Container extends EventEmitter {
Object.keys(this.options).forEach((key) => {
const value = this.options[key];
if (!(value && value.equals ? value.equals(defaultOptions[key]) : Object.is(value, defaultOptions[key]))) {
optionsCopy[key] = value && value.toJSON ? value.toJSON() : value;
optionsCopy[key] = value;
}
});

const json = Object.assign({
const json = {
constructor: this.constructor.name,
position: this.position,
});
};
if (this.children.length) {
json.children = this.children.map(child => child.toJSON());
}
Expand Down Expand Up @@ -438,11 +441,12 @@ export default class Container extends EventEmitter {
* @return {ContainerEvent}
*/
static get events () {
return Object.assign(super.events, {
return {
...super.events,
attach: "attach",
detach: "detach",
draw: "draw",
});
};
}

/**
Expand Down
4 changes: 2 additions & 2 deletions modules/container/container.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ test("toJSON", (t) => {
const json = t.context.toJSON();

t.is(json.children, undefined);
t.deepEqual(json.position, [10, 20]);
t.deepEqual(json.position, t.context.position);
t.is(json.options, undefined);
t.is(json.constructor, "Container");

Expand All @@ -272,7 +272,7 @@ test("toJSON", (t) => {
const reJson = specific.toJSON();
t.deepEqual(reJson.options, {
opacity: 0.5,
rotationAnchor: [10, 20],
rotationAnchor: specific.options.rotationAnchor,
});
t.is(reJson.children.length, 1);
});
Expand Down
5 changes: 3 additions & 2 deletions modules/draggable/draggable.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,11 @@ Component.prototype.draggable = function draggable (options) {
this.options.cursor = Component.cursors.grab;
}
this.isDraggable = true;
const mergedOptions = Object.assign({
const mergedOptions = {
x: true,
y: true,
}, options);
...options,
};

let startPosition = null;
let originPosition = null;
Expand Down
13 changes: 8 additions & 5 deletions modules/image/image.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,11 @@ export default class Image extends Rectangle {
* @inheritDoc
*/
toJSON () {
return Object.assign(super.toJSON(), {
url: this.url,
});
const { url } = this;
return {
...super.toJSON(),
url,
};
}

/**
Expand Down Expand Up @@ -143,8 +145,9 @@ export default class Image extends Rectangle {
* @return {RectangleOptions}
*/
static get defaultOptions () {
return Object.assign(super.defaultOptions, {
return {
...super.defaultOptions,
fill: null,
});
};
}
}
10 changes: 6 additions & 4 deletions modules/input/input.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,13 +99,14 @@ export default class Input extends Container {
* @return {InputOptions}
*/
static get defaultOptions () {
return Object.assign(super.defaultOptions, {
return {
...super.defaultOptions,
value: null,
fill: "#444",
background: "#f6f6f6",
border: "#aaa",
hover: "#dcdcdc",
});
};
}

/**
Expand All @@ -117,8 +118,9 @@ export default class Input extends Container {
* @return {InputEvents}
*/
static get events () {
return Object.assign(super.events, {
return {
...super.events,
change: "change",
});
};
}
}
13 changes: 8 additions & 5 deletions modules/line/line.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,11 @@ export default class Line extends Component {
* @inheritDoc
*/
toJSON () {
return Object.assign(super.toJSON(), {
points: this.points,
});
const { points } = this;
return {
...super.toJSON(),
points,
};
}

/**
Expand All @@ -78,12 +80,13 @@ export default class Line extends Component {
* @return {LineOptions}
*/
static get defaultOptions () {
const options = Object.assign(super.defaultOptions, {
const options = {
...super.defaultOptions,
cap: Line.caps.round,
join: Line.joins.round,
fill: null,
cursor: null,
});
};
options.stroke = super.defaultOptions.fill;
return options;
}
Expand Down
15 changes: 10 additions & 5 deletions modules/path/path.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,10 @@ export default class Path extends Component {

if (!isClosed) {
// Overrides options to work like a line if not closed
this.options = Object.assign(Line.defaultOptions, options);
this.options = {
...Line.defaultOptions,
...options,
};
}

this.instructions = instructions;
Expand Down Expand Up @@ -120,10 +123,12 @@ export default class Path extends Component {
* @inheritDoc
*/
toJSON () {
return Object.assign(super.toJSON(), {
instructions: this.instructions.map(instruction => instruction.toJSON()),
isClosed: this.isClosed,
});
const { instructions, isClosed } = this;
return {
...super.toJSON(),
instructions,
isClosed,
};
}

/**
Expand Down
5 changes: 3 additions & 2 deletions modules/pencil.js/pencil.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ function from (json) {
}

// Export all under namespace
export default Object.assign({
export default {
EventEmitter,
Component,
Input,
Expand All @@ -104,7 +104,8 @@ export default Object.assign({
version,
author,
homepage,
}, exportableClasses);
...exportableClasses,
};

// Named exports
export {
Expand Down
8 changes: 5 additions & 3 deletions modules/polygon/polygon.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,11 @@ export default class Polygon extends Component {
* @inheritDoc
*/
toJSON () {
return Object.assign(super.toJSON(), {
points: this.points,
});
const { points } = this;
return {
...super.toJSON(),
points,
};
}

/**
Expand Down
5 changes: 1 addition & 4 deletions modules/polygon/polygon.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,7 @@ test("trace", (t) => {
test("toJSON", (t) => {
const json = t.context.toJSON();

t.deepEqual(json.points, [
[100, 200],
[300, 400],
]);
t.deepEqual(json.points, t.context.points);
t.is(json.constructor, "Polygon");
});

Expand Down
6 changes: 5 additions & 1 deletion modules/position/position.js
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,11 @@ export default class Position {
* @return {Array<Number>}
*/
toJSON () {
return [truncate(this.x), truncate(this.y)];
const { x, y } = this;
return [
truncate(x),
truncate(y),
];
}

/**
Expand Down
5 changes: 3 additions & 2 deletions modules/progress-bar/progress-bar.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,12 @@ export default class ProgressBar extends Input {
* @return {SliderOptions}
*/
static get defaultOptions () {
return Object.assign(super.defaultOptions, {
return {
...super.defaultOptions,
value: 0,
width: 200,
speed: 0.3,
});
};
}

/**
Expand Down
Loading

0 comments on commit 600648c

Please sign in to comment.