npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

to-aop

v0.5.4

Published

AOP(aspect-oriented programming) library or creating hooks for debug and monkey patch function using ES6 Proxy|Class Prototype.

Downloads

12,058

Readme

to-aop

Build Status Coverage Status NPM package version code style: prettier

The to-aop module help you with applying Aspect Oriented Programming to JavaScript. It use under the hood ES Proxy for object same as other similar modules. It allow you hook class without creating new instance as well. It use javascript prototype for that.

For example I am using it for adding hooks to production code where all debug code is missing. I don't have got access to instance but I have only class constructor. But usage is unlimited.

More articles about AOP:

  1. https://blog.mgechev.com/2015/07/29/aspect-oriented-programming-javascript-aop-js/
  2. https://hackernoon.com/aspect-oriented-programming-in-javascript-es5-typescript-d751dda576d0

Examples:

  1. https://github.com/mjancarik/shallow-with-context/blob/master/src/shallowWithContext.js
  2. https://github.com/seznam/ima/blob/master/packages/devtools/src/services/defaultSettings.json

Installation

npm i to-aop --save

Usage

Easy example for native Date.now method.

import { aop, hookName, createHook } from 'to-aop';

const nowHook = createHook(hookName.aroundMethod, 'now', (args) => {
    return 1;
});

aop(Date, nowHook);

Date.now() // returns 1

Other example with more hooks for your defined class.

// a.js
export default class A {
  constructor(variable) {
    this.variable = variable;
  }

  method() {
    return this.variable;
  }

  notHookedMethod() {
    return 'not hook';
  }    
}

Applying AOP to class

You can use it for creating hook to your favorite framework or your own application without modification source code.

import { aop, hookName, createHook, unAop } from 'to-aop';
import A from './a';

const classHookBefore = createHook(
  hookName.beforeMethod,
  /^(method)$/,
  ({ target, object, property, context, args, meta }) => {
    //call your own hook
    meta.startTime = performance.now();
    console.log(
      `Instance of ${target.name} call "${property}"
with arguments ${args && args.length ? args : '[]'}.`
    );
  }
);

const classHookAfter = createHook(
  hookName.afterMethod,
  /^(method)$/,
  ({ target, object, property, context, args, payload, meta }) => {
    //call your own hook
    console.log(
      `Instance of ${target.name} call "${property}"
with arguments ${args && args.length ? args : '[]'}
and return value is "${payload}" took ${performance.now() - meta.startTime}.`
    );
  }
);

const hooks = Object.assign({}, classHookBefore, classHookAfter);

aop(
  A,
  hooks
); // bind hook to class
const a = new A('my hook');

a.method(); // log: 'Instance of A call "method" with arguments [] and return value is "my hook".', returns: 'my hook'
a.notHookedClassMethod(); // returns: 'not hook'

unAop(A);
a.method(); // returns: 'my hook'
a.notHookedClassMethod(); // returns: 'not hook'

Applying AOP to instance or object

import { aop, hookName, createHook, unAop } from 'to-aop';
import A from './a';

const instanceHook = createHook(
  hookName.afterMethod,
  /^(method)$/,
  ({ target, object, property, context, args, payload, meta }) => {
    console.log(
      `Instance of ${object.constructor.name} call "${property}"
with arguments ${args && args.length ? args : '[]'}
and return value is "${payload}".`
    );
  }
);

const a = new A('my hook');
const hookedInstance = aop(a, instanceHook); // bind hook to instance

hookedInstance.method(); // log: 'Instance of A call "method" with arguments [] and return value is "my hook.', returns: 'my hook'
hookedInstance.notHookedClassMethod(); // returns: 'not hook'

unAop(a);
a.method(); // returns: 'my hook'
a.notHookedClassMethod(); // returns: 'not hook'

Documentation

createHook(hookName, regular, callbackHook)

  • (string) hookName - set of hooks
  • (string, regexp, Array, function) regular - condition for filtering
  • (function) callbackHook - your defined action

aop(target, pattern, settings)

  • (class, object) - target for hooks
  • (Object<string, Array>) - pattern of hooks
  • {Object} settings
  • {booelan} [settings.constructor=false] - allow hook class constructor

unAop(target)

  • (class, object) - target for hooks

Hooks API

We implemented base set of hooks which you can use for AOP.

  1. beforeMethod
  2. afterMethod
  3. aroundMethod
  4. beforeGetter
  5. afterGetter
  6. aroundGetter
  7. beforeSetter
  8. afterSetter
  9. aroundSetter
import { hookName } from 'to-aop';

// hookName.(beforeMethod|afterMethod|...)