From 2f1d5dc21fa486a29f141df9ca24721356c77047 Mon Sep 17 00:00:00 2001 From: Muhammad Fawwaz Orabi Date: Mon, 14 May 2018 23:00:21 +0300 Subject: [PATCH 1/2] Add types for p-memoize --- types/p-memoize/index.d.ts | 43 ++++++++++++++++++++++++++++++ types/p-memoize/p-memoize-tests.ts | 0 types/p-memoize/tsconfig.json | 22 +++++++++++++++ types/p-memoize/tslint.json | 1 + 4 files changed, 66 insertions(+) create mode 100644 types/p-memoize/index.d.ts create mode 100644 types/p-memoize/p-memoize-tests.ts create mode 100644 types/p-memoize/tsconfig.json create mode 100644 types/p-memoize/tslint.json diff --git a/types/p-memoize/index.d.ts b/types/p-memoize/index.d.ts new file mode 100644 index 0000000000..a7bb1b129d --- /dev/null +++ b/types/p-memoize/index.d.ts @@ -0,0 +1,43 @@ +// Type definitions for p-memoize 1.0 +// Project: https://github.com/sindresorhus/p-memoize#readme +// Definitions by: forabi +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +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?: Map | WeakMap; + + /** Cache rejected promises. */ + cachePromiseRejection?: boolean; +} + +interface PMemoize { + any>(f: T, memoizeOptions?: MemOptions): T; +} + +declare const pMemoize: PMemoize; + +export = pMemoize; diff --git a/types/p-memoize/p-memoize-tests.ts b/types/p-memoize/p-memoize-tests.ts new file mode 100644 index 0000000000..e69de29bb2 diff --git a/types/p-memoize/tsconfig.json b/types/p-memoize/tsconfig.json new file mode 100644 index 0000000000..de251f82a2 --- /dev/null +++ b/types/p-memoize/tsconfig.json @@ -0,0 +1,22 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictNullChecks": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "p-memoize-tests.ts" + ] +} diff --git a/types/p-memoize/tslint.json b/types/p-memoize/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/p-memoize/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" } From 7640f36d26142183ef124f3edd3505303829fb63 Mon Sep 17 00:00:00 2001 From: Muhammad Fawwaz Orabi Date: Mon, 14 May 2018 23:14:07 +0300 Subject: [PATCH 2/2] Fix issues --- types/p-memoize/index.d.ts | 21 +++++++++++++++++---- types/p-memoize/p-memoize-tests.ts | 13 +++++++++++++ types/p-memoize/tsconfig.json | 16 +++++----------- 3 files changed, 35 insertions(+), 15 deletions(-) diff --git a/types/p-memoize/index.d.ts b/types/p-memoize/index.d.ts index a7bb1b129d..0b997b4b19 100644 --- a/types/p-memoize/index.d.ts +++ b/types/p-memoize/index.d.ts @@ -2,6 +2,8 @@ // Project: https://github.com/sindresorhus/p-memoize#readme // Definitions by: forabi // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +// TypeScript Version: 2.3 + interface MemOptions { /** * Milliseconds until the cache expires. @@ -28,16 +30,27 @@ interface MemOptions { * * @default new Map() */ - cache?: Map | WeakMap; + cache?: pMemoize.Cache; /** Cache rejected promises. */ cachePromiseRejection?: boolean; } -interface PMemoize { - any>(f: T, memoizeOptions?: MemOptions): T; -} +type PMemoize = any>( + f: T, + memoizeOptions?: MemOptions +) => T; declare const pMemoize: PMemoize; +declare namespace pMemoize { + interface Cache { + get(key: K): V; + set(key: K, value: V): void; + has(key: K): boolean; + delete(key: K): void; + clear?(): void; + } +} + export = pMemoize; diff --git a/types/p-memoize/p-memoize-tests.ts b/types/p-memoize/p-memoize-tests.ts index e69de29bb2..8626482dab 100644 --- a/types/p-memoize/p-memoize-tests.ts +++ b/types/p-memoize/p-memoize-tests.ts @@ -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(); diff --git a/types/p-memoize/tsconfig.json b/types/p-memoize/tsconfig.json index de251f82a2..0605de1be9 100644 --- a/types/p-memoize/tsconfig.json +++ b/types/p-memoize/tsconfig.json @@ -1,22 +1,16 @@ { "compilerOptions": { "module": "commonjs", - "lib": [ - "es6" - ], + "lib": ["es6"], "noImplicitAny": true, "noImplicitThis": true, "strictNullChecks": true, "baseUrl": "../", - "typeRoots": [ - "../" - ], + "typeRoots": ["../"], "types": [], "noEmit": true, - "forceConsistentCasingInFileNames": true + "forceConsistentCasingInFileNames": true, + "strictFunctionTypes": true }, - "files": [ - "index.d.ts", - "p-memoize-tests.ts" - ] + "files": ["index.d.ts", "p-memoize-tests.ts"] }