UNPKG

ami-cjs.js

Version:

<p align="center"> <img src="https://cloud.githubusercontent.com/assets/214063/23213764/78ade038-f90c-11e6-8208-4fcade5f3832.png" width="60%"> </p>

124 lines (102 loc) 3.63 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } /** * Base object. * * @module models/base */ var ModelsBase = function () { function ModelsBase() { _classCallCheck(this, ModelsBase); this._id = -1; } /** * Merge 2 arrays of models. * Merge the target array into the reference array. * * @param {Array.<Models>} referenceArray - Array to be merge against * @param {Array.<Models>} targetArray - Array to be merged against reference. * * @return {boolean} True if merge was sucessful. False if something went wrong. */ _createClass(ModelsBase, [{ key: 'mergeModels', value: function mergeModels(referenceArray, targetArray) { if (!(this._validateModelArray(referenceArray) && this._validateModelArray(targetArray))) { window.console.log('invalid inputs provided.'); return false; } for (var i = 0; i < targetArray.length; i++) { // test targetArray against existing targetArray for (var j = 0; j < referenceArray.length; j++) { if (referenceArray[j].merge(targetArray[i])) { // merged successfully break; } else if (j === referenceArray.length - 1) { // last merge was not successful // this is a new targetArray referenceArray.push(targetArray[i]); } } } return true; } /** * Merge model against current model. */ }, { key: 'merge', value: function merge(model) { // make sure model is valid if (!this.validate(model)) { return false; } // they can be merged if they match if (this._id === model._id) { return true; } return false; } /** * Validate a model. * * @return {boolean} True if model is valid. False if not. */ }, { key: 'validate', value: function validate(model) { if (!(model && model !== null && typeof model.merge === 'function')) { return false; } return true; } /** * Validate array of models. * * @param {Array.<Models>} modelArray - Array containing models. * * @return {boolean} True if array is valid. False if not. */ }, { key: '_validateModelArray', value: function _validateModelArray(modelArray) { if (!(modelArray !== null && Array === modelArray.constructor)) { window.console.log('invalid model array provided.'); return false; } for (var i = 0; i < modelArray.length; i++) { if (!(modelArray[i] && modelArray[i] !== null && typeof modelArray[i].validate === 'function' && modelArray[i].validate(modelArray[i]))) { return false; } } return true; } }]); return ModelsBase; }(); exports.default = ModelsBase; module.exports = exports['default'];