-
-
Notifications
You must be signed in to change notification settings - Fork 56
Refactor TypeScript definition to use CJS compatible export #32
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
sindresorhus
merged 8 commits into
sindresorhus:master
from
BendingBender:cjs-comaptible-exports
Mar 31, 2019
Merged
Changes from 4 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
d01a262
Refactor TypeScript definition to use CJS compatible export
BendingBender 5b744a4
Update dev deps
BendingBender 7792387
Document reasons for legacy import syntax
BendingBender d7077c6
Update index.d.ts
sindresorhus 46096f2
Merge remote-tracking branch 'upstream/master' into cjs-comaptible-ex…
BendingBender bf00164
Fixes after review
BendingBender 5ebdeb2
Merge branch 'cjs-comaptible-exports' of github.com:BendingBender/mem…
BendingBender 9a4331c
Fix wording
BendingBender File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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, ReturnType>; | ||
/** | ||
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, ReturnType>; | ||
|
||
/** | ||
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; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.