@wordpress/hooks
Advanced tools
Comparing version 1.1.4 to 1.1.5
@@ -46,3 +46,3 @@ import validateNamespace from './validateNamespace.js'; | ||
if (hooks.hasOwnProperty(hookName)) { | ||
if (hooks[hookName]) { | ||
// Find the correct insert index of the new hook. | ||
@@ -49,0 +49,0 @@ var handlers = hooks[hookName].handlers; |
@@ -25,3 +25,3 @@ import validateHookName from './validateHookName.js'; | ||
return hooks.hasOwnProperty(hookName) && hooks[hookName].runs ? hooks[hookName].runs : 0; | ||
return hooks[hookName] && hooks[hookName].runs ? hooks[hookName].runs : 0; | ||
}; | ||
@@ -28,0 +28,0 @@ } |
@@ -20,3 +20,3 @@ /** | ||
return function hasHook(hookName) { | ||
return hooks.hasOwnProperty(hookName) ? hooks[hookName].handlers.length : 0; | ||
return hooks[hookName] ? hooks[hookName].handlers.length : 0; | ||
}; | ||
@@ -23,0 +23,0 @@ } |
@@ -0,1 +1,2 @@ | ||
import _Object$create from 'babel-runtime/core-js/object/create'; | ||
import createAddHook from './createAddHook'; | ||
@@ -15,4 +16,6 @@ import createRemoveHook from './createRemoveHook'; | ||
function createHooks() { | ||
var actions = {}; | ||
var filters = {}; | ||
var actions = _Object$create(null); | ||
var filters = _Object$create(null); | ||
actions.__current = []; | ||
filters.__current = []; | ||
@@ -19,0 +22,0 @@ return { |
@@ -35,3 +35,3 @@ import validateNamespace from './validateNamespace.js'; | ||
// Bail if no hooks exist by this name | ||
if (!hooks.hasOwnProperty(hookName)) { | ||
if (!hooks[hookName]) { | ||
return 0; | ||
@@ -38,0 +38,0 @@ } |
@@ -24,16 +24,7 @@ import validateHookName from './validateHookName.js'; | ||
return function runHooks(hookName) { | ||
if (!validateHookName(hookName)) { | ||
return; | ||
var handlers = void 0; | ||
if (hooks[hookName]) { | ||
handlers = hooks[hookName].handlers; | ||
} | ||
if (!hooks.hasOwnProperty(hookName)) { | ||
hooks[hookName] = { | ||
runs: 0, | ||
handlers: [] | ||
}; | ||
} | ||
var handlers = hooks[hookName].handlers; | ||
for (var _len = arguments.length, args = Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) { | ||
@@ -43,3 +34,3 @@ args[_key - 1] = arguments[_key]; | ||
if (!handlers.length) { | ||
if (!handlers || !handlers.length) { | ||
return returnFirstArg ? args[0] : undefined; | ||
@@ -53,14 +44,15 @@ } | ||
hooks.__current = hooks.__current || []; | ||
hooks.__current.push(hookInfo); | ||
if (!hooks[hookName]) { | ||
hooks[hookName] = { | ||
runs: 0, | ||
handlers: [] | ||
}; | ||
} | ||
hooks[hookName].runs++; | ||
var maybeReturnValue = args[0]; | ||
while (hookInfo.currentIndex < handlers.length) { | ||
var handler = handlers[hookInfo.currentIndex]; | ||
maybeReturnValue = handler.callback.apply(null, args); | ||
if (returnFirstArg) { | ||
args[0] = maybeReturnValue; | ||
} | ||
args[0] = handler.callback.apply(null, args); | ||
hookInfo.currentIndex++; | ||
@@ -72,3 +64,3 @@ } | ||
if (returnFirstArg) { | ||
return maybeReturnValue; | ||
return args[0]; | ||
} | ||
@@ -75,0 +67,0 @@ }; |
@@ -60,3 +60,3 @@ 'use strict'; | ||
if (hooks.hasOwnProperty(hookName)) { | ||
if (hooks[hookName]) { | ||
// Find the correct insert index of the new hook. | ||
@@ -63,0 +63,0 @@ var handlers = hooks[hookName].handlers; |
@@ -35,3 +35,3 @@ 'use strict'; | ||
return hooks.hasOwnProperty(hookName) && hooks[hookName].runs ? hooks[hookName].runs : 0; | ||
return hooks[hookName] && hooks[hookName].runs ? hooks[hookName].runs : 0; | ||
}; | ||
@@ -38,0 +38,0 @@ } |
@@ -25,3 +25,3 @@ "use strict"; | ||
return function hasHook(hookName) { | ||
return hooks.hasOwnProperty(hookName) ? hooks[hookName].handlers.length : 0; | ||
return hooks[hookName] ? hooks[hookName].handlers.length : 0; | ||
}; | ||
@@ -28,0 +28,0 @@ } |
@@ -7,2 +7,6 @@ 'use strict'; | ||
var _create = require('babel-runtime/core-js/object/create'); | ||
var _create2 = _interopRequireDefault(_create); | ||
var _createAddHook = require('./createAddHook'); | ||
@@ -44,4 +48,6 @@ | ||
function createHooks() { | ||
var actions = {}; | ||
var filters = {}; | ||
var actions = (0, _create2.default)(null); | ||
var filters = (0, _create2.default)(null); | ||
actions.__current = []; | ||
filters.__current = []; | ||
@@ -48,0 +54,0 @@ return { |
@@ -49,3 +49,3 @@ 'use strict'; | ||
// Bail if no hooks exist by this name | ||
if (!hooks.hasOwnProperty(hookName)) { | ||
if (!hooks[hookName]) { | ||
return 0; | ||
@@ -52,0 +52,0 @@ } |
@@ -34,16 +34,7 @@ 'use strict'; | ||
return function runHooks(hookName) { | ||
if (!(0, _validateHookName2.default)(hookName)) { | ||
return; | ||
var handlers = void 0; | ||
if (hooks[hookName]) { | ||
handlers = hooks[hookName].handlers; | ||
} | ||
if (!hooks.hasOwnProperty(hookName)) { | ||
hooks[hookName] = { | ||
runs: 0, | ||
handlers: [] | ||
}; | ||
} | ||
var handlers = hooks[hookName].handlers; | ||
for (var _len = arguments.length, args = Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) { | ||
@@ -53,3 +44,3 @@ args[_key - 1] = arguments[_key]; | ||
if (!handlers.length) { | ||
if (!handlers || !handlers.length) { | ||
return returnFirstArg ? args[0] : undefined; | ||
@@ -63,14 +54,15 @@ } | ||
hooks.__current = hooks.__current || []; | ||
hooks.__current.push(hookInfo); | ||
if (!hooks[hookName]) { | ||
hooks[hookName] = { | ||
runs: 0, | ||
handlers: [] | ||
}; | ||
} | ||
hooks[hookName].runs++; | ||
var maybeReturnValue = args[0]; | ||
while (hookInfo.currentIndex < handlers.length) { | ||
var handler = handlers[hookInfo.currentIndex]; | ||
maybeReturnValue = handler.callback.apply(null, args); | ||
if (returnFirstArg) { | ||
args[0] = maybeReturnValue; | ||
} | ||
args[0] = handler.callback.apply(null, args); | ||
hookInfo.currentIndex++; | ||
@@ -82,3 +74,3 @@ } | ||
if (returnFirstArg) { | ||
return maybeReturnValue; | ||
return args[0]; | ||
} | ||
@@ -85,0 +77,0 @@ }; |
{ | ||
"name": "@wordpress/hooks", | ||
"version": "1.1.4", | ||
"version": "1.1.5", | ||
"description": "WordPress Hooks library", | ||
@@ -5,0 +5,0 @@ "author": "WordPress", |
@@ -44,3 +44,3 @@ import validateNamespace from './validateNamespace.js'; | ||
if ( hooks.hasOwnProperty( hookName ) ) { | ||
if ( hooks[ hookName ] ) { | ||
// Find the correct insert index of the new hook. | ||
@@ -47,0 +47,0 @@ const handlers = hooks[ hookName ].handlers; |
@@ -25,3 +25,3 @@ import validateHookName from './validateHookName.js'; | ||
return hooks.hasOwnProperty( hookName ) && hooks[ hookName ].runs | ||
return hooks[ hookName ] && hooks[ hookName ].runs | ||
? hooks[ hookName ].runs | ||
@@ -28,0 +28,0 @@ : 0; |
@@ -20,3 +20,3 @@ /** | ||
return function hasHook( hookName ) { | ||
return hooks.hasOwnProperty( hookName ) | ||
return hooks[ hookName ] | ||
? hooks[ hookName ].handlers.length | ||
@@ -23,0 +23,0 @@ : 0; |
@@ -15,4 +15,6 @@ import createAddHook from './createAddHook'; | ||
function createHooks() { | ||
const actions = {}; | ||
const filters = {}; | ||
const actions = Object.create( null ); | ||
const filters = Object.create( null ); | ||
actions.__current = []; | ||
filters.__current = []; | ||
@@ -19,0 +21,0 @@ return { |
@@ -35,3 +35,3 @@ import validateNamespace from './validateNamespace.js'; | ||
// Bail if no hooks exist by this name | ||
if ( ! hooks.hasOwnProperty( hookName ) ) { | ||
if ( ! hooks[ hookName ] ) { | ||
return 0; | ||
@@ -38,0 +38,0 @@ } |
@@ -24,17 +24,8 @@ import validateHookName from './validateHookName.js'; | ||
return function runHooks( hookName, ...args ) { | ||
if ( ! validateHookName( hookName ) ) { | ||
return; | ||
let handlers; | ||
if ( hooks[ hookName ] ) { | ||
handlers = hooks[ hookName ].handlers; | ||
} | ||
if ( ! hooks.hasOwnProperty( hookName ) ) { | ||
hooks[ hookName ] = { | ||
runs: 0, | ||
handlers: [], | ||
}; | ||
} | ||
const handlers = hooks[ hookName ].handlers; | ||
if ( ! handlers.length ) { | ||
if ( ! handlers || ! handlers.length ) { | ||
return returnFirstArg | ||
@@ -50,14 +41,15 @@ ? args[ 0 ] | ||
hooks.__current = hooks.__current || []; | ||
hooks.__current.push( hookInfo ); | ||
if ( ! hooks[ hookName ] ) { | ||
hooks[ hookName ] = { | ||
runs: 0, | ||
handlers: [], | ||
}; | ||
} | ||
hooks[ hookName ].runs++; | ||
let maybeReturnValue = args[ 0 ]; | ||
while ( hookInfo.currentIndex < handlers.length ) { | ||
const handler = handlers[ hookInfo.currentIndex ]; | ||
maybeReturnValue = handler.callback.apply( null, args ); | ||
if ( returnFirstArg ) { | ||
args[ 0 ] = maybeReturnValue; | ||
} | ||
args[ 0 ] = handler.callback.apply( null, args ); | ||
hookInfo.currentIndex++; | ||
@@ -69,3 +61,3 @@ } | ||
if ( returnFirstArg ) { | ||
return maybeReturnValue; | ||
return args[ 0 ]; | ||
} | ||
@@ -72,0 +64,0 @@ }; |
@@ -74,2 +74,6 @@ /* eslint-disable no-console */ | ||
for ( const k in hooks ) { | ||
if ( '__current' === k ) { | ||
continue; | ||
} | ||
delete hooks[ k ]; | ||
@@ -218,16 +222,2 @@ } | ||
test( 'cannot run filters with non-string names', () => { | ||
expect( applyFilters( () => {}, 42 ) ).toBe( undefined ); | ||
expect( console ).toHaveErroredWith( | ||
'The hook name must be a non-empty string.' | ||
); | ||
} ); | ||
test( 'cannot run filters named with __ prefix', () => { | ||
expect( applyFilters( '__test', 42 ) ).toBe( undefined ); | ||
expect( console ).toHaveErroredWith( | ||
'The hook name cannot begin with `__`.' | ||
); | ||
} ); | ||
test( 'add 3 filters with different priorities and run them', () => { | ||
@@ -234,0 +224,0 @@ addFilter( 'test.filter', 'my_callback_filter_a', filter_a ); |
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
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
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
37
69058
1849
1