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

@irrelon/intercept

v1.0.3

Published

A simple tool to intercept calls to a function and return a different result (the original function) is never called.

Downloads

6

Readme

Intercept

A simple tool to intercept calls to a function and return a different result (the original function) is never called.

Useful when writing unit tests so you don't have to worry about side effects of calling a function, API calls being made, data being changed etc.

Install

npm i @irrelon/intercept --save-dev

Usage

replace(replacement)

Creates an intercept that when accessed, provides the specified value instead of the original.

Returns a restorer function that when called will disable the intercept and restore the value to the original one.

// Require the assert module from Node to test our responses
const assert = require("assert");

// Require the intercept module
const intercept = require("@irrelon/intercept");

// Create a simple test object with a function called
// foo() that when called will return the string "foo" 
const testObj = {
	foo: () => {
		return "foo";
	}
};
		
// Intercept testObj's foo() function and when it is
// called, return "bar" instead. You can call the returned
// restorer function if you wish to cancel the intercept.
// You can also use the restore() function if you prefer not
// to keep a reference to the restorer (see doc for restore()
// further down this readme).
const restorer = intercept(testObj, "foo").replace(() => {
	return "bar";
});

// Call foo() the first time and we get our value
// from the function we replaced it with
const returnVal1 = testObj.foo();

// Now restore the original
restorer();

// Call foo() a second time and we get the response
// from the original foo() function of "foo"
const returnVal2 = testObj.foo();

assert.strictEqual(returnVal1, "bar", "The first response is correct");
assert.strictEqual(returnVal2, "foo", "The second response is correct");

replaceReturn(returnVal, autoRestoreCount = 0)

Creates an intercept that when called returns the specified value instead of calling the original.

If an autoRestoreCount is specified then the intercept will be automatically restored to the original once the intercept has been called the specified number of times. If not specified then the original will only be restored by calling restore() or the restorer function returned from replaceReturn().

Returns a restorer function that when called will disable the intercept and restore the function to the original one.

// Require the assert module from Node to test our responses
const assert = require("assert");

// Require the intercept module
const intercept = require("@irrelon/intercept");

// Create a simple test object with a function called
// foo() that when called will return the string "foo" 
const testObj = {
	foo: () => {
		return "foo";
	}
};
		
// Intercept testObj's foo() function and when it is
// called, return "bar" instead. Notice replaceReturn()
// is used with a count of 1 in the second argument. This
// restores the original function that was intercepted
// after the first call to the intercepted one.
const restorer = intercept(testObj, "foo").replaceReturn("bar", 1);

// Call foo() the first time and we get our value
// from intercept's replaceReturn() which will be "bar"
const returnVal1 = testObj.foo();

// Call foo() a second time and we get the response
// from the original foo() function of "foo" because we
// specified an autoRestoreCount of 1 to replaceReturn()
const returnVal2 = testObj.foo();

assert.strictEqual(returnVal1, "bar", "The first response is correct");
assert.strictEqual(returnVal2, "foo", "The second response is correct");

restore()

Restores any intercepts on the object to their original values effectively disabling intercept on the object and path.

// Require the assert module from Node to test our responses
const assert = require("assert");

// Require the intercept module
const intercept = require("@irrelon/intercept");

// Create a simple test object with a function called
// foo() that when called will return the string "foo" 
const testObj = {
	"foo": () => {
		return "foo";
	}
};

// Create the intercept
const restorer = intercept(testObj, "foo").replaceReturn("bar");

// Check the intercept works
const returnVal1 = testObj.foo();

// Restore the intercept back to the original (you could
// also just called restorer() as returned from replaceReturn()
// but both ways are acceptable)
intercept(testObj, "foo").restore();

// Check the original works
const returnVal2 = testObj.foo();

// Make sure our values are what we expect
assert.strictEqual(returnVal1, "bar", "The first response is correct");
assert.strictEqual(returnVal2, "foo", "The second response is correct");