1.0.0 • Published 2 years ago

uncino v1.0.0

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

Uncino 🪝

Fast, tiny and solid hooks system for Javascript and NodeJS

Uncino is italian word for hook

Do you know Wordpress hooks system? Uncino is a hooks system highly inspired to it!

  • Async / Await support
  • Node or Browser support
  • Actions / Hooks system
  • Easy to use
  • No Dependencies

Support this project

Buy me a coffee

Installation

npm i uncino or yarn add uncino

Quick example

const uncino = require('uncino')
const hooks = uncino()

hooks.addHook('test', 'namespace', async (number) => {
  return number + 10
})

hooks.addHook('test', 'namespace', async (number) => {
  return number + 20
})

async function testHook(myNumber) {
  const newNumber = await hooks.runHook('test', myNumber)
  console.log(myNumber, newNumber)
}

testHook(3)  // 3 33
testHook(10)  // 10 40

Philosophy

Uncino permit to your code to interact/modify another piece of code at specific, pre-defined spots.

Uncino has two types of hooks: Actions and Hooks. To use either, you need to write a custom function, and then register for a specific action or hook.

Actions allow you to add data or change how your code operates. Actions will run at a specific point in the execution. Callback functions for Actions can perform some kind of a task, like echoing output to the user or inserting something into the database. Callback functions for an Action do not return anything back to the calling Action hook.

Hooks give you the ability to change data during the execution of your code. Callback functions for Filters will accept a variable, modify it, and return it. They are meant to work in an isolated manner, and should never have side effects such as affecting global variables and output. Filters expect to have something returned back to them.

With Uncino you can create your own hook spots so that other developers can extend and modify your code or you can create your pluggable core.

Hooks API

Method NameArgumentsDescription
addHookhookName, namespace, callback, priorityRegister callback as new hook for hookName in namespace with priority (default: 10).
removeHookhookName, namespaceRemove hook for hookName in namespace
hasHookhookName, namespaceCheck if hook for hookName in namespace exists
removeAllHookshookNameRemove all hooks for hookName
runHookhookName, paramsRun hook for hookName with params. It returns a Promise with the hook result

Actions API

Method NameArgumentsDescription
addActionactionName, namespace, callback, priorityRegister callback as new action for actionName in namespace with priority (default: 10).
removeActionactionName, namespaceRemove hook for actionName in namespace
hasActionactionName, namespaceCheck if hook for actionName in namespace exists
removeAllActionsactionNameRemove all actionss for actionName
runActionactionName, paramsRun action for actionName with params. It returns a Promise with void value

Example

Clone the repo and look in the test folder

Develop

Clone the repo then use npm install for download all the dependencies then launch npm run build for build the project

Pull Requests?

I'd love them!

Comments?

Let's hear them! (The nice ones please!)

Me?

In case you're interested I'm @riktarweb

1.0.0

2 years ago

0.0.5

2 years ago

0.0.4

2 years ago

0.0.3

2 years ago

0.0.2

2 years ago

0.0.1

2 years ago