Upgrade Ava to v4
This commit is contained in:
parent
9a40cc5274
commit
ce89f1b611
1153 changed files with 27264 additions and 95308 deletions
84
node_modules/mem/dist/index.d.ts
generated
vendored
84
node_modules/mem/dist/index.d.ts
generated
vendored
|
|
@ -10,7 +10,7 @@ interface CacheStorage<KeyType, ValueType> {
|
|||
delete: (key: KeyType) => void;
|
||||
clear?: () => void;
|
||||
}
|
||||
interface Options<FunctionToMemoize extends AnyFunction, CacheKeyType> {
|
||||
export interface Options<FunctionToMemoize extends AnyFunction, CacheKeyType> {
|
||||
/**
|
||||
Milliseconds until the cache expires.
|
||||
|
||||
|
|
@ -25,7 +25,7 @@ interface Options<FunctionToMemoize extends AnyFunction, CacheKeyType> {
|
|||
You can have it cache **all** the arguments by value with `JSON.stringify`, if they are compatible:
|
||||
|
||||
```
|
||||
import mem = require('mem');
|
||||
import mem from 'mem';
|
||||
|
||||
mem(function_, {cacheKey: JSON.stringify});
|
||||
```
|
||||
|
|
@ -33,8 +33,8 @@ interface Options<FunctionToMemoize extends AnyFunction, CacheKeyType> {
|
|||
Or you can use a more full-featured serializer like [serialize-javascript](https://github.com/yahoo/serialize-javascript) to add support for `RegExp`, `Date` and so on.
|
||||
|
||||
```
|
||||
import mem = require('mem');
|
||||
import serializeJavascript = require('serialize-javascript');
|
||||
import mem from 'mem';
|
||||
import serializeJavascript from 'serialize-javascript';
|
||||
|
||||
mem(function_, {cacheKey: serializeJavascript});
|
||||
```
|
||||
|
|
@ -58,16 +58,16 @@ interface Options<FunctionToMemoize extends AnyFunction, CacheKeyType> {
|
|||
|
||||
@example
|
||||
```
|
||||
import mem = require('mem');
|
||||
import mem from 'mem';
|
||||
|
||||
let i = 0;
|
||||
const counter = () => ++i;
|
||||
let index = 0;
|
||||
const counter = () => ++index;
|
||||
const memoized = mem(counter);
|
||||
|
||||
memoized('foo');
|
||||
//=> 1
|
||||
|
||||
// Cached as it's the same arguments
|
||||
// Cached as it's the same argument
|
||||
memoized('foo');
|
||||
//=> 1
|
||||
|
||||
|
|
@ -79,40 +79,38 @@ memoized('bar');
|
|||
//=> 2
|
||||
```
|
||||
*/
|
||||
declare const mem: {
|
||||
<FunctionToMemoize extends AnyFunction, CacheKeyType>(fn: FunctionToMemoize, { cacheKey, cache, maxAge }?: Options<FunctionToMemoize, CacheKeyType>): FunctionToMemoize;
|
||||
/**
|
||||
@returns A [decorator](https://github.com/tc39/proposal-decorators) to memoize class methods or static class methods.
|
||||
|
||||
@example
|
||||
```
|
||||
import mem = require('mem');
|
||||
|
||||
class Example {
|
||||
index = 0
|
||||
|
||||
@mem.decorator()
|
||||
counter() {
|
||||
return ++this.index;
|
||||
}
|
||||
export default function mem<FunctionToMemoize extends AnyFunction, CacheKeyType>(fn: FunctionToMemoize, { cacheKey, cache, maxAge, }?: Options<FunctionToMemoize, CacheKeyType>): FunctionToMemoize;
|
||||
/**
|
||||
@returns A [decorator](https://github.com/tc39/proposal-decorators) to memoize class methods or static class methods.
|
||||
|
||||
@example
|
||||
```
|
||||
import {memDecorator} from 'mem';
|
||||
|
||||
class Example {
|
||||
index = 0
|
||||
|
||||
@memDecorator()
|
||||
counter() {
|
||||
return ++this.index;
|
||||
}
|
||||
|
||||
class ExampleWithOptions {
|
||||
index = 0
|
||||
|
||||
@mem.decorator({maxAge: 1000})
|
||||
counter() {
|
||||
return ++this.index;
|
||||
}
|
||||
}
|
||||
|
||||
class ExampleWithOptions {
|
||||
index = 0
|
||||
|
||||
@memDecorator({maxAge: 1000})
|
||||
counter() {
|
||||
return ++this.index;
|
||||
}
|
||||
```
|
||||
*/
|
||||
decorator<FunctionToMemoize_1 extends AnyFunction, CacheKeyType_1>(options?: Options<FunctionToMemoize_1, CacheKeyType_1>): (target: any, propertyKey: string, descriptor: PropertyDescriptor) => void;
|
||||
/**
|
||||
Clear all cached data of a memoized function.
|
||||
|
||||
@param fn - Memoized function.
|
||||
*/
|
||||
clear(fn: AnyFunction): void;
|
||||
};
|
||||
export = mem;
|
||||
}
|
||||
```
|
||||
*/
|
||||
export declare function memDecorator<FunctionToMemoize extends AnyFunction, CacheKeyType>(options?: Options<FunctionToMemoize, CacheKeyType>): (target: any, propertyKey: string, descriptor: PropertyDescriptor) => void;
|
||||
/**
|
||||
Clear all cached data of a memoized function.
|
||||
|
||||
@param fn - Memoized function.
|
||||
*/
|
||||
export declare function memClear(fn: AnyFunction): void;
|
||||
export {};
|
||||
|
|
|
|||
70
node_modules/mem/dist/index.js
generated
vendored
70
node_modules/mem/dist/index.js
generated
vendored
|
|
@ -1,7 +1,5 @@
|
|||
'use strict';
|
||||
const mimicFn = require("mimic-fn");
|
||||
const mapAgeCleaner = require("map-age-cleaner");
|
||||
const decoratorInstanceMap = new WeakMap();
|
||||
import mimicFn from 'mimic-fn';
|
||||
import mapAgeCleaner from 'map-age-cleaner';
|
||||
const cacheStore = new WeakMap();
|
||||
/**
|
||||
[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.
|
||||
|
|
@ -10,16 +8,16 @@ const cacheStore = new WeakMap();
|
|||
|
||||
@example
|
||||
```
|
||||
import mem = require('mem');
|
||||
import mem from 'mem';
|
||||
|
||||
let i = 0;
|
||||
const counter = () => ++i;
|
||||
let index = 0;
|
||||
const counter = () => ++index;
|
||||
const memoized = mem(counter);
|
||||
|
||||
memoized('foo');
|
||||
//=> 1
|
||||
|
||||
// Cached as it's the same arguments
|
||||
// Cached as it's the same argument
|
||||
memoized('foo');
|
||||
//=> 1
|
||||
|
||||
|
|
@ -31,42 +29,40 @@ memoized('bar');
|
|||
//=> 2
|
||||
```
|
||||
*/
|
||||
const mem = (fn, { cacheKey, cache = new Map(), maxAge } = {}) => {
|
||||
export default function mem(fn, { cacheKey, cache = new Map(), maxAge, } = {}) {
|
||||
if (typeof maxAge === 'number') {
|
||||
// TODO: Drop after https://github.com/SamVerschueren/map-age-cleaner/issues/5
|
||||
// @ts-expect-error
|
||||
mapAgeCleaner(cache);
|
||||
}
|
||||
const memoized = function (...arguments_) {
|
||||
const key = cacheKey ? cacheKey(arguments_) : arguments_[0];
|
||||
const cacheItem = cache.get(key);
|
||||
if (cacheItem) {
|
||||
return cacheItem.data;
|
||||
return cacheItem.data; // eslint-disable-line @typescript-eslint/no-unsafe-return
|
||||
}
|
||||
const result = fn.apply(this, arguments_);
|
||||
cache.set(key, {
|
||||
data: result,
|
||||
maxAge: maxAge ? Date.now() + maxAge : Number.POSITIVE_INFINITY
|
||||
maxAge: maxAge ? Date.now() + maxAge : Number.POSITIVE_INFINITY,
|
||||
});
|
||||
return result;
|
||||
return result; // eslint-disable-line @typescript-eslint/no-unsafe-return
|
||||
};
|
||||
mimicFn(memoized, fn, {
|
||||
ignoreNonConfigurable: true
|
||||
ignoreNonConfigurable: true,
|
||||
});
|
||||
cacheStore.set(memoized, cache);
|
||||
return memoized;
|
||||
};
|
||||
}
|
||||
/**
|
||||
@returns A [decorator](https://github.com/tc39/proposal-decorators) to memoize class methods or static class methods.
|
||||
|
||||
@example
|
||||
```
|
||||
import mem = require('mem');
|
||||
import {memDecorator} from 'mem';
|
||||
|
||||
class Example {
|
||||
index = 0
|
||||
|
||||
@mem.decorator()
|
||||
@memDecorator()
|
||||
counter() {
|
||||
return ++this.index;
|
||||
}
|
||||
|
|
@ -75,35 +71,38 @@ class Example {
|
|||
class ExampleWithOptions {
|
||||
index = 0
|
||||
|
||||
@mem.decorator({maxAge: 1000})
|
||||
@memDecorator({maxAge: 1000})
|
||||
counter() {
|
||||
return ++this.index;
|
||||
}
|
||||
}
|
||||
```
|
||||
*/
|
||||
mem.decorator = (options = {}) => (target, propertyKey, descriptor) => {
|
||||
const input = target[propertyKey];
|
||||
if (typeof input !== 'function') {
|
||||
throw new TypeError('The decorated value must be a function');
|
||||
}
|
||||
delete descriptor.value;
|
||||
delete descriptor.writable;
|
||||
descriptor.get = function () {
|
||||
if (!decoratorInstanceMap.has(this)) {
|
||||
const value = mem(input, options);
|
||||
decoratorInstanceMap.set(this, value);
|
||||
return value;
|
||||
export function memDecorator(options = {}) {
|
||||
const instanceMap = new WeakMap();
|
||||
return (target, propertyKey, descriptor) => {
|
||||
const input = target[propertyKey]; // eslint-disable-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-member-access
|
||||
if (typeof input !== 'function') {
|
||||
throw new TypeError('The decorated value must be a function');
|
||||
}
|
||||
return decoratorInstanceMap.get(this);
|
||||
delete descriptor.value;
|
||||
delete descriptor.writable;
|
||||
descriptor.get = function () {
|
||||
if (!instanceMap.has(this)) {
|
||||
const value = mem(input, options);
|
||||
instanceMap.set(this, value);
|
||||
return value;
|
||||
}
|
||||
return instanceMap.get(this);
|
||||
};
|
||||
};
|
||||
};
|
||||
}
|
||||
/**
|
||||
Clear all cached data of a memoized function.
|
||||
|
||||
@param fn - Memoized function.
|
||||
*/
|
||||
mem.clear = (fn) => {
|
||||
export function memClear(fn) {
|
||||
const cache = cacheStore.get(fn);
|
||||
if (!cache) {
|
||||
throw new TypeError('Can\'t clear a function that was not memoized!');
|
||||
|
|
@ -112,5 +111,4 @@ mem.clear = (fn) => {
|
|||
throw new TypeError('The cache Map can\'t be cleared!');
|
||||
}
|
||||
cache.clear();
|
||||
};
|
||||
module.exports = mem;
|
||||
}
|
||||
|
|
|
|||
24
node_modules/mem/node_modules/mimic-fn/index.d.ts
generated
vendored
24
node_modules/mem/node_modules/mimic-fn/index.d.ts
generated
vendored
|
|
@ -1,12 +1,10 @@
|
|||
declare namespace mimicFn {
|
||||
interface Options {
|
||||
/**
|
||||
Skip modifying [non-configurable properties](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/getOwnPropertyDescriptor#Description) instead of throwing an error.
|
||||
export interface Options {
|
||||
/**
|
||||
Skip modifying [non-configurable properties](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/getOwnPropertyDescriptor#Description) instead of throwing an error.
|
||||
|
||||
@default false
|
||||
*/
|
||||
readonly ignoreNonConfigurable?: boolean;
|
||||
}
|
||||
@default false
|
||||
*/
|
||||
readonly ignoreNonConfigurable?: boolean;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -22,7 +20,7 @@ Modifies the `to` function to mimic the `from` function. Returns the `to` functi
|
|||
|
||||
@example
|
||||
```
|
||||
import mimicFn = require('mimic-fn');
|
||||
import mimicFunction from 'mimic-fn';
|
||||
|
||||
function foo() {}
|
||||
foo.unicorn = '🦄';
|
||||
|
|
@ -34,7 +32,7 @@ function wrapper() {
|
|||
console.log(wrapper.name);
|
||||
//=> 'wrapper'
|
||||
|
||||
mimicFn(wrapper, foo);
|
||||
mimicFunction(wrapper, foo);
|
||||
|
||||
console.log(wrapper.name);
|
||||
//=> 'foo'
|
||||
|
|
@ -43,14 +41,12 @@ console.log(wrapper.unicorn);
|
|||
//=> '🦄'
|
||||
```
|
||||
*/
|
||||
declare function mimicFn<
|
||||
export default function mimicFunction<
|
||||
ArgumentsType extends unknown[],
|
||||
ReturnType,
|
||||
FunctionType extends (...arguments: ArgumentsType) => ReturnType
|
||||
>(
|
||||
to: (...arguments: ArgumentsType) => ReturnType,
|
||||
from: FunctionType,
|
||||
options?: mimicFn.Options,
|
||||
options?: Options,
|
||||
): FunctionType;
|
||||
|
||||
export = mimicFn;
|
||||
|
|
|
|||
12
node_modules/mem/node_modules/mimic-fn/index.js
generated
vendored
12
node_modules/mem/node_modules/mimic-fn/index.js
generated
vendored
|
|
@ -1,5 +1,3 @@
|
|||
'use strict';
|
||||
|
||||
const copyProperty = (to, from, property, ignoreNonConfigurable) => {
|
||||
// `Function#length` should reflect the parameters of `to` not `from` since we keep its body.
|
||||
// `Function#prototype` is non-writable and non-configurable so can never be modified.
|
||||
|
|
@ -23,8 +21,8 @@ const copyProperty = (to, from, property, ignoreNonConfigurable) => {
|
|||
};
|
||||
|
||||
// `Object.defineProperty()` throws if the property exists, is not configurable and either:
|
||||
// - one its descriptors is changed
|
||||
// - it is non-writable and its value is changed
|
||||
// - one its descriptors is changed
|
||||
// - it is non-writable and its value is changed
|
||||
const canCopyProperty = function (toDescriptor, fromDescriptor) {
|
||||
return toDescriptor === undefined || toDescriptor.configurable || (
|
||||
toDescriptor.writable === fromDescriptor.writable &&
|
||||
|
|
@ -59,7 +57,7 @@ const changeToString = (to, from, name) => {
|
|||
Object.defineProperty(to, 'toString', {...toStringDescriptor, value: newToString});
|
||||
};
|
||||
|
||||
const mimicFn = (to, from, {ignoreNonConfigurable = false} = {}) => {
|
||||
export default function mimicFunction(to, from, {ignoreNonConfigurable = false} = {}) {
|
||||
const {name} = to;
|
||||
|
||||
for (const property of Reflect.ownKeys(from)) {
|
||||
|
|
@ -70,6 +68,4 @@ const mimicFn = (to, from, {ignoreNonConfigurable = false} = {}) => {
|
|||
changeToString(to, from, name);
|
||||
|
||||
return to;
|
||||
};
|
||||
|
||||
module.exports = mimicFn;
|
||||
}
|
||||
|
|
|
|||
2
node_modules/mem/node_modules/mimic-fn/license
generated
vendored
2
node_modules/mem/node_modules/mimic-fn/license
generated
vendored
|
|
@ -1,6 +1,6 @@
|
|||
MIT License
|
||||
|
||||
Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (sindresorhus.com)
|
||||
Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (https://sindresorhus.com)
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
||||
|
||||
|
|
|
|||
15
node_modules/mem/node_modules/mimic-fn/package.json
generated
vendored
15
node_modules/mem/node_modules/mimic-fn/package.json
generated
vendored
|
|
@ -1,16 +1,19 @@
|
|||
{
|
||||
"name": "mimic-fn",
|
||||
"version": "3.1.0",
|
||||
"version": "4.0.0",
|
||||
"description": "Make a function mimic another one",
|
||||
"license": "MIT",
|
||||
"repository": "sindresorhus/mimic-fn",
|
||||
"funding": "https://github.com/sponsors/sindresorhus",
|
||||
"author": {
|
||||
"name": "Sindre Sorhus",
|
||||
"email": "sindresorhus@gmail.com",
|
||||
"url": "sindresorhus.com"
|
||||
"url": "https://sindresorhus.com"
|
||||
},
|
||||
"type": "module",
|
||||
"exports": "./index.js",
|
||||
"engines": {
|
||||
"node": ">=8"
|
||||
"node": ">=12"
|
||||
},
|
||||
"scripts": {
|
||||
"test": "xo && ava && tsd"
|
||||
|
|
@ -35,8 +38,8 @@
|
|||
"change"
|
||||
],
|
||||
"devDependencies": {
|
||||
"ava": "^2.1.0",
|
||||
"tsd": "^0.7.1",
|
||||
"xo": "^0.24.0"
|
||||
"ava": "^3.15.0",
|
||||
"tsd": "^0.14.0",
|
||||
"xo": "^0.38.2"
|
||||
}
|
||||
}
|
||||
13
node_modules/mem/node_modules/mimic-fn/readme.md
generated
vendored
13
node_modules/mem/node_modules/mimic-fn/readme.md
generated
vendored
|
|
@ -1,24 +1,20 @@
|
|||
<img src="media/logo.svg" alt="mimic-fn" width="400">
|
||||
<br>
|
||||
|
||||
[](https://travis-ci.org/sindresorhus/mimic-fn)
|
||||
|
||||
> Make a function mimic another one
|
||||
|
||||
Useful when you wrap a function in another function and like to preserve the original name and other properties.
|
||||
|
||||
|
||||
## Install
|
||||
|
||||
```
|
||||
$ npm install mimic-fn
|
||||
```
|
||||
|
||||
|
||||
## Usage
|
||||
|
||||
```js
|
||||
const mimicFn = require('mimic-fn');
|
||||
import mimicFunction from 'mimic-fn';
|
||||
|
||||
function foo() {}
|
||||
foo.unicorn = '🦄';
|
||||
|
|
@ -30,7 +26,7 @@ function wrapper() {
|
|||
console.log(wrapper.name);
|
||||
//=> 'wrapper'
|
||||
|
||||
mimicFn(wrapper, foo);
|
||||
mimicFunction(wrapper, foo);
|
||||
|
||||
console.log(wrapper.name);
|
||||
//=> 'foo'
|
||||
|
|
@ -45,7 +41,7 @@ console.log(String(wrapper));
|
|||
|
||||
## API
|
||||
|
||||
### mimicFn(to, from, options?)
|
||||
### mimicFunction(to, from, options?)
|
||||
|
||||
Modifies the `to` function to mimic the `from` function. Returns the `to` function.
|
||||
|
||||
|
|
@ -71,7 +67,7 @@ Type: `object`
|
|||
|
||||
##### ignoreNonConfigurable
|
||||
|
||||
Type: `boolean`<br>
|
||||
Type: `boolean`\
|
||||
Default: `false`
|
||||
|
||||
Skip modifying [non-configurable properties](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/getOwnPropertyDescriptor#Description) instead of throwing an error.
|
||||
|
|
@ -81,7 +77,6 @@ Skip modifying [non-configurable properties](https://developer.mozilla.org/en-US
|
|||
- [rename-fn](https://github.com/sindresorhus/rename-fn) - Rename a function
|
||||
- [keep-func-props](https://github.com/ehmicky/keep-func-props) - Wrap a function without changing its name and other properties
|
||||
|
||||
|
||||
---
|
||||
|
||||
<div align="center">
|
||||
|
|
|
|||
38
node_modules/mem/package.json
generated
vendored
38
node_modules/mem/package.json
generated
vendored
|
|
@ -1,6 +1,6 @@
|
|||
{
|
||||
"name": "mem",
|
||||
"version": "8.1.1",
|
||||
"version": "9.0.2",
|
||||
"description": "Memoize functions - An optimization used to speed up consecutive function calls by caching the result of calls with identical input",
|
||||
"license": "MIT",
|
||||
"repository": "sindresorhus/mem",
|
||||
|
|
@ -10,19 +10,19 @@
|
|||
"email": "sindresorhus@gmail.com",
|
||||
"url": "https://sindresorhus.com"
|
||||
},
|
||||
"type": "module",
|
||||
"exports": "./dist/index.js",
|
||||
"engines": {
|
||||
"node": ">=10"
|
||||
"node": ">=12.20"
|
||||
},
|
||||
"scripts": {
|
||||
"test": "xo && npm run build && tsd && ava",
|
||||
"test": "xo && ava && npm run build && tsd",
|
||||
"build": "del-cli dist && tsc",
|
||||
"prepack": "npm run build"
|
||||
},
|
||||
"main": "dist",
|
||||
"types": "dist/index.d.ts",
|
||||
"files": [
|
||||
"dist/index.js",
|
||||
"dist/index.d.ts"
|
||||
"dist"
|
||||
],
|
||||
"keywords": [
|
||||
"memoize",
|
||||
|
|
@ -39,30 +39,32 @@
|
|||
],
|
||||
"dependencies": {
|
||||
"map-age-cleaner": "^0.1.3",
|
||||
"mimic-fn": "^3.1.0"
|
||||
"mimic-fn": "^4.0.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@ava/typescript": "^1.1.1",
|
||||
"@sindresorhus/tsconfig": "^0.7.0",
|
||||
"@sindresorhus/tsconfig": "^1.0.2",
|
||||
"@types/serialize-javascript": "^4.0.0",
|
||||
"ava": "^3.15.0",
|
||||
"del-cli": "^3.0.1",
|
||||
"delay": "^4.4.0",
|
||||
"serialize-javascript": "^5.0.1",
|
||||
"ts-node": "^10.1.0",
|
||||
"tsd": "^0.13.1",
|
||||
"typescript": "^4.0.3",
|
||||
"xo": "^0.38.2"
|
||||
"typescript": "^4.3.5",
|
||||
"xo": "^0.41.0"
|
||||
},
|
||||
"ava": {
|
||||
"files": [
|
||||
"test.ts"
|
||||
],
|
||||
"timeout": "1m",
|
||||
"typescript": {
|
||||
"rewritePaths": {
|
||||
"./": "dist/"
|
||||
}
|
||||
}
|
||||
"extensions": {
|
||||
"ts": "module"
|
||||
},
|
||||
"nonSemVerExperiments": {
|
||||
"configurableModuleFormat": true
|
||||
},
|
||||
"nodeArguments": [
|
||||
"--loader=ts-node/esm"
|
||||
]
|
||||
},
|
||||
"xo": {
|
||||
"rules": {
|
||||
|
|
|
|||
84
node_modules/mem/readme.md
generated
vendored
84
node_modules/mem/readme.md
generated
vendored
|
|
@ -4,7 +4,11 @@
|
|||
|
||||
Memory is automatically released when an item expires or the cache is cleared.
|
||||
|
||||
By default, **only the first argument is considered** and it only works with [primitives](https://developer.mozilla.org/en-US/docs/Glossary/Primitive). If you need to cache multiple arguments or cache `object`s *by value*, have a look at alternative [caching strategies](#caching-strategy) below.
|
||||
<!-- Please keep this section in sync with https://github.com/sindresorhus/p-memoize/blob/main/readme.md -->
|
||||
|
||||
By default, **only the memoized function's first argument is considered** via strict equality comparison. If you need to cache multiple arguments or cache `object`s *by value*, have a look at alternative [caching strategies](#caching-strategy) below.
|
||||
|
||||
If you want to memoize Promise-returning functions (like `async` functions), you might be better served by [p-memoize](https://github.com/sindresorhus/p-memoize).
|
||||
|
||||
## Install
|
||||
|
||||
|
|
@ -15,10 +19,10 @@ $ npm install mem
|
|||
## Usage
|
||||
|
||||
```js
|
||||
const mem = require('mem');
|
||||
import mem from 'mem';
|
||||
|
||||
let i = 0;
|
||||
const counter = () => ++i;
|
||||
let index = 0;
|
||||
const counter = () => ++index;
|
||||
const memoized = mem(counter);
|
||||
|
||||
memoized('foo');
|
||||
|
|
@ -40,43 +44,41 @@ memoized('bar', 'foo');
|
|||
//=> 2
|
||||
```
|
||||
|
||||
##### Works fine with promise returning functions
|
||||
##### Works well with Promise-returning functions
|
||||
|
||||
But you might want to use [p-memoize](https://github.com/sindresorhus/p-memoize) for more Promise-specific behaviors.
|
||||
|
||||
```js
|
||||
const mem = require('mem');
|
||||
import mem from 'mem';
|
||||
|
||||
let i = 0;
|
||||
const counter = async () => ++i;
|
||||
let index = 0;
|
||||
const counter = async () => ++index;
|
||||
const memoized = mem(counter);
|
||||
|
||||
(async () => {
|
||||
console.log(await memoized());
|
||||
//=> 1
|
||||
console.log(await memoized());
|
||||
//=> 1
|
||||
|
||||
// The return value didn't increase as it's cached
|
||||
console.log(await memoized());
|
||||
//=> 1
|
||||
})();
|
||||
// The return value didn't increase as it's cached
|
||||
console.log(await memoized());
|
||||
//=> 1
|
||||
```
|
||||
|
||||
```js
|
||||
const mem = require('mem');
|
||||
const got = require('got');
|
||||
const delay = require('delay');
|
||||
import mem from 'mem';
|
||||
import got from 'got';
|
||||
import delay from 'delay';
|
||||
|
||||
const memGot = mem(got, {maxAge: 1000});
|
||||
|
||||
(async () => {
|
||||
await memGot('https://sindresorhus.com');
|
||||
await memGot('https://sindresorhus.com');
|
||||
|
||||
// This call is cached
|
||||
await memGot('https://sindresorhus.com');
|
||||
// This call is cached
|
||||
await memGot('https://sindresorhus.com');
|
||||
|
||||
await delay(2000);
|
||||
await delay(2000);
|
||||
|
||||
// This call is not cached as the cache has expired
|
||||
await memGot('https://sindresorhus.com');
|
||||
})();
|
||||
// This call is not cached as the cache has expired
|
||||
await memGot('https://sindresorhus.com');
|
||||
```
|
||||
|
||||
### Caching strategy
|
||||
|
|
@ -138,7 +140,7 @@ heavyMemoizedOperation('hello', {full: true}); // Retrieved from cache
|
|||
If your function accepts multiple arguments that aren't supported by `JSON.stringify` (e.g. DOM elements and functions), you can instead extend the initial exact equality (`===`) to work on multiple arguments using [`many-keys-map`](https://github.com/fregante/many-keys-map):
|
||||
|
||||
```js
|
||||
const ManyKeysMap = require('many-keys-map');
|
||||
import ManyKeysMap from 'many-keys-map';
|
||||
|
||||
const addListener = (emitter, eventName, listener) => emitter.on(eventName, listener);
|
||||
|
||||
|
|
@ -196,7 +198,7 @@ Use a different cache storage. Must implement the following methods: `.has(key)`
|
|||
|
||||
Refer to the [caching strategies](#caching-strategy) section for more information.
|
||||
|
||||
### mem.decorator(options)
|
||||
### memDecorator(options)
|
||||
|
||||
Returns a [decorator](https://github.com/tc39/proposal-decorators) to memoize class methods or static class methods.
|
||||
|
||||
|
|
@ -213,12 +215,12 @@ Type: `object`
|
|||
Same as options for `mem()`.
|
||||
|
||||
```ts
|
||||
import mem = require('mem');
|
||||
import {memDecorator} from 'mem';
|
||||
|
||||
class Example {
|
||||
index = 0
|
||||
|
||||
@mem.decorator()
|
||||
@memDecorator()
|
||||
counter() {
|
||||
return ++this.index;
|
||||
}
|
||||
|
|
@ -227,14 +229,14 @@ class Example {
|
|||
class ExampleWithOptions {
|
||||
index = 0
|
||||
|
||||
@mem.decorator({maxAge: 1000})
|
||||
@memDecorator({maxAge: 1000})
|
||||
counter() {
|
||||
return ++this.index;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### mem.clear(fn)
|
||||
### memClear(fn)
|
||||
|
||||
Clear all cached data of a memoized function.
|
||||
|
||||
|
|
@ -253,21 +255,19 @@ If you want to know how many times your cache had a hit or a miss, you can make
|
|||
#### Example
|
||||
|
||||
```js
|
||||
const mem = require('mem');
|
||||
const StatsMap = require('stats-map');
|
||||
const got = require('got');
|
||||
import mem from 'mem';
|
||||
import StatsMap from 'stats-map';
|
||||
import got from 'got';
|
||||
|
||||
const cache = new StatsMap();
|
||||
const memGot = mem(got, {cache});
|
||||
|
||||
(async () => {
|
||||
await memGot('https://sindresorhus.com');
|
||||
await memGot('https://sindresorhus.com');
|
||||
await memGot('https://sindresorhus.com');
|
||||
await memGot('https://sindresorhus.com');
|
||||
await memGot('https://sindresorhus.com');
|
||||
await memGot('https://sindresorhus.com');
|
||||
|
||||
console.log(cache.stats);
|
||||
//=> {hits: 2, misses: 1}
|
||||
})();
|
||||
console.log(cache.stats);
|
||||
//=> {hits: 2, misses: 1}
|
||||
```
|
||||
|
||||
## Related
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue