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

function-proxy

v0.5.2

Published

proxy functions to modify arguments or spying

Downloads

7

Readme

function-proxy Build Status

Proxy functions with functions for modifying arguments, intercepting errors, or spying

Spies

spy(fn, spyFn) aka mux

useful for tests, to ensure a function is invoked

var spy = require('function-proxy').spy;

request.get('http://google.com', spy(cb, spyFn));
function cb (err, res) {
  console.log(res.statusCode); // 200
}
function spyFn (err, res) {
  console.log(res.statusCode); // 200
}

spyOnMethod(obj, methodName, spyFn)

useful for tests, to ensure an object method is invoked

var spyOnMethod = require('function-proxy').spyOnMethod;
var utils = {
  exists: function (val) {...}
}
spyOnMethod(utils, 'exists', spy);
utils.exists('test'); // returns true like normal

function spy (val) {
  console.log(val); // test
}

spyOnClassMethod(obj, methodName, spyFn)

useful for tests, to ensure a Class method is invoked

var spyOnClassMethod = require('function-proxy').spyOnClassMethod;
var Animal = require('./Animal');
var animal = new Animal();

spyOnMethod(Animal, 'setIsMammal', spy);

animal.setIsMammal(true);
console.log(animal.isMammal); // true, method worked like normal

function spy (val) {
  console.log(val); // true, spy was also invoked
}

Error Intercept

intercept(fn, onError) aka int

useful for calling back a common onError function

only invokes onError if error exists

var intercept = require('function-proxy').intercept;
var newFn = intercept(fn, onError)
newFn(new Error('boom'));

function onError (err) {
  console.error(err.message); // boom
}
function fn (...) {...}; // was never invoked

only invokes fn if error does not exist, also does not pass error to fn

var intercept = require('function-proxy').intercept;
var newFn = intercept(fn, onError)
newFn(null, 'hello');

function fn (str) { // notice no error arg
  console.log(str); // hello
}
function onError (...) {...}; // was never invoked

invokes fn with error if dontSliceError arg is true

var intercept = require('function-proxy').intercept;
var dontSliceError = true;
var newFn = intercept(fn, onError, dontSliceError) // true indicates pass on error to fn
newFn(null, 'hello');

function fn (err, str) { // notice HAS error arg
  // err will always not exist here.
  console.log(str); // hello
}
function onError (...) {...}; // was never invoked

Transform Args like Arrays

sliceArgs(fn, start, end) aka slice

allows you to slice off arguments you dont care for (Array.prototype.slice for arguments)

var sliceArgs = require('function-proxy').sliceArgs;
var newFn = sliceArgs(fn, 0, 1)
newFn('foo', 'bar', 'baz');

function fn (a, b, c) {
  console.log(arguments); // { 0:'foo' } , 'bar' and 'baz' were not passed
};

spliceArgs(fn, index, howMany /*, elementsToInsert.. */) aka splice

allows you to splice arguments (remove and/or add add args), (Array.prototype.splice for arguments)

var spliceArgs = require('function-proxy').spliceArgs;
var newFn = spliceArgs(fn, 0, 1, 'inserted')
newFn('foo', 'bar');

function fn (a, b, c) {
  console.log(arguments); // { 0:'inserted', 1:'bar' }, overrode foo with splice
};

mapArgs(fn, mapFn [, includeError]) aka map

allows you to map arguments to new values before passing them along

var mapArgs = require('function-proxy').mapArgs;
var newFn = mapArgs(fn, parseToInt)
newFn(null, '1', '2');

function fn (err, a, b) {
  console.log(arguments); // { 0:null, 1:1, 2:2 }, args were parsed from Strings to Integers
};
var mapArgs = require('function-proxy').mapArgs;
var includeError = true; // includeError specifies not to skip the error arg (args[0])
var newFn = mapArgs(fn, parseToInt, includeError)
newFn('1', '2');

function fn (a, b) {
  console.log(arguments); // { 0:1, 1:2 }, args were parsed from Strings to Integers
};

filterArgs(fn, filterFn [, includeError]) aka filter

allows you to filter (remove) arguments passed

var filterArgs = require('function-proxy').filterArgs;
var newFn = filterArgs(fn, exists)
newFn(null, 'foo', null, 'bar');

function fn (err, a, b) {
  console.log(arguments); // { 0:null, 1:'foo', 2:'bar' }, null args were ignored
};
var filterArgs = require('function-proxy').filterArgs;
var newFn = filterArgs(fn, exists)
newFn('foo', null, 'bar');

function fn (err, a, b) {
  console.log(arguments); // { 0:'foo', 1:'bar' }, null args were ignored
};

Map Args Transforms (from tjmehta/map-utils)

pickArgs(fn, keys..., includeError) aka pick

picks keys off each arg before passing

omitArgs(fn, keys..., includeError) aka omit

omits keys off each arg before passing

setArgs(fn, setObj, includeError) aka set

setArgs(fn, key, value, includeError)

sets keys and values on each arg before passing

unsetArgs(fn, key, value, includeError) aka unset

unsets keys on each arg before passing

pluckArgs(fn, key, includeError) aka pluck

allows you map each arg to a key on the arg

mixed example

var pluckArgs = require('function-proxy').pluckArgs;
var sliceArgs = require('function-proxy').sliceArgs;
request.get('http://google.com', pluckArgs(sliceArgs(cb, 0, 1), 'statusCode'));

function cb (err, statusCode) {
  console.log(statusCode); // 200, yay google is up
};

License

MIT