
Security News
Deno 2.2 Improves Dependency Management and Expands Node.js Compatibility
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
hook-function
Advanced tools
utils for adding before and after hook to another function
A small utils for adding before and after hook to another function, no extra dependencies
// with npm
npm install hook-function
// or with yarn
yarn add hook-function
function first() {
console.log('1st');
}
function second() {
console.log('2nd');
}
function main() {
console.log('main');
}
const makeDelayFn = (ms) =>
function delayed() {
return new Promise((resolve) => {
setTimeout(() => {
resolve(console.log(`wait ${ms}ms`));
}, ms);
});
};
function instant() {
console.log('instant');
}
hook.before
& hook.after
const { hook } = require('hook-function');
const { before, after } = hook;
let mainWith2BeforeHooks = before(main, first, second);
mainWith2BeforeHooks();
// 1st
// 2nd
// main
let mainWith2AfterHooks = after(main, first, second);
mainWith2AfterHooks(main);
// main
// 1st
// 2nd
hook.beforeSequentially
& hook.afterSequentially
const { hook } = require('hook-function');
const { after, afterSequentially } = hook;
// beforeSequentially is similar
let sequentialHookFn = afterSequentially(
main,
makeDelayFn(300),
instant,
makeDelayFn(100)
);
sequentialHookFn();
// main
// wait 300ms
// instant
// wait 100ms
let normalHookFn = after(main, makeDelayFn(300), instant, makeDelayFn(100));
normalHookFn();
// main
// instant
// wait 100ms
// wait 300ms
hook.after
using data returned by main functionData return by main function are not chained in after-functions. Same data will be applied to each after-functions.
If after-functions are regular functions instead of arrow functions, the hook calls the regular functions directly.
const { hook } = require('hook-function');
const { after } = hook;
function printInput(input) {
console.log(input || 'nothing')
}
function returnTen() {
return 10;
}
function printDouble(num) {
console.log(num * 2);
}
}
function printTriple(num) {
console.log(num * 3);
}
let fn = after(
returnTen,
(result) => printDouble(result),
(result) => printTriple(result),
printInput,
(result) => printInput(result)
);
fn();
// 20
// 30
// nothing
// 10
FAQs
utils for adding before and after hook to another function
The npm package hook-function receives a total of 0 weekly downloads. As such, hook-function popularity was classified as not popular.
We found that hook-function demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
Security News
React's CRA deprecation announcement sparked community criticism over framework recommendations, leading to quick updates acknowledging build tools like Vite as valid alternatives.
Security News
Ransomware payment rates hit an all-time low in 2024 as law enforcement crackdowns, stronger defenses, and shifting policies make attacks riskier and less profitable.