mcf-objects/dist/mcf-objects.js
2020-03-16 23:25:54 +01:00

2 lines
No EOL
6.9 KiB
JavaScript

!function(t,e){"object"==typeof exports&&"object"==typeof module?module.exports=e():"function"==typeof define&&define.amd?define([],e):"object"==typeof exports?exports.mcfObjects=e():t.mcfObjects=e()}(window,(function(){return function(t){var e={};function n(i){if(e[i])return e[i].exports;var o=e[i]={i:i,l:!1,exports:{}};return t[i].call(o.exports,o,o.exports,n),o.l=!0,o.exports}return n.m=t,n.c=e,n.d=function(t,e,i){n.o(t,e)||Object.defineProperty(t,e,{enumerable:!0,get:i})},n.r=function(t){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"__esModule",{value:!0})},n.t=function(t,e){if(1&e&&(t=n(t)),8&e)return t;if(4&e&&"object"==typeof t&&t&&t.__esModule)return t;var i=Object.create(null);if(n.r(i),Object.defineProperty(i,"default",{enumerable:!0,value:t}),2&e&&"string"!=typeof t)for(var o in t)n.d(i,o,function(e){return t[e]}.bind(null,o));return i},n.n=function(t){var e=t&&t.__esModule?function(){return t.default}:function(){return t};return n.d(e,"a",e),e},n.o=function(t,e){return Object.prototype.hasOwnProperty.call(t,e)},n.p="",n(n.s=0)}([function(t,e,n){"use strict";function i(t){return(i="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t})(t)}function o(t,e){return!e||"object"!==i(e)&&"function"!=typeof e?function(t){if(void 0===t)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return t}(t):e}function r(t){return(r=Object.setPrototypeOf?Object.getPrototypeOf:function(t){return t.__proto__||Object.getPrototypeOf(t)})(t)}function a(t,e){if("function"!=typeof e&&null!==e)throw new TypeError("Super expression must either be null or a function");t.prototype=Object.create(e&&e.prototype,{constructor:{value:t,writable:!0,configurable:!0}}),e&&u(t,e)}function u(t,e){return(u=Object.setPrototypeOf||function(t,e){return t.__proto__=e,t})(t,e)}function f(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}function c(t,e){for(var n=0;n<e.length;n++){var i=e[n];i.enumerable=i.enumerable||!1,i.configurable=!0,"value"in i&&(i.writable=!0),Object.defineProperty(t,i.key,i)}}function s(t,e,n){return e&&c(t.prototype,e),n&&c(t,n),t}n.r(e),n.d(e,"definedObject",(function(){return p}));var l=function(){function t(e){f(this,t),this.definition=e,this.validations={}}return s(t,[{key:"setValue",value:function(t){this.validate(t),this.value=t}},{key:"getValue",value:function(){return this.value}},{key:"validate",value:function(t){for(var e in this.validations)this.validations[e](t,this.definition)}}]),t}(),h=function(t){function e(t){var n;return f(this,e),(n=o(this,r(e).call(this,t))).addValidations(),n}return a(e,t),s(e,[{key:"addValidations",value:function(){for(var t in this.validations.type=function(t){if("string"!=typeof t)throw new Error("MCF_String creation: the type of ".concat(t,' isn\'t "string"'))},this.definition)switch(t){case"maxLength":this.validations.maxLength=function(t,e){if(t.length>e.maxLength)throw new Error('MCF_String creation: the length of the string "'.concat(t,'" is higher than the maximum defined: ').concat(e.maxLength))};break;case"minLength":this.validations.minLength=function(t,e){if(t.length<e.minLength)throw new Error('MCF_String creation: the length of the string "'.concat(t,'" is lower than the minimum defined: ').concat(e.minLength))}}}}]),e}(l),d=function(t){function e(t){var n;return f(this,e),(n=o(this,r(e).call(this,t))).addValidations(),n}return a(e,t),s(e,[{key:"addValidations",value:function(){for(var t in this.validations.type=function(t){if("number"!=typeof t)throw new Error("MCF_Number creation: the type of the value ".concat(t,' isn\'t "number"'))},this.definition)switch(t){case"max":this.validations.max=function(t,e){if(t>e.max)throw new Error("MCF_Number creation: the value ".concat(t," is higher than the maximum defined: ").concat(e.maxLength))};break;case"min":this.validations.min=function(t,e){if(t<e.min)throw new Error("MCF_Number creation: the value ".concat(t," is lower than the minimum defined: ").concat(e.minLength))}}}}]),e}(l),v=function(t){function e(t){var n;return f(this,e),(n=o(this,r(e).call(this,t))).addValidations(),n}return a(e,t),s(e,[{key:"addValidations",value:function(){this.validations.type=function(t){if("boolean"!=typeof t)throw new Error("MCF_Boolean creation: the type of the value ".concat(t,' isn\'t "boolean"'))}}}]),e}(l),y=function(t){function e(t){var n;return f(this,e),(n=o(this,r(e).call(this,t))).addValidations(),n}return a(e,t),s(e,[{key:"addValidations",value:function(){for(var t in this.validations.type=function(t){if(!Array.isArray(t))throw new Error("MCF_Array creation: the type of ".concat(t,' isn\'t "array"'))},this.definition)switch(t){case"maxLength":this.validations.maxLength=function(t,e){if(t.length>e.maxLength)throw new Error('MCF_Array creation: the length of the array "'.concat(t,'" is higher than the maximum defined: ').concat(e.maxLength))};break;case"minLength":this.validations.minLength=function(t,e){if(t.length<e.minLength)throw new Error('MCF_Array creation: the length of the array "'.concat(t,'" is lower than the minimum defined: ').concat(e.minLength))}}}}]),e}(l),m=function(t){function e(t){var n;return f(this,e),(n=o(this,r(e).call(this,t))).addValidations(),n}return a(e,t),s(e,[{key:"addValidations",value:function(){this.validations.isAvailable=function(t,e){if(!e.availableValues.reduce((function(e,n){return t===n||e}),!1))throw new Error('MCF_Enum creation: the value "'.concat(t,"\" isn't one of the next values, included in the field definition: ").concat(e.availableValues.toString()))}}}]),e}(l),b=function(){function t(){f(this,t)}return s(t,null,[{key:"buildDefinedObject",value:function(t){var e=t.getDefinition().fields,n={};for(var i in e)switch(e[i].TYPE){case"string":n[i]=new h(e[i]);break;case"number":n[i]=new d(e[i]);break;case"boolean":n[i]=new v(e[i]);break;case"enum":n[i]=new m(e[i]);break;case"array":n[i]=new y(e[i])}return n}}]),t}(),p=function(){function t(e){f(this,t),this.definition=e,this.validations={},this.fields=b.buildDefinedObject(this),this.setValue(e.value)}return s(t,[{key:"getDefinition",value:function(){return this.definition}},{key:"getValue",value:function(){var t={};for(var e in this.fields)t[e]=this.fields[e].value;return t}},{key:"setValue",value:function(t){for(var e in this.definition.fields)if(void 0===t[e]||null===t){if(!0===this.definition.fields[e].required)throw new Error("The field ".concat(e," is required"));if(void 0!==this.definition.fields[e].defaultValue&&(t[e]=this.definition.fields[e].defaultValue),!1===this.definition.fields[e].null)throw new Error("The field ".concat(e," can't be undefined"))}for(var n in t)void 0===t[n]?this.fields[n].value=void 0:this.fields[n].setValue(t[n])}},{key:"setPropertiesValue",value:function(t){for(var e in t)this.fields[e].setValue(t[e])}}]),t}()}])}));
//# sourceMappingURL=mcf-objects.js.map