Comparing version 1.0.0 to 1.1.0
16
index.js
'use strict'; | ||
const mimicFn = require('mimic-fn'); | ||
const cacheStore = new WeakMap(); | ||
const defaultCacheKey = function (x) { | ||
@@ -19,3 +21,3 @@ if (arguments.length === 1 && (x === null || x === undefined || (typeof x !== 'function' && typeof x !== 'object'))) { | ||
const memoized = function () { | ||
const cache = memoized.__cache__; | ||
const cache = cacheStore.get(memoized); | ||
const key = opts.cacheKey.apply(null, arguments); | ||
@@ -41,7 +43,15 @@ | ||
memoized.__cache__ = opts.cache; | ||
mimicFn(memoized, fn); | ||
cacheStore.set(memoized, opts.cache); | ||
return memoized; | ||
}; | ||
module.exports.clear = fn => { | ||
const cache = cacheStore.get(fn); | ||
if (cache && typeof cache.clear === 'function') { | ||
cache.clear(); | ||
} | ||
}; |
{ | ||
"name": "mem", | ||
"version": "1.0.0", | ||
"version": "1.1.0", | ||
"description": "Memoize functions - An optimization used to speed up consecutive function calls by caching the result of calls with identical input", | ||
@@ -5,0 +5,0 @@ "license": "MIT", |
@@ -77,5 +77,5 @@ # mem [![Build Status](https://travis-ci.org/sindresorhus/mem.svg?branch=master)](https://travis-ci.org/sindresorhus/mem) | ||
### mem(input, [options]) | ||
### mem(fn, [options]) | ||
#### input | ||
#### fn | ||
@@ -105,8 +105,18 @@ Type: `Function` | ||
Type: `Object` | ||
Type: `Object`<br> | ||
Default: `new Map()` | ||
Use a different cache storage. Must implement the following methods: `.has(key)`, `.get(key)`, `.set(key, value)`. You could for example use a `WeakMap` instead. | ||
Use a different cache storage. Must implement the following methods: `.has(key)`, `.get(key)`, `.set(key, value)`, and optionally `.clear()`. You could for example use a `WeakMap` instead. | ||
### mem.clear(fn) | ||
Clear all cached data of a memoized function. | ||
#### fn | ||
Type: `Function` | ||
Memoized function. | ||
## Tips | ||
@@ -113,0 +123,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
5984
40
148
1