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 🙏

© 2025 – Pkg Stats / Ryan Hefner

interceptor.js

v0.2.0

Published

Interceptor.js is a small yet powerful JavaScript tool library that provides the ability to intercept a JavaScript function call.

Downloads

5

Readme

Interceptor.js

Latest version: v0.2.0

Interceptor.js is a JavaScript framework that provides the ability to intercept a JavaScript function call.

What does it do?

Let's look at the hello world example

var hi = function(name){ return "Hello " + name ;}//
hi.call();
// => "Hello undefined" because we didn't pass in any arguments

//We can fix this by intercepting before the function call
Interceptor.intercept(hi, function(thisArg, targetFunc, argList){
	if(argList.length == 0 || argList[0] == undefined){
		argList[0] = "guest" ;
	}
});

hi.call();
// =>  "Hello guest" ! 	
hi.call(null, "World");
// => "Hello World" 

Prerequisite and limitation

As you can see from the example above, the hi function is called by its call method.

Indeed Interceptor.js intercepts a function by overwriting its call and apply methods. Therefore, in order for the interception to be working,

  • it requires the caller to call the target function through its call or apply method.

Programming APIs

Interceptor

  • Interceptor.intercept - function(targetFunc, preFunc, postFunc)

    • targetFunc - The target function that will be intercepted. One function can be intercepted as many times as desired and the last interception will be invoked first.
    • preFunc - function(thisArg, targetFunc, argList) - The function that will be 'pluged in' and will run before the target function runs.
      • thisArg - The this object in the context of the target function.
      • targetFunc - Points to the target function.
      • argList - An array of objects that will be the arguments to invoke the target function.
    • postFunc - function(thisArg, targetFunc, argList, returnVal) - The function that will run after the target function runs.
      • thisArg, targetFunc, argList - Same as above.
      • returnVal - The return value from the target function. If you want to return a different value to the caller, use this.doReturn(myNewReturnValue).
  • Interceptor.revert - function(targetFunc)

    • targetFunc - Revert the last interception on targetFunc.

  • Interceptor.revertAll - function(targetFunc)

    • targetFunc - Revert all interceptions on targetFunc.
  • Interceptor.noConflict - function() Restore the global variable Interceptor to its previous value and returns the reference of Interceptor.js

Interceptor.prototype

The preFunc and postFunc will be assigned to an instance of Interceptor class. Therefore within the context of these two functions you have access to the methods inherits from Interceptor.prototype.

  • Interceptor.prototype.doReturn - function(returnVal) - Force to return the value returnVal instead of the one from target function. If called within preFunc, the call to the target function will be skipped. The postFunc will still run.
    • reutrnVal - The value to return.
  • Interceptor.prototype.doSkip - function() - Skip the call to the target function. The postFunc will still run.

Another brief example

var square = function(x){ return x * x ;};
square.call();
// => NaN
Interceptor.intercept(square, function(thisArg, targetFunc, argList){
	if(typeof argList[0] !== "number"){
		this.doReturn(undefined);
	}
});
square.call();
// => undefined
square.call(null, 2);
// => 4
// Now we intercept the return value
Interceptor.intercept(square, null,  function(thisArg, targetFunc, argList, ret){
	if(typeof ret !== "number"){
		this.doReturn(0);
	}
});
square.call();
// => 0
square.call(null, 2);
// => 4