UNPKG

@plasma-js/hades

Version:

A standalone event handler and data manager.

2 lines (1 loc) 6.63 kB
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports,require("babel-polyfill"),require("yup")):"function"==typeof define&&define.amd?define(["exports","babel-polyfill","yup"],e):e((t=t||self).index={},null,t.yup)}(this,function(t,e,i){"use strict";function a(t){return(a="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 c(t,e,n,r,o,i,a){try{var u=t[i](a),c=u.value}catch(t){return void n(t)}u.done?e(c):Promise.resolve(c).then(r,o)}function n(u){return function(){var t=this,a=arguments;return new Promise(function(e,n){function r(t){c(i,e,n,r,o,"next",t)}function o(t){c(i,e,n,r,o,"throw",t)}var i=u.apply(t,a);r(void 0)})}}function r(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}function o(t,e){for(var n,r=0;r<e.length;r++)(n=e[r]).enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(t,n.key,n)}function u(t,e,n){return e&&o(t.prototype,e),n&&o(t,n),t}function l(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&&f(t,e)}function s(t){return(s=Object.setPrototypeOf?Object.getPrototypeOf:function(t){return t.__proto__||Object.getPrototypeOf(t)})(t)}function f(t,e){return(f=Object.setPrototypeOf||function(t,e){return t.__proto__=e,t})(t,e)}function h(){return(h=function(){if("undefined"==typeof Reflect||!Reflect.construct)return!1;if(Reflect.construct.sham)return!1;if("function"==typeof Proxy)return!0;try{return Date.prototype.toString.call(Reflect.construct(Date,[],function(){})),!0}catch(t){return!1}}()?Reflect.construct:function(t,e,n){var r=[null];r.push.apply(r,e);var o=new(Function.bind.apply(t,r));return n&&f(o,n.prototype),o}).apply(null,arguments)}function p(t){var n="function"==typeof Map?new Map:void 0;return(p=function(t){function e(){return h(t,arguments,s(this).constructor)}if(null===t||!function(t){return-1!==Function.toString.call(t).indexOf("[native code]")}(t))return t;if("function"!=typeof t)throw new TypeError("Super expression must either be null or a function");if(void 0!==n){if(n.has(t))return n.get(t);n.set(t,e)}return e.prototype=Object.create(t.prototype,{constructor:{value:e,enumerable:!1,writable:!0,configurable:!0}}),f(e,t)})(t)}function y(t){if(void 0===t)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return t}function d(t,e){return!e||"object"!=typeof e&&"function"!=typeof e?y(t):e}var v,b,m=(u(R,[{key:"publish",value:function(t){var e=this,n=1<arguments.length&&void 0!==arguments[1]?arguments[1]:null,r=this.events[t];if(!r||!r.length)throw new Error("Event called before one handler definition.");r.forEach(function(t){t.bind(e,n)})}},{key:"on",value:function(t,e){if(!t||"string"!=typeof t)throw new Error("The first param is empty or invalid.");if(this.events[t]||(this.events[t]=[]),!e||"function"!=typeof e)throw new Error("The second param is required and should be a function!");this.events[t].push(e)}}]),R),w=function o(i){"object"!==a(i)&&"array"!=typeof i||("object"===a(i)?Object:Array).keys(i).forEach(function(t){if("object"===a(i[t])||"array"==typeof i[t]){var e=i[t],n=Array.isArray(e)?"array":"object",r="array"==n?"of":"shape";i[t]=[],i[t][n]=null,i[t][r]=o(e)}else i[t]=function(t){var n=[];return t.split("|").forEach(function(t){var e=t.split("(")[1]?t.split("(")[1].split(")")[0]:null;n[e?t.split("(")[0]:t]=e}),n}(i[t])});return i},E=(l(_,p(Error)),_),j=(l(S,m),u(S,[{key:"add",value:(b=n(regeneratorRuntime.mark(function t(e){return regeneratorRuntime.wrap(function(t){for(;;)switch(t.prev=t.next){case 0:return t.prev=0,t.next=3,this.validate(e);case 3:return e._id=Date.now(),this.data.push(e),t.abrupt("return",this.data[this.data.length]);case 8:throw t.prev=8,t.t0=t.catch(0),new E(t.t0);case 11:case 12:case"end":return t.stop()}},t,this,[[0,8]])})),function(){return b.apply(this,arguments)})},{key:"runner",value:function(n){var r=this,o=i;return Object.keys(n).forEach(function(t){var e=n[t];o=o[t].call(r,e)}),o}},{key:"mapEntries",value:function(e){var n={};return Object.keys(e).forEach(function(t){n[t]=e[t]}),n}},{key:"mapSchema",value:function(e){var n=this,r=1<arguments.length&&void 0!==arguments[1]?arguments[1]:null,o=i;return Object.keys(e).forEach(function(t){r?"shape"===r?(o===i&&(o={}),o[t]=n.mapSchema(e[t])):"of"===r&&(o=n.mapSchema(e[t])):"object"===t||"array"===t?o=null===e[t]?o[t].call(o):o[t].call(o,e[t]):"shape"===t||"of"===t?o=o[t].call(o,n.mapSchema(e[t],t)):null!==e[t]||(o=o[t].call(o))}),o}},{key:"validate",value:(v=n(regeneratorRuntime.mark(function t(e){return regeneratorRuntime.wrap(function(t){for(;;)switch(t.prev=t.next){case 0:if("function"==typeof this.schema.validate)return t.abrupt("return",this.schema.validate(e));t.next=4;break;case 4:throw new Error("Unable to validate with a undefined model schema.");case 5:case"end":return t.stop()}},t,this)})),function(){return v.apply(this,arguments)})}]),S),g=(u(x,[{key:"new",value:function(t,e){if(this.data[t]||this[t])throw!this[t]instanceof j?new Error("Unable to create the collection. '".concat(t.charAt(0).toUpperCase(),"' is a reserved word.")):new Error("Collection '".concat(t,"' already exists!"));if("object"!==a(e))throw new Error("The second param 'Fields' should be a valid object");this.data[t]=new j(e),this[t]=this.list[t]=this.data[t]}},{key:"list",value:function(){return this.list}},{key:"remove",value:function(t){if(!this.data[t])throw new Error("Collection '".concat(t,"' not found!"));this.data[t]=null,delete this[t],delete this.list[t],delete this.data[t]}}]),x),k=(l(O,m),O);function O(t){var e;return r(this,O),(e=d(this,s(O).call(this))).collections=new g,t&&"function"==typeof t&&t.bind(y(e)),e}function x(){r(this,x),this.data=[],this.list=[]}function S(t){var e;if(r(this,S),e=d(this,s(S).call(this)),!t)throw new Error("The second param 'fields' is obrigatory!");return e.data=[],e.fields=t,e.model=function(t){var e=[];return Array.isArray(t)?(e.array=null,e.of=w(t)):(e.object=null,e.shape=w(t)),e}(e.fields),e.schema=e.mapSchema(e.model),e}function _(t){var e;return r(this,_),(e=d(this,s(_).call(this,t.message))).errors=t.errors,e.params=t.params,e.value=t.value,e}function R(){r(this,R),this.events=[],this.history=[],this.getters=[]}t.Collection=j,t.Collections=g,t.Events=m,t.default=k,Object.defineProperty(t,"__esModule",{value:!0})});