-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweightEventEmmitter.min.js
1 lines (1 loc) · 3.13 KB
/
weightEventEmmitter.min.js
1
'use strict';var _createClass=function(){function a(a,b){for(var c,d=0;d<b.length;d++)c=b[d],c.enumerable=c.enumerable||!1,c.configurable=!0,'value'in c&&(c.writable=!0),Object.defineProperty(a,c.key,c)}return function(b,c,d){return c&&a(b.prototype,c),d&&a(b,d),b}}();function _defineProperty(a,b,c){return b in a?Object.defineProperty(a,b,{value:c,enumerable:!0,configurable:!0,writable:!0}):a[b]=c,a}function _toConsumableArray(a){if(Array.isArray(a)){for(var b=0,c=Array(a.length);b<a.length;b++)c[b]=a[b];return c}return Array.from(a)}function _classCallCheck(a,b){if(!(a instanceof b))throw new TypeError('Cannot call a class as a function')}var WeightEventEmmitter=function(){function a(){_classCallCheck(this,a),this.listeners={}}return _createClass(a,[{key:'_isValidListener',value:function c(a){if(!a)return!1;var b=Object.prototype.toString.call(a);return'[object Function]'===b||'[object RegExp]'===b||'[object Object]'===b&&this._isValidListener(a.listener)}},{key:'_isValidEventName',value:function d(a){if(!a)return!1;var b=Object.prototype.toString.call(a),c=/^([^\.]+\.(\-|\+)?(\d+\.)?\d+)$|^([^\.]+)$/;return!!('[object String]'===b&&c.test(a))}},{key:'_stricterParseFloat',value:function b(a){return /^(\-|\+)?([0-9]+(\.[0-9]+)?|Infinity)$/.test(a)?+a:NaN}},{key:'_findIndexOfListeners',value:function c(a,b){return a.findIndex(function(a){return a===b})}},{key:'$once',value:function c(a,b){return b._once=!0,this.$on(a,b)}},{key:'$off',value:function f(a,b){if(this.listeners[a]){var c=this.listeners[a];for(var g in c)if(c.hasOwnProperty(g)){var d=c[g],e=this._findIndexOfListeners(d,b);if(-1===e)continue;else d.splice(e,1)}return this}}},{key:'$emit',value:function j(a){for(var c=this,d=arguments.length,e=Array(1<d?d-1:0),f=1;f<d;f++)e[f-1]=arguments[f];if(this.listeners[a]){var g=this.listeners[a],h=Object.keys(g).filter(function(a){return'[object String]'===Object.prototype.toString.call(a)}).sort(function(d,a){return c._stricterParseFloat(a)-c._stricterParseFloat(d)}),i=[];return h.forEach(function(a){i.push.apply(i,_toConsumableArray(g[a]))}),i.forEach(function(b){b.apply(c,e),b._once&&c.$off(a,b)}),this}}},{key:'$on',value:function f(a,b){if(!this._isValidListener(b))throw new TypeError('listener is not valid, must be a funtion or valid object');if(!this._isValidEventName(a))throw new TypeError('eventName is not valid, must be valid static string or string which includes eventName and weight, such as `event` or `event.1` or `event.1.1`');var c=void 0,d=void 0,e=/^([^\.]+\.(\-|\+)?(\d+\.)?\d+)$/;return e.test(a)?(d=/^[^\.]+/.exec(a)[0],c=a.split(/^[^\.]+\./)[1]):(d=a,c=-Infinity),this.listeners[d]?this.listeners[d][c]?this.listeners[d][c].push(b):this.listeners[d][c]=[b]:this.listeners[d]=_defineProperty({},c,[b]),this}},{key:'$offAll',value:function d(a){var b=this.listeners,c=this._isValidEventName;if('string'==typeof a){if(!c(a))throw new TypeError('eventName is not valid, must be valid static string or string which includes eventName and weight, such as `event` or `event.1` or `event.1.1`');delete b[a]}else if(a instanceof RegExp)for(var e in b)b.hasOwnProperty(e)&&a.test(e)&&delete b[e];return this}}]),a}();module.exports=WeightEventEmmitter;