0.0.1 • Published 5 years ago

@knesk/hook v0.0.1

Weekly downloads
-
License
MIT
Repository
github
Last release
5 years ago

Knesk Hook · GitHub license

A Wordpress like hook and filter module in Javascript.

Can be used as Javascript Plugin or Node.js Module with any kind of Node.js Framework

How to Install

npm install @knesk/hook
or
yarn add @knesk/hook

All Examples

Check example.js file for all examples and simply run by using command node example.js

Action Example

const Hook = require('./index.js')();
async function actionDemo1() {
    console.log('\n======= Action DEMO1 =========');
    Hook.Action.add('action/demo1', function(person) {
        console.log(`My name is ${person.fname} ${person.lname}`)
    }, {priority: 11});
    Hook.Action.add('action/demo1', function() {
        console.log('Again one more action');
    }, {priority: 12});
    
    // Demo 2
    Hook.Action.add('action/demo2', function(fname, lname) {
        console.log(`My name is ${fname} ${lname}`);
    });
    
    await Hook.Action.do('action/demo1', {fname: 'Aman', lname: 'Khanakia'});
    await Hook.Action.do('action/demo2', 'Aman', 'Khanakia');
    /*
    outputs -
       My name is Aman Khanakia
       Again one more action
       My name is Aman Khanakia
    */
}

Filter Examples

const Hook = require('./index.js')();
async function filterDemo1() {
    console.log('\n======= FILTER DEMO1 =========');
    Hook.Filter.add('filter/demo1', function(person) {
        console.log(person) // { fname: 'Aman', lname: 'Khanakia' }
        person.fname = 'Kevin';
        return person
    });
    
    console.log(await Hook.Filter.apply('filter/demo1', {fname: 'Aman', lname: 'Khanakia'}))
    // output - { fname: 'Kevin', lname: 'Khanakia' }
}

async function filterDemo3() {
    console.log('\n======= FILTER DEMO3 =========');
    Hook.Filter.add('filter/demo3', function(fname, lname) {
        fname = 'Kevin'
        return [...arguments]
    });
    
    Hook.Filter.add('filter/demo3', function(fname, lname) {
        fname = 'Ryan'
        lname = 'Sassy'
        return [...arguments]
    });
    const result = (await Hook.Filter.apply('filter/demo3', 'Aman', 'Khanakia'))
    console.log(result)
    // output - [ 'Ryan', 'Sassy' ]
    console.log(`result[0] - ${result[0]}`)
    // output - Ryan
}
0.0.1

5 years ago