Comparing version 5.0.0 to 5.1.0
declare namespace mem { | ||
interface CacheStorage<KeyType extends unknown, ValueType extends unknown> { | ||
interface CacheStorage<KeyType, ValueType> { | ||
has(key: KeyType): boolean; | ||
@@ -12,4 +12,4 @@ get(key: KeyType): ValueType | undefined; | ||
ArgumentsType extends unknown[], | ||
CacheKeyType extends unknown, | ||
ReturnType extends unknown | ||
CacheKeyType, | ||
ReturnType | ||
> { | ||
@@ -77,4 +77,4 @@ /** | ||
ArgumentsType extends unknown[], | ||
ReturnType extends unknown, | ||
CacheKeyType extends unknown | ||
ReturnType, | ||
CacheKeyType | ||
>( | ||
@@ -90,3 +90,3 @@ fn: (...arguments: ArgumentsType) => ReturnType, | ||
*/ | ||
clear<ArgumentsType extends unknown[], ReturnType extends unknown>( | ||
clear<ArgumentsType extends unknown[], ReturnType>( | ||
fn: (...arguments: ArgumentsType) => ReturnType | ||
@@ -93,0 +93,0 @@ ): void; |
44
index.js
@@ -25,37 +25,27 @@ 'use strict'; | ||
const mem = (fn, options) => { | ||
options = { | ||
cacheKey: defaultCacheKey, | ||
cache: new Map(), | ||
cachePromiseRejection: true, | ||
...options | ||
}; | ||
if (typeof options.maxAge === 'number') { | ||
mapAgeCleaner(options.cache); | ||
const mem = (fn, { | ||
cacheKey = defaultCacheKey, | ||
cache = new Map(), | ||
cachePromiseRejection = true, | ||
maxAge | ||
} = {}) => { | ||
if (typeof maxAge === 'number') { | ||
mapAgeCleaner(cache); | ||
} | ||
const {cache} = options; | ||
options.maxAge = options.maxAge || 0; | ||
const setData = (key, data) => { | ||
cache.set(key, { | ||
data, | ||
maxAge: Date.now() + options.maxAge | ||
}); | ||
}; | ||
const memoized = function (...arguments_) { | ||
const key = options.cacheKey(...arguments_); | ||
const key = cacheKey(...arguments_); | ||
if (cache.has(key)) { | ||
return cache.get(key).data; | ||
return maxAge ? cache.get(key).data : cache.get(key); | ||
} | ||
const cacheItem = fn.call(this, ...arguments_); | ||
const cacheItem = fn.apply(this, arguments_); | ||
setData(key, cacheItem); | ||
cache.set(key, maxAge ? { | ||
data: cacheItem, | ||
maxAge: Date.now() + maxAge | ||
} : cacheItem); | ||
if (isPromise(cacheItem) && options.cachePromiseRejection === false) { | ||
// Remove rejected promises from cache unless `cachePromiseRejection` is set to `true` | ||
if (isPromise(cacheItem) && cachePromiseRejection === false) { | ||
cacheItem.catch(() => cache.delete(key)); | ||
@@ -73,3 +63,3 @@ } | ||
cacheStore.set(memoized, options.cache); | ||
cacheStore.set(memoized, cache); | ||
@@ -76,0 +66,0 @@ return memoized; |
{ | ||
"name": "mem", | ||
"version": "5.0.0", | ||
"version": "5.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", |
@@ -81,3 +81,3 @@ # mem [![Build Status](https://travis-ci.org/sindresorhus/mem.svg?branch=master)](https://travis-ci.org/sindresorhus/mem) | ||
### mem(fn, [options]) | ||
### mem(fn, options?) | ||
@@ -166,4 +166,12 @@ #### fn | ||
## License | ||
--- | ||
MIT © [Sindre Sorhus](https://sindresorhus.com) | ||
<div align="center"> | ||
<b> | ||
<a href="https://tidelift.com/subscription/pkg/npm-mem?utm_source=npm-mem&utm_medium=referral&utm_campaign=readme">Get professional support for this package with a Tidelift subscription</a> | ||
</b> | ||
<br> | ||
<sub> | ||
Tidelift helps make open source sustainable for maintainers while giving companies<br>assurances about security, maintenance, and licensing for their dependencies. | ||
</sub> | ||
</div> |
9692
176
133