Big News: Socket Selected for OpenAI's Cybersecurity Grant Program.Details
Socket
Book a DemoSign in
Socket

mem

Package Overview
Dependencies
Maintainers
1
Versions
29
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

mem - npm Package Compare versions

Comparing version
4.1.0
to
4.2.0
+71
index.d.ts
export interface CacheStorage<
KeyType extends unknown,
ValueType extends unknown
> {
has(key: KeyType): boolean;
get(key: KeyType): ValueType | undefined;
set(key: KeyType, value: ValueType): void;
delete(key: KeyType): void;
clear?: () => void;
}
export interface Options<
ArgumentsType extends unknown[],
CacheKeyType extends unknown,
ReturnType extends unknown
> {
/**
* Milliseconds until the cache expires.
*
* @default Infinity
*/
readonly maxAge?: number;
/**
* Determines the cache key for storing the result based on the function arguments. By default, if there's only one argument and it's a [primitive](https://developer.mozilla.org/en-US/docs/Glossary/Primitive), it's used directly as a key, otherwise it's all the function arguments JSON stringified as an array.
*
* You could for example change it to only cache on the first argument `x => JSON.stringify(x)`.
*/
readonly cacheKey?: (...arguments: ArgumentsType) => CacheKeyType;
/**
* Use a different cache storage. Must implement the following methods: `.has(key)`, `.get(key)`, `.set(key, value)`, `.delete(key)`, and optionally `.clear()`. You could for example use a `WeakMap` instead or [`quick-lru`](https://github.com/sindresorhus/quick-lru) for a LRU cache.
*
* @default new Map()
*/
readonly cache?: CacheStorage<CacheKeyType, ReturnType>;
/**
* Cache rejected promises.
*
* @default false
*/
readonly cachePromiseRejection?: boolean;
}
/**
* [Memoize](https://en.wikipedia.org/wiki/Memoization) functions - An optimization used to speed up consecutive function calls by caching the result of calls with identical input.
*
* @param fn - Function to be memoized.
*/
declare const mem: {
<
ArgumentsType extends unknown[],
ReturnType extends unknown,
CacheKeyType extends unknown
>(
fn: (...arguments: ArgumentsType) => ReturnType,
options?: Options<ArgumentsType, CacheKeyType, ReturnType>
): (...arguments: ArgumentsType) => ReturnType;
/**
* Clear all cached data of a memoized function.
*
* @param fn - Memoized function.
*/
clear<ArgumentsType extends unknown[], ReturnType extends unknown>(
fn: (...arguments: ArgumentsType) => ReturnType
): void;
};
export default mem;
+17
-16

@@ -8,9 +8,9 @@ 'use strict';

const defaultCacheKey = (...args) => {
if (args.length === 0) {
const defaultCacheKey = (...arguments_) => {
if (arguments_.length === 0) {
return '__defaultKey';
}
if (args.length === 1) {
const [firstArgument] = args;
if (arguments_.length === 1) {
const [firstArgument] = arguments_;
if (

@@ -25,6 +25,6 @@ firstArgument === null ||

return JSON.stringify(args);
return JSON.stringify(arguments_);
};
module.exports = (fn, options) => {
const mem = (fn, options) => {
options = Object.assign({

@@ -50,21 +50,19 @@ cacheKey: defaultCacheKey,

const memoized = function (...args) {
const key = options.cacheKey(...args);
const memoized = function (...arguments_) {
const key = options.cacheKey(...arguments_);
if (cache.has(key)) {
const c = cache.get(key);
return c.data;
return cache.get(key).data;
}
const ret = fn.call(this, ...args);
const cacheItem = fn.call(this, ...arguments_);
setData(key, ret);
setData(key, cacheItem);
if (isPromise(ret) && options.cachePromiseRejection === false) {
if (isPromise(cacheItem) && options.cachePromiseRejection === false) {
// Remove rejected promises from cache unless `cachePromiseRejection` is set to `true`
ret.catch(() => cache.delete(key));
cacheItem.catch(() => cache.delete(key));
}
return ret;
return cacheItem;
};

@@ -83,2 +81,5 @@

module.exports = mem;
module.exports.default = mem;
module.exports.clear = fn => {

@@ -85,0 +86,0 @@ const cache = cacheStore.get(fn);

{
"name": "mem",
"version": "4.1.0",
"version": "4.2.0",
"description": "Memoize functions - An optimization used to speed up consecutive function calls by caching the result of calls with identical input",

@@ -16,6 +16,7 @@ "license": "MIT",

"scripts": {
"test": "xo && ava"
"test": "xo && ava && tsd-check"
},
"files": [
"index.js"
"index.js",
"index.d.ts"
],

@@ -37,10 +38,11 @@ "keywords": [

"map-age-cleaner": "^0.1.1",
"mimic-fn": "^1.0.0",
"mimic-fn": "^2.0.0",
"p-is-promise": "^2.0.0"
},
"devDependencies": {
"ava": "^1.0.1",
"ava": "^1.3.1",
"delay": "^4.1.0",
"xo": "^0.23.0"
"tsd-check": "^0.3.0",
"xo": "^0.24.0"
}
}