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;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue