From d01a2626f152b58c46e3bcc9c0417846c372c324 Mon Sep 17 00:00:00 2001 From: Dimitri Benin Date: Mon, 25 Mar 2019 22:49:31 +0100 Subject: [PATCH 1/6] Refactor TypeScript definition to use CJS compatible export --- index.d.ts | 125 +++++++++++++++++++++++++++++------------------- index.test-d.ts | 4 +- package.json | 6 +-- 3 files changed, 80 insertions(+), 55 deletions(-) diff --git a/index.d.ts b/index.d.ts index 4a56271..1ecb3ce 100644 --- a/index.d.ts +++ b/index.d.ts @@ -1,71 +1,96 @@ -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; -} +declare namespace mem { + interface CacheStorage { + 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; + interface Options< + ArgumentsType extends unknown[], + CacheKeyType extends unknown, + ReturnType extends unknown + > { + /** + Milliseconds until the cache expires. - /** - * 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; + @default Infinity + */ + readonly maxAge?: number; - /** - * 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; + /** + 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. - /** - * Cache rejected promises. - * - * @default false - */ - readonly cachePromiseRejection?: boolean; + 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. 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; + + /** + 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: { + /** + [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. + + @example + ``` + import mem = require('mem'); + + let i = 0; + const counter = () => ++i; + const memoized = mem(counter); + + memoized('foo'); + //=> 1 + + // Cached as it's the same arguments + memoized('foo'); + //=> 1 + + // Not cached anymore as the arguments changed + memoized('bar'); + //=> 2 + + memoized('bar'); + //=> 2 + ``` + */ < ArgumentsType extends unknown[], ReturnType extends unknown, CacheKeyType extends unknown >( fn: (...arguments: ArgumentsType) => ReturnType, - options?: Options + options?: mem.Options ): (...arguments: ArgumentsType) => ReturnType; /** - * Clear all cached data of a memoized function. - * - * @param fn - Memoized function. - */ + Clear all cached data of a memoized function. + + @param fn - Memoized function. + */ clear( fn: (...arguments: ArgumentsType) => ReturnType ): void; + + // TODO: remove this on next major release + default: typeof mem; }; -export default mem; +export = mem; diff --git a/index.test-d.ts b/index.test-d.ts index 3ba568e..62098ad 100644 --- a/index.test-d.ts +++ b/index.test-d.ts @@ -1,5 +1,5 @@ -import {expectType} from 'tsd-check'; -import mem from '.'; +import {expectType} from 'tsd'; +import mem = require('.'); const fn = (string: string) => true; diff --git a/package.json b/package.json index b2bc386..1509644 100644 --- a/package.json +++ b/package.json @@ -13,7 +13,7 @@ "node": ">=6" }, "scripts": { - "test": "xo && ava && tsd-check" + "test": "xo && ava && tsd" }, "files": [ "index.js", @@ -38,9 +38,9 @@ "p-is-promise": "^2.0.0" }, "devDependencies": { - "ava": "^1.3.1", + "ava": "^1.4.0", "delay": "^4.1.0", - "tsd-check": "^0.3.0", + "tsd": "^0.7.0", "xo": "^0.24.0" } } From 5b744a43668571ff2068662dec0a11a49ca351bf Mon Sep 17 00:00:00 2001 From: Dimitri Benin Date: Fri, 29 Mar 2019 08:23:20 +0100 Subject: [PATCH 2/6] Update dev deps --- package.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index 1509644..9ad882d 100644 --- a/package.json +++ b/package.json @@ -38,9 +38,9 @@ "p-is-promise": "^2.0.0" }, "devDependencies": { - "ava": "^1.4.0", + "ava": "^1.4.1", "delay": "^4.1.0", - "tsd": "^0.7.0", + "tsd": "^0.7.1", "xo": "^0.24.0" } } From 7792387e5ba9e4cf5890c5dbf86f07873e54b5dc Mon Sep 17 00:00:00 2001 From: Dimitri Benin Date: Fri, 29 Mar 2019 16:35:38 +0100 Subject: [PATCH 3/6] Document reasons for legacy import syntax --- index.test-d.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/index.test-d.ts b/index.test-d.ts index 62098ad..a26bb22 100644 --- a/index.test-d.ts +++ b/index.test-d.ts @@ -1,4 +1,5 @@ import {expectType} from 'tsd'; +// Following import syntax makes sure that type intellisense interop with plain JS isn't broken import mem = require('.'); const fn = (string: string) => true; From d7077c673b7f0552a4773d30e04a8441f434bef9 Mon Sep 17 00:00:00 2001 From: Sindre Sorhus Date: Sun, 31 Mar 2019 16:56:01 +0700 Subject: [PATCH 4/6] Update index.d.ts --- index.d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.d.ts b/index.d.ts index 1ecb3ce..4b75564 100644 --- a/index.d.ts +++ b/index.d.ts @@ -89,7 +89,7 @@ declare const mem: { fn: (...arguments: ArgumentsType) => ReturnType ): void; - // TODO: remove this on next major release + // TODO: Remove this for the next major release default: typeof mem; }; From bf00164a8da769a8cb81fd47ea8c5d58fc9d9bf7 Mon Sep 17 00:00:00 2001 From: Dimitri Benin Date: Sun, 31 Mar 2019 19:12:24 +0200 Subject: [PATCH 5/6] Fixes after review --- index.js | 1 + index.test-d.ts | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index a300a51..b6a0bff 100644 --- a/index.js +++ b/index.js @@ -76,6 +76,7 @@ const mem = (fn, options) => { }; module.exports = mem; +// TODO: remove this in the next major version module.exports.default = mem; module.exports.clear = fn => { diff --git a/index.test-d.ts b/index.test-d.ts index 2ced90c..7a6d5b5 100644 --- a/index.test-d.ts +++ b/index.test-d.ts @@ -1,5 +1,5 @@ import {expectType} from 'tsd'; -// Following import syntax makes sure that type intellisense interop with plain JS isn't broken +// The following import syntax makes sure that the type IntelliSense interop with plain JS isn't broken import mem = require('.'); const fn = (string: string) => true; From 9a4331c24e360c3316dc8723af1fc27d200e8b36 Mon Sep 17 00:00:00 2001 From: Dimitri Benin Date: Sun, 31 Mar 2019 19:15:38 +0200 Subject: [PATCH 6/6] Fix wording --- index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.js b/index.js index b6a0bff..51faf01 100644 --- a/index.js +++ b/index.js @@ -76,7 +76,7 @@ const mem = (fn, options) => { }; module.exports = mem; -// TODO: remove this in the next major version +// TODO: Remove this for the next major release module.exports.default = mem; module.exports.clear = fn => {