Socket
Book a DemoInstallSign in
Socket

hooki

Package Overview
Dependencies
Maintainers
1
Versions
11
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

hooki

An advanced Javascript hook system.

latest
Source
npmnpm
Version
0.4.0
Version published
Weekly downloads
2
100%
Maintainers
1
Weekly downloads
 
Created
Source

Hooki

Hooki Logo

⚓️ An advanced Javascript hook system.

Installation

npm install hooki --save

Usage

// product.class.js

import Hooki from 'hooki';
import { before, after } from './hooks';

class Product {
  constructor(name, price, stock) {
    this.name = name;
    this.price = price;
    this.stock = stock;
  }

  buy(quantity) {
    console.log(`${this.name} x${quantity}`);
  }

}

export new Hooki(ProductClass, before, after);

// hooks.js

export const before = {
  buy: [
    function verifyStock(context) {
      if (context.params.quantity > context.self.quantity) {
        throw Error('Amount exceeds stock!');
      }
      return context;
    }
  ]
};

export const after = {
  buy: [
    function removeFromStock(context) {
      context.self.quantity -= context.params.quantity;
      return context
    }
  ]
};

// app.js

import Product from './product.class.js';

const bag = new Product('bag', 10.43, 5);

bag.buy(4); 
// -> bag x4
console.log(bag.stock); 
// -> 1
bag.buy(2); 
// -> Error: Amount exceeds stock!

Roadmap

Tests

  • tests

target types

  • object
  • class
  • function

action types

  • function
  • get
  • set
  • getter
  • setter
  • construct

hook types

  • after
  • before
  • errors

features

  • plugin system
  • async hooks (promises, callback)
  • extended hooks methods (target.after([hook]), target.before([hooks]))

plugins

  • hooki-events
  • hooki-validator
  • hooki-errors
  • hooki-lifecycle
  • hooki-modules

Keywords

hook

FAQs

Package last updated on 10 Aug 2017

Did you know?

Socket

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.

Install

Related posts