Skip to content

Commit

Permalink
feat(shape): Implements JSON and Object serializers for shape
Browse files Browse the repository at this point in the history
This feature allows to you deserialize and serialize any Shape for sharing it with any others shapes
or to store it
  • Loading branch information
ghaiklor committed Nov 19, 2015
1 parent de587ef commit 0d3dd5d
Showing 1 changed file with 58 additions and 22 deletions.
80 changes: 58 additions & 22 deletions src/shapes/Shape.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
* }
*/
export class Shape {
_name = 'Shape';
static name = 'Shape';

_text = '';
_width = 15;
_height = 5;
Expand Down Expand Up @@ -52,27 +53,6 @@ export class Shape {
this.setForeground(foreground);
}

/**
* Get name of shape.
* Base shape has `Shape` name.
*
* @returns {String}
*/
getName() {
return this._name;
}

/**
* Set new name to shape.
*
* @param {String} name New name of the shape
* @returns {Shape}
*/
setName(name) {
this._name = name;
return this;
}

/**
* Get text content from this shape.
*
Expand Down Expand Up @@ -206,6 +186,36 @@ export class Shape {
throw new Error('render() method must be implemented');
}

/**
* Returns Object representation of the shape.
* This representation consists of all options fields.
*
* @returns {{name: String, options: {text: String, width: Number, height: Number, x: Number, y: Number, background: String, foreground: String}}}
*/
toObject() {
return {
name: Shape.name,
options: {
text: this.getText(),
width: this.getWidth(),
height: this.getHeight(),
x: this.getPosition().x,
y: this.getPosition().y,
background: this.getBackground(),
foreground: this.getForeground()
}
};
}

/**
* Returns JSON representation of the shape.
*
* @returns {String}
*/
toJSON() {
return JSON.stringify(this.toObject());
}

/**
* Wrapper around `new Shape()`.
*
Expand All @@ -215,4 +225,30 @@ export class Shape {
static create(...args) {
return new this(...args);
}

/**
* Creates new Shape instance from Object representation.
*
* @static
* @param {Object} obj Object that you got from {@link Shape.toObject}
* @returns {Shape}
*/
static fromObject(obj) {
if (!obj.name || !obj.options) throw new Error('It looks like it is not an Object representation of the Shape');
if (obj.name !== Shape.name) throw new Error('You are trying to create Shape from Object representation of another Shape');

return new this(obj.options);
}

/**
* Creates new Shape instance from JSON representation.
*
* @static
* @param {String} json JSON string that you got from {@link Shape.toJSON}
* @returns {Shape}
*/
static fromJSON(json) {
let obj = JSON.parse(json);
return Shape.fromObject(obj);
}
}

0 comments on commit 0d3dd5d

Please sign in to comment.