0.0.1 • Published 3 years ago

cachewrap v0.0.1

Weekly downloads
-
License
MIT
Repository
github
Last release
3 years ago

cachewrap

cache the return of a function

Install

npm install cachewrap

Usage

const createCacheWrap = require('cachewrap')

const cacheWrap = createcacheWrap()

function random(min, max) {
  return Math.random() * (max - min) + min;
}

const wrappedRandom = cacheWrap(random)

wrappedRandom(10, 100) === wrappedRandom(10, 100) // true
Refreshing

Cached data can be refreshed by using the refresh prop

wrappedRandom.refresh(10, 100) === wrappedRandom(10, 100) // true
wrappedRandom(10, 100) === wrappedRandom.refresh(10, 100) // false

Cache hash

By default all function calls are hashed with params.map(p => p.toString).join(). Continuing the above example, the hash function can be overwritten as:

const cacheWrap = createCache(myCustomDefaultHasher)

const wrappedRandom = cacheWrap(random) // uses myCustomDefaultHasher

const wrappedRandom2 = cacheWrap(random, myCustomHasher) // uses myCustomHasher

Baby vector created by catalyststuff - www.freepik.com