hooks-mixin
Advanced tools
Comparing version 1.2.2 to 1.3.0
@@ -1,1 +0,1 @@ | ||
"use strict";function _createForOfIteratorHelper(a){if("undefined"==typeof Symbol||null==a[Symbol.iterator]){if(Array.isArray(a)||(a=_unsupportedIterableToArray(a))){var b=0,c=function(){};return{s:c,n:function n(){return b>=a.length?{done:!0}:{done:!1,value:a[b++]}},e:function e(a){throw a},f:c}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}var d,e,f=!0,g=!1;return{s:function s(){d=a[Symbol.iterator]()},n:function n(){var a=d.next();return f=a.done,a},e:function e(a){g=!0,e=a},f:function f(){try{f||null==d["return"]||d["return"]()}finally{if(g)throw e}}}}function _unsupportedIterableToArray(a,b){if(a){if("string"==typeof a)return _arrayLikeToArray(a,b);var c=Object.prototype.toString.call(a).slice(8,-1);return"Object"===c&&a.constructor&&(c=a.constructor.name),"Map"===c||"Set"===c?Array.from(c):"Arguments"===c||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(c)?_arrayLikeToArray(a,b):void 0}}function _arrayLikeToArray(a,b){(null==b||b>a.length)&&(b=a.length);for(var c=0,d=Array(b);c<b;c++)d[c]=a[c];return d}function hook(a,b){if(!(a&&"string"==typeof a))throw new TypeError("name should be non empty string");if("function"!=typeof b)throw new TypeError("fn is not a function");this.__hooks||(this.__hooks={}),this.__hooks[a]||(this.__hooks[a]=[]),this.__hooks[a].push(b)}function getHooks(a,b){return a.__hooks&&a.__hooks[b]?a.__hooks[b]:null}function removeHook(a,b){var c=getHooks(this,a);if(!c)return null;var d=c.indexOf(b);return-1===d?null:(c.splice(d,1),b)}function getHookArguments(a,b){var c=Array.from(b);return c.shift(),c.push(a),c}function processHooks(a){var b=getHooks(this,a);if(b){var c,d=getHookArguments(this,arguments),e=_createForOfIteratorHelper(b);try{for(e.s();!(c=e.n()).done;){var f=c.value;f.apply(null,d)}}catch(a){e.e(a)}finally{e.f()}}}function processHooksAsync(a){var b=getHooks(this,a);if(b){var c=getHookArguments(this,arguments);return Promise.all(b.map(function(a){return a.apply(null,c)}))}}module.exports=function(a){return a.prototype.hook=hook,a.prototype.removeHook=removeHook,a.prototype.processHooks=processHooks,a.prototype.processHooksAsync=processHooksAsync,a}; | ||
"use strict";function _createForOfIteratorHelper(a,b){var c;if("undefined"==typeof Symbol||null==a[Symbol.iterator]){if(Array.isArray(a)||(c=_unsupportedIterableToArray(a))||b&&a&&"number"==typeof a.length){c&&(a=c);var d=0,e=function(){};return{s:e,n:function n(){return d>=a.length?{done:!0}:{done:!1,value:a[d++]}},e:function e(a){throw a},f:e}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}var f,g=!0,h=!1;return{s:function s(){c=a[Symbol.iterator]()},n:function n(){var a=c.next();return g=a.done,a},e:function e(a){h=!0,f=a},f:function f(){try{g||null==c["return"]||c["return"]()}finally{if(h)throw f}}}}function _slicedToArray(a,b){return _arrayWithHoles(a)||_iterableToArrayLimit(a,b)||_unsupportedIterableToArray(a,b)||_nonIterableRest()}function _nonIterableRest(){throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}function _unsupportedIterableToArray(a,b){if(a){if("string"==typeof a)return _arrayLikeToArray(a,b);var c=Object.prototype.toString.call(a).slice(8,-1);return"Object"===c&&a.constructor&&(c=a.constructor.name),"Map"===c||"Set"===c?Array.from(a):"Arguments"===c||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(c)?_arrayLikeToArray(a,b):void 0}}function _arrayLikeToArray(a,b){(null==b||b>a.length)&&(b=a.length);for(var c=0,d=Array(b);c<b;c++)d[c]=a[c];return d}function _iterableToArrayLimit(a,b){if("undefined"!=typeof Symbol&&Symbol.iterator in Object(a)){var c=[],d=!0,e=!1,f=void 0;try{for(var g,h=a[Symbol.iterator]();!(d=(g=h.next()).done)&&(c.push(g.value),!(b&&c.length===b));d=!0);}catch(a){e=!0,f=a}finally{try{d||null==h["return"]||h["return"]()}finally{if(e)throw f}}return c}}function _arrayWithHoles(a){if(Array.isArray(a))return a}function bulkHooksRead(a,b){for(var c=function(){var a=_slicedToArray(e[d],2),c=a[0],f=a[1];Array.isArray(f)?f.forEach(function(a){return b(c,a)}):b(c,f)},d=0,e=Object.entries(a);d<e.length;d++)c()}function hook(a,b){if(!(a&&"string"==typeof a))throw new TypeError("name should be non empty string");if("function"!=typeof b)throw new TypeError("fn is not a function");this.__hooks||(this.__hooks={}),this.__hooks[a]||(this.__hooks[a]=[]),this.__hooks[a].push(b)}function hooks(a){var b=this;bulkHooksRead(a,function(a,c){return b.hook(a,c)})}function getHooks(a,b){return a.__hooks&&a.__hooks[b]?a.__hooks[b]:null}function removeHook(a,b){var c=getHooks(this,a);if(!c)return null;var d=c.indexOf(b);return-1===d?null:(c.splice(d,1),b)}function removeHooks(a){var b=this;a?bulkHooksRead(a,function(a,c){return b.removeHook(a,c)}):this.__hooks=null}function getHookArguments(a,b){var c=Array.from(b);return c.shift(),c.push(a),c}function processHooks(a){var b=getHooks(this,a);if(b){var c,d=getHookArguments(this,arguments),e=_createForOfIteratorHelper(b);try{for(e.s();!(c=e.n()).done;){var f=c.value;f.apply(null,d)}}catch(a){e.e(a)}finally{e.f()}}}function processHooksAsync(a){var b=getHooks(this,a);if(b){var c=getHookArguments(this,arguments);return Promise.all(b.map(function(a){return a.apply(null,c)}))}}module.exports=function(a){return a.prototype.hook=hook,a.prototype.hooks=hooks,a.prototype.removeHook=removeHook,a.prototype.removeHooks=removeHooks,a.prototype.processHooks=processHooks,a.prototype.processHooksAsync=processHooksAsync,a}; |
62
index.js
@@ -0,3 +1,14 @@ | ||
function bulkHooksRead(hooks, cb) { | ||
for (const [name, fn] of Object.entries(hooks)) { | ||
if (Array.isArray(fn)) { | ||
fn.forEach((fn) => cb(name, fn)); | ||
} else { | ||
cb(name, fn); | ||
} | ||
} | ||
} | ||
/** | ||
* Add hook to Class | ||
* | ||
* @param {String} name hook's name | ||
@@ -19,3 +30,13 @@ * @param {Function} fn hook's callback | ||
/** | ||
* get hooks of context | ||
* Add group of hooks to Class | ||
* | ||
* @param {Object} hooks key of object is the hook's name, value is the hook's callback or array of callbacks | ||
*/ | ||
function hooks(hooks) { | ||
bulkHooksRead(hooks, (name, fn) => this.hook(name, fn)); | ||
} | ||
/** | ||
* Get hooks of context | ||
* | ||
* @param {Object} context | ||
@@ -31,3 +52,4 @@ * @param {String} name hook's name | ||
/** | ||
* remove hook by name and callback | ||
* Remove hook by name and callback | ||
* | ||
* @param {String} name hook's name | ||
@@ -47,3 +69,21 @@ * @param {Function} fn hook's callback | ||
/** | ||
* get hook arguments | ||
* Remove group of hooks from Class | ||
* | ||
* @param {Object} hooks key of object is the hook's name, value is the hook's callback or array of callbacks | ||
*/ | ||
function removeHooks(hooks) { | ||
if (!hooks) { | ||
this.__hooks = null; | ||
} else { | ||
bulkHooksRead( | ||
hooks, | ||
(name, fn) => this.removeHook(name, fn) | ||
); | ||
} | ||
} | ||
/** | ||
* Get hook arguments | ||
* | ||
* @param {Object} context | ||
@@ -61,4 +101,5 @@ * @param {Object} arguments | ||
/** | ||
* process hooks by name | ||
* @param {String} name hook's name | ||
* Process hooks by name | ||
* | ||
* @param {String} name hook's name | ||
*/ | ||
@@ -75,4 +116,6 @@ function processHooks(name) { | ||
/** | ||
* process hooks by name (“parallel” with Promise.all) | ||
* @param {String} name | ||
* Process hooks by name (“parallel” with Promise.all) | ||
* | ||
* @async | ||
* @param {String} name | ||
* @return {Promise} | ||
@@ -91,7 +134,10 @@ */ | ||
* Apply hooks' methods to Class | ||
* @param {Function} Class constructor | ||
* | ||
* @param {Function} Class constructor | ||
*/ | ||
module.exports = function HooksMixin(Class) { | ||
Class.prototype.hook = hook; | ||
Class.prototype.hooks = hooks; | ||
Class.prototype.removeHook = removeHook; | ||
Class.prototype.removeHooks = removeHooks; | ||
Class.prototype.processHooks = processHooks; | ||
@@ -98,0 +144,0 @@ Class.prototype.processHooksAsync = processHooksAsync; |
{ | ||
"name": "hooks-mixin", | ||
"version": "1.2.2", | ||
"version": "1.3.0", | ||
"description": "A small mixin for classes. Adds hooks ability to your classes", | ||
@@ -15,4 +15,5 @@ "main": "index.js", | ||
"scripts": { | ||
"lint": "eslint ./", | ||
"test": "jest", | ||
"build": "NODE_ENV=production ./node_modules/.bin/babel index.js -d build/", | ||
"test": "jest", | ||
"tarball": "tarball=\"$(npm pack .)\"; wc -c \"${tarball}\"; tar tvf \"${tarball}\"; rm \"${tarball}\";", | ||
@@ -22,10 +23,9 @@ "prepublishOnly": "yarn build && yarn tarball" | ||
"devDependencies": { | ||
"@babel/cli": "^7.8.4", | ||
"@babel/core": "^7.9.0", | ||
"@babel/preset-env": "^7.9.0", | ||
"babel-eslint": "^10.1.0", | ||
"@babel/cli": "^7.13.10", | ||
"@babel/core": "^7.13.10", | ||
"@babel/preset-env": "^7.13.10", | ||
"babel-preset-minify": "^0.5.1", | ||
"eslint": "^6.8.0", | ||
"jest": "^25.2.7" | ||
"eslint": "^7.22.0", | ||
"jest": "^26.6.3" | ||
} | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
9907
6
130