mirror of
https://github.com/zhigang1992/DefinitelyTyped.git
synced 2026-06-01 03:20:46 +08:00
Merge pull request #25773 from forabi/add-p-memoize
Add types for p-memoize
This commit is contained in:
56
types/p-memoize/index.d.ts
vendored
Normal file
56
types/p-memoize/index.d.ts
vendored
Normal file
@@ -0,0 +1,56 @@
|
||||
// Type definitions for p-memoize 1.0
|
||||
// Project: https://github.com/sindresorhus/p-memoize#readme
|
||||
// Definitions by: forabi <https://github.com/forabi>
|
||||
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
|
||||
// TypeScript Version: 2.3
|
||||
|
||||
interface MemOptions {
|
||||
/**
|
||||
* Milliseconds until the cache expires.
|
||||
* @default Infinity
|
||||
*/
|
||||
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, 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)`.
|
||||
*/
|
||||
cacheKey?: (...args: any[]) => string;
|
||||
|
||||
/**
|
||||
* 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` for a LRU cache.
|
||||
*
|
||||
* @default new Map()
|
||||
*/
|
||||
cache?: pMemoize.Cache;
|
||||
|
||||
/** Cache rejected promises. */
|
||||
cachePromiseRejection?: boolean;
|
||||
}
|
||||
|
||||
type PMemoize = <T extends (...args: any[]) => any>(
|
||||
f: T,
|
||||
memoizeOptions?: MemOptions
|
||||
) => T;
|
||||
|
||||
declare const pMemoize: PMemoize;
|
||||
|
||||
declare namespace pMemoize {
|
||||
interface Cache<K = string, V = any> {
|
||||
get(key: K): V;
|
||||
set(key: K, value: V): void;
|
||||
has(key: K): boolean;
|
||||
delete(key: K): void;
|
||||
clear?(): void;
|
||||
}
|
||||
}
|
||||
|
||||
export = pMemoize;
|
||||
13
types/p-memoize/p-memoize-tests.ts
Normal file
13
types/p-memoize/p-memoize-tests.ts
Normal file
@@ -0,0 +1,13 @@
|
||||
import pMemoize = require('p-memoize');
|
||||
import { Cache } from 'p-memoize';
|
||||
|
||||
const a = pMemoize(async () => Promise.resolve('Hello world!'));
|
||||
|
||||
const b = pMemoize(async () => Promise.resolve(1), {
|
||||
maxAge: 1000,
|
||||
cache: new Map()
|
||||
});
|
||||
|
||||
a();
|
||||
|
||||
b();
|
||||
16
types/p-memoize/tsconfig.json
Normal file
16
types/p-memoize/tsconfig.json
Normal file
@@ -0,0 +1,16 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"module": "commonjs",
|
||||
"lib": ["es6"],
|
||||
"noImplicitAny": true,
|
||||
"noImplicitThis": true,
|
||||
"strictNullChecks": true,
|
||||
"baseUrl": "../",
|
||||
"typeRoots": ["../"],
|
||||
"types": [],
|
||||
"noEmit": true,
|
||||
"forceConsistentCasingInFileNames": true,
|
||||
"strictFunctionTypes": true
|
||||
},
|
||||
"files": ["index.d.ts", "p-memoize-tests.ts"]
|
||||
}
|
||||
1
types/p-memoize/tslint.json
Normal file
1
types/p-memoize/tslint.json
Normal file
@@ -0,0 +1 @@
|
||||
{ "extends": "dtslint/dt.json" }
|
||||
Reference in New Issue
Block a user