Skip to content

Commit

Permalink
Refactor TypeScript definition to use CJS compatible export (#32)
Browse files Browse the repository at this point in the history
  • Loading branch information
BendingBender authored and sindresorhus committed Mar 31, 2019
1 parent 298a71e commit 9a432b9
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 55 deletions.
125 changes: 75 additions & 50 deletions 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<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;
}

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<CacheKeyType, {data: ReturnType; 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](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<CacheKeyType, {data: ReturnType; maxAge: number}>;

/**
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<ArgumentsType, CacheKeyType, ReturnType>
options?: mem.Options<ArgumentsType, CacheKeyType, ReturnType>
): (...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<ArgumentsType extends unknown[], ReturnType extends unknown>(
fn: (...arguments: ArgumentsType) => ReturnType
): void;

// TODO: Remove this for the next major release
default: typeof mem;
};

export default mem;
export = mem;
1 change: 1 addition & 0 deletions index.js
Expand Up @@ -76,6 +76,7 @@ const mem = (fn, options) => {
};

module.exports = mem;
// TODO: Remove this for the next major release
module.exports.default = mem;

module.exports.clear = fn => {
Expand Down
5 changes: 3 additions & 2 deletions index.test-d.ts
@@ -1,5 +1,6 @@
import {expectType} from 'tsd-check';
import mem from '.';
import {expectType} from 'tsd';
// 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;

Expand Down
6 changes: 3 additions & 3 deletions package.json
Expand Up @@ -13,7 +13,7 @@
"node": ">=6"
},
"scripts": {
"test": "xo && ava && tsd-check"
"test": "xo && ava && tsd"
},
"files": [
"index.js",
Expand All @@ -38,9 +38,9 @@
"p-is-promise": "^2.0.0"
},
"devDependencies": {
"ava": "^1.3.1",
"ava": "^1.4.1",
"delay": "^4.1.0",
"tsd-check": "^0.3.0",
"tsd": "^0.7.1",
"xo": "^0.24.0"
}
}

0 comments on commit 9a432b9

Please sign in to comment.