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

interpatcher

v1.0.2

Published

A library for patching, replacing and decorating javascript methods during runtime. ๐Ÿ’

Downloads

11

Readme

InterPatcher ๐Ÿ’

A library for patching, replacing and decorating javascript methods during runtime.

Also called monkey patching ๐Ÿ’๐Ÿ’๐Ÿ’.

Source Code

The source code is available in GitHub.

Installation

npm i interpatcher

My first patch

// This prefixes the patched method with another method of our selection
addPrefix(targetObject, 'targetMethod', (prefixData) => {
  console.log("hello from a prefix");
});

Prefixes

Prefixes are patches that run before the original method.

addPrefix(targetObject, 'targetMethod', (prefixData) => {/**/});

The callback receives a PrefixData variable which keeps track of:

context : any, // The context from where the original method was called.
args : any[], // The arguments which were used to call the original method.
originalMethod : Function, // The original method so you can call it without causing overflows.
runOriginal : boolean, // Whether the original method should be skipped.
returnValue : any, // The return value in case you skip to run the original method.

So this allows you to completelly replace any method if you wanted to!

addPrefix(Math, 'abs', (prefixData) => {
  prefixData.runOriginal = false;
  prefixData.returnValue = -prefixData.args[0];
});

Postfixes

Postfixes are like prefixes, but they run after the original method instead.

addPrefix(targetObject, 'targetMethod', (postfixData) => {/**/});

The callback receives a PostfixData variable which keeps track of:

context : any, // The context from where the original method was called.
args : any[], // The arguments which were used to call the original method.
originalMethod : Function, // The original method so you can call it without causing overflows.
returnValue : any, // The return value (which is the original method value by default and can be replaced).

Unpatching

For unpatching just save the returned id for your prefix or postfix:

const prefixId = addPrefix(...);
unpatchPrefix(targetObject, targetMethod, prefixId);
const postfixId = addPostfix(...);
unpatchPostfix(targetObject, targetMethod, postfixId);

You can also unpatch all prefixes, postfixes and overrides from a patched method:

unpatchAll(targetObject, 'targetMethod');

Multi Prefixes, Postfixes and Overrides

Yes, you can have multiple prefixes, postfixes and overrides for a single method, they will run in order of registration, meaning that the later you register a method, the later it will run.