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 🙏

© 2026 – Pkg Stats / Ryan Hefner

fn-cache-wrapper

v1.1.0

Published

Apply fn-cache to all functions of an object

Readme

fn-cache-wrapper

Apply fn-cache to all functions of an object.

Usage

Installation

npm install --save fn-cache-wrapper

Wrap

wrap returns a new object with cached versions of another objects functions. Works for regular objects and ES6 classes.

const { wrap } = require('fn-cache-wrapper');

class ApiClient {

  things() {
    return request('http://api.domain.com/things');
  }
  
}

let client = new ApiClient();
let cachedClient = wrap(client, { lifetime: 2000 });

// response will be cached
cachedClient.things();

wrap is useful when you need want to deduplicate all client calls within a request without needing to manage a separate cache. Just wrap the client and at the end of the request throw the wrapper away.

Replace

replace replaces all the functions on an object with cached versions.

const { replace } = require('fn-cache-wrapper');

class ApiClient {

  things() {
    return request('http://api.domain.com/things');
  }
  
}

let client = new ApiClient();
replace(client, { lifetime: 2000 });

// response will be cached
client.things();

replace is useful when you want default caching behavior for a client always.

Clearing caches

You can clear the function cache manually just like with fn-cache for both wrap and replace.

let client = wrap(new ApiClient(), { lifetime: 2000 });
client.things();
client.things.clearCache();

Configuration

Deep caching [wrap]

When using wrap the object being wrapped may call other functions defined on itself.

class ApiClient {

  me() {
    return request('http://api.domain.com/users/me');
  }

  myThings() {
    return this.me()
      .then(id => request(`http://api.domain.com/users/${id}/things`);
  }
  
}

By default wrap will bind the functions being cached such that calls like this.me() will go to the cached version and not the uncached version. This can be disabled by passing { deep: false } when creating the wrapper.

const { wrap } = require('fn-cache-wrapper');

let client = new ApiClient();
let cached = wrap(new ApiClient(), { lifetime: 2000, deep: false });

client.me();         // now there is a cached result
client.me();         // will get the cached result
client.myThings();   // will not use the cached 'me()' result.

Property copying [wrap]

When using wrap property values are copied from the original object.

const { wrap } = require('fn-cache-wrapper');

class ApiClient {

  constructor() {
    this.host = 'http://api.domain.com';
  }

  me() {
    return request(`${this.host}/users/me`);
  }
  
}

let client = new ApiClient();
let cached = wrap(new ApiClient(), { lifetime: 2000 });


cached.me()   // access to 'this.host' will work

client.host = 'http://api-2.domain.com';
client.me.clearCache();
client.me()   // still using 'http://api.domain.com'

Property copying can be turned off be passing { props: false } in the options.

Excluding functions [wrap, replace]

An array of function names can be passed under the exclude option to skip caching.

const { wrap } = require('fn-cache-wrapper');

let client = new ApiClient();
let cached = wrap(new ApiClient(), { lifetime: 2000, exclude: ['me'] });

client.me();         // will not be cached
client.myThings();   // the result of 'myThings()' will be cached

No binding [wrap, replace]

If you do not want the functions to be bound to the containing object use the { bind: false } option. Obviously this is only useful when you want the this reference to be context specific and not in the class examples used thus far.

const { wrap } = require('fn-cache-wrapper');

let client = new ApiClient();
let cached = wrap(new ApiClient(), { lifetime: 2000, bind: false });

client.me();         // works as normal
client.myThings();   // will fail with a TypeError as 'this' is undefined.