instrument-methods

1.1.1 • Public • Published

Build Status npm

instrument-methods

Simple object method instrumentation.

$ npm install --save instrument-methods

Usage

import instrumentMethods from 'instrument-methods';
 
const instance = {
  doThis: () => { ... },
  doThat: () => { ... }
};
 
instrumentMethods(instance, {
  before: (methodName, args) => console.log(`"${methodName}" is about to be called!`, ...args),
  after: (methodName, argS) => console.log(`"${methodName}" was called!`, ...args)
});
 
instance.doThat(1, 2, 3);
 
// Logs:
// "doThat" is about to be called!, 1, 2, 3
// "doThat" was called!, 1, 2, 3

Even though it modifies the object, instrumentMethods also returns a reference to the modified object so it can be used as part of an expression:

const after = methodName => console.log(`"${methodName}" was called!`);
 
const instance = instrumentMethods({
  doThis: () => { ... },
  doThat: () => { ... }
}, { after });

License

MIT License

Readme

Keywords

none

Package Sidebar

Install

npm i instrument-methods

Weekly Downloads

170

Version

1.1.1

License

MIT

Last publish

Collaborators

  • markdalgleish