Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit9a432b9

Browse files
BendingBendersindresorhus
authored andcommitted
Refactor TypeScript definition to use CJS compatible export (#32)
1 parent298a71e commit9a432b9

File tree

4 files changed

+82
-55
lines changed

4 files changed

+82
-55
lines changed

‎index.d.ts‎

Lines changed: 75 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,71 +1,96 @@
1-
exportinterfaceCacheStorage<
2-
KeyTypeextendsunknown,
3-
ValueTypeextendsunknown
4-
>{
5-
has(key:KeyType):boolean;
6-
get(key:KeyType):ValueType|undefined;
7-
set(key:KeyType,value:ValueType):void;
8-
delete(key:KeyType):void;
9-
clear?:()=>void;
10-
}
1+
declarenamespacemem{
2+
interfaceCacheStorage<KeyTypeextendsunknown,ValueTypeextendsunknown>{
3+
has(key:KeyType):boolean;
4+
get(key:KeyType):ValueType|undefined;
5+
set(key:KeyType,value:ValueType):void;
6+
delete(key:KeyType):void;
7+
clear?:()=>void;
8+
}
119

12-
exportinterfaceOptions<
13-
ArgumentsTypeextendsunknown[],
14-
CacheKeyTypeextendsunknown,
15-
ReturnTypeextendsunknown
16-
>{
17-
/**
18-
* Milliseconds until the cache expires.
19-
*
20-
*@default Infinity
21-
*/
22-
readonlymaxAge?:number;
10+
interfaceOptions<
11+
ArgumentsTypeextendsunknown[],
12+
CacheKeyTypeextendsunknown,
13+
ReturnTypeextendsunknown
14+
>{
15+
/**
16+
Milliseconds until the cache expires.
2317
24-
/**
25-
* 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.
26-
*
27-
* You could for example change it to only cache on the first argument `x => JSON.stringify(x)`.
28-
*/
29-
readonlycacheKey?:(...arguments:ArgumentsType)=>CacheKeyType;
18+
@default Infinity
19+
*/
20+
readonlymaxAge?:number;
3021

31-
/**
32-
* 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.
33-
*
34-
*@default new Map()
35-
*/
36-
readonlycache?:CacheStorage<CacheKeyType,{data:ReturnType;maxAge:number}>;
22+
/**
23+
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.
3724
38-
/**
39-
* Cache rejected promises.
40-
*
41-
*@default false
42-
*/
43-
readonlycachePromiseRejection?:boolean;
25+
You could for example change it to only cache on the first argument `x => JSON.stringify(x)`.
26+
*/
27+
readonlycacheKey?:(...arguments:ArgumentsType)=>CacheKeyType;
28+
29+
/**
30+
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.
31+
32+
@default new Map()
33+
*/
34+
readonlycache?:CacheStorage<CacheKeyType,{data:ReturnType;maxAge:number}>;
35+
36+
/**
37+
Cache rejected promises.
38+
39+
@default false
40+
*/
41+
readonlycachePromiseRejection?:boolean;
42+
}
4443
}
4544

46-
/**
47-
* [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.
48-
*
49-
*@param fn - Function to be memoized.
50-
*/
5145
declareconstmem:{
46+
/**
47+
[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.
48+
49+
@param fn - Function to be memoized.
50+
51+
@example
52+
```
53+
import mem = require('mem');
54+
55+
let i = 0;
56+
const counter = () => ++i;
57+
const memoized = mem(counter);
58+
59+
memoized('foo');
60+
//=> 1
61+
62+
// Cached as it's the same arguments
63+
memoized('foo');
64+
//=> 1
65+
66+
// Not cached anymore as the arguments changed
67+
memoized('bar');
68+
//=> 2
69+
70+
memoized('bar');
71+
//=> 2
72+
```
73+
*/
5274
<
5375
ArgumentsTypeextendsunknown[],
5476
ReturnTypeextendsunknown,
5577
CacheKeyTypeextendsunknown
5678
>(
5779
fn:(...arguments:ArgumentsType)=>ReturnType,
58-
options?:Options<ArgumentsType,CacheKeyType,ReturnType>
80+
options?:mem.Options<ArgumentsType,CacheKeyType,ReturnType>
5981
):(...arguments:ArgumentsType)=>ReturnType;
6082

6183
/**
62-
*Clear all cached data of a memoized function.
63-
*
64-
*@param fn - Memoized function.
65-
*/
84+
Clear all cached data of a memoized function.
85+
86+
@param fn - Memoized function.
87+
*/
6688
clear<ArgumentsTypeextendsunknown[],ReturnTypeextendsunknown>(
6789
fn:(...arguments:ArgumentsType)=>ReturnType
6890
):void;
91+
92+
// TODO: Remove this for the next major release
93+
default:typeofmem;
6994
};
7095

71-
exportdefaultmem;
96+
export=mem;

‎index.js‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ const mem = (fn, options) => {
7676
};
7777

7878
module.exports=mem;
79+
// TODO: Remove this for the next major release
7980
module.exports.default=mem;
8081

8182
module.exports.clear=fn=>{

‎index.test-d.ts‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
import{expectType}from'tsd-check';
2-
importmemfrom'.';
1+
import{expectType}from'tsd';
2+
// The following import syntax makes sure that the type IntelliSense interop with plain JS isn't broken
3+
importmem= require('.');
34

45
constfn=(string:string)=>true;
56

‎package.json‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
"node":">=6"
1414
},
1515
"scripts": {
16-
"test":"xo && ava && tsd-check"
16+
"test":"xo && ava && tsd"
1717
},
1818
"files": [
1919
"index.js",
@@ -38,9 +38,9 @@
3838
"p-is-promise":"^2.0.0"
3939
},
4040
"devDependencies": {
41-
"ava":"^1.3.1",
41+
"ava":"^1.4.1",
4242
"delay":"^4.1.0",
43-
"tsd-check":"^0.3.0",
43+
"tsd":"^0.7.1",
4444
"xo":"^0.24.0"
4545
}
4646
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp