Runtime Hooks
What's This?
Adding hooks to any function you need!
Usage
Install via NPM:
npm install runtime-hooksBasic usage:
import { withHookBefore } from 'runtime-hooks'
window.alert = withHookBefore(window.alert, console.log)These utils are mainly designed for advanced usage (logging / testing / debugging...), don't abusing them in business code.
withHookBefore
(originalFn: function, hookFn: function): function
Given original function, return a new high-order function that:
- Calls your customs hook function beforehand.
- Calls the original function.
If your hook function returns false, the original function will not be executed. You can replace the reference to original function with the generated function, which makes sense for function hooking:
window.alert = withHookBefore(window.alert, console.log)withHookAfter
(originalFn: function, hookFn: function): function
Given original function, return a new high-order function that:
- Calls the original function.
- Calls your customs hook function afterwards.
hookArgs
(originalFn: function, argsGetter: function): function
Given original function, runs a new high-order function that:
- Calls
argsGetterwith args of original function. - If
argsGetterreturns an array, replace original args with it, or else keep the args. - Calls original function with these args, returning what it returns.
hookOutput
(originalFn: function, outputGetter: function): function
Given original function, runs a new high-order function that:
- Calls original function and get its output.
- Calls
outputGetterwith output, returning what it returns.
Both
hookOutputandwithHookAfterruns after original function.hookOutputreplaces the output, butwithHookAfterwon't.
License
MIT

Formed in 2009, the Archive Team (not to be confused with the archive.org Archive-It Team) is a rogue archivist collective dedicated to saving copies of rapidly dying or deleted websites for the sake of history and digital heritage. The group is 100% composed of volunteers and interested parties, and has expanded into a large amount of related projects for saving online and digital history.
