src/models/CustomClass.js
import { stringToCamel } from '../utilities/FormatUtility';
/**
* Model representing Dynamic CustomClass.
*/
class CustomClassModel {
/**
* @param {Object} response - JSON formatted default response of a dynamic content
* @param {Number} response.id - The ID of the content
* @param {String} response.title - Title of the content
* @param {String} response.description - A description related to the content
* @param {String} response.created_at - The time at which the content was created
* @param {String} response.updated_at - The time at which the content was updated
* @param {Number} response.position - The position where the content is put on the platform
* @param {Array} response.tags - The tags to describe the content
* @param {Object} response.brand - The brand to describe the content
*/
constructor(response = {}) {
const _response = response.data || response; // if json api is passed in directly
const attributes = _response.attributes || _response; // if json api is passed in directly
if (_response.id) {
/** @type {Number} */
// this.id = parseInt(_response.id, 10);
// If this is non a number just keep the string value
if (Number.isNaN(_response.id)) {
this.id = _response.id;
} else { // If this is a number, parse it out into an int
this.id = parseInt(_response.id, 10);
}
}
// Object.assign(this, attributes);
Object.keys(attributes).forEach((key) => {
if (['type', 'relationships', 'id'].indexOf(key) === -1) {
/** @type {Any} */
this[stringToCamel(key)] = attributes[key];
}
});
}
}
export default CustomClassModel;