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

Commit32c01b1

Browse files
authored
Require Node.js 12.20 and move to ESM (#76)
1 parenta473986 commit32c01b1

File tree

7 files changed

+126
-136
lines changed

7 files changed

+126
-136
lines changed

‎.github/workflows/main.yml‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ jobs:
1010
fail-fast:false
1111
matrix:
1212
node-version:
13+
-16
1314
-14
1415
-12
15-
-10
1616
steps:
1717
-uses:actions/checkout@v2
18-
-uses:actions/setup-node@v1
18+
-uses:actions/setup-node@v2
1919
with:
2020
node-version:${{ matrix.node-version }}
2121
-run:npm install

‎index.ts‎

Lines changed: 52 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1-
'use strict';
2-
importmimicFn= require('mimic-fn');
3-
importmapAgeCleaner= require('map-age-cleaner');
1+
importmimicFnfrom'mimic-fn';
2+
importmapAgeCleanerfrom'map-age-cleaner';
43

54
typeAnyFunction=(...arguments_:any)=>any;
65

76
constdecoratorInstanceMap=newWeakMap();
87

9-
constcacheStore=newWeakMap<AnyFunction>();
8+
constcacheStore=newWeakMap<AnyFunction,CacheStorage<any,any>>();
109

1110
interfaceCacheStorageContent<ValueType>{
1211
data:ValueType;
@@ -23,7 +22,7 @@ interface CacheStorage<KeyType, ValueType> {
2322

2423
interfaceOptions<
2524
FunctionToMemoizeextendsAnyFunction,
26-
CacheKeyType
25+
CacheKeyType,
2726
>{
2827
/**
2928
Milliseconds until the cache expires.
@@ -40,16 +39,16 @@ interface Options<
4039
You can have it cache **all** the arguments by value with `JSON.stringify`, if they are compatible:
4140
4241
```
43-
import mem= require('mem');
42+
import memfrom'mem';
4443
4544
mem(function_, {cacheKey: JSON.stringify});
4645
```
4746
4847
Or you can use a more full-featured serializer like [serialize-javascript](https://github.com/yahoo/serialize-javascript) to add support for `RegExp`, `Date` and so on.
4948
5049
```
51-
import mem= require('mem');
52-
import serializeJavascript= require('serialize-javascript');
50+
import memfrom'mem';
51+
import serializeJavascriptfrom'serialize-javascript';
5352
5453
mem(function_, {cacheKey: serializeJavascript});
5554
```
@@ -75,7 +74,7 @@ interface Options<
7574
7675
@example
7776
```
78-
import mem= require('mem');
77+
import memfrom'mem';
7978
8079
let i = 0;
8180
const counter = () => ++i;
@@ -96,63 +95,59 @@ memoized('bar');
9695
//=> 2
9796
```
9897
*/
99-
constmem=<
98+
exportdefaultfunctionmem<
10099
FunctionToMemoizeextendsAnyFunction,
101-
CacheKeyType
100+
CacheKeyType,
102101
>(
103102
fn:FunctionToMemoize,
104103
{
105104
cacheKey,
106105
cache=newMap(),
107-
maxAge
108-
}:Options<FunctionToMemoize,CacheKeyType>={}
109-
):FunctionToMemoize=>{
106+
maxAge,
107+
}:Options<FunctionToMemoize,CacheKeyType>={},
108+
):FunctionToMemoize{
110109
if(typeofmaxAge==='number'){
111-
// TODO: Drop after https://github.com/SamVerschueren/map-age-cleaner/issues/5
112-
//@ts-expect-error
113-
mapAgeCleaner(cache);
110+
mapAgeCleaner(cacheasunknownasMap<CacheKeyType,ReturnType<FunctionToMemoize>>);
114111
}
115112

116-
constmemoized=function(this:any, ...arguments_){
117-
constkey=cacheKey ?cacheKey(arguments_) :arguments_[0];
113+
constmemoized=function(this:any, ...arguments_:Parameters<FunctionToMemoize>):ReturnType<FunctionToMemoize>{
114+
constkey=cacheKey ?cacheKey(arguments_) :arguments_[0]asCacheKeyType;
118115

119116
constcacheItem=cache.get(key);
120117
if(cacheItem){
121-
returncacheItem.data;
118+
returncacheItem.data;// eslint-disable-line @typescript-eslint/no-unsafe-return
122119
}
123120

124-
constresult=fn.apply(this,arguments_);
121+
constresult=fn.apply(this,arguments_)asReturnType<FunctionToMemoize>;
125122

126123
cache.set(key,{
127124
data:result,
128-
maxAge:maxAge ?Date.now()+maxAge :Number.POSITIVE_INFINITY
125+
maxAge:maxAge ?Date.now()+maxAge :Number.POSITIVE_INFINITY,
129126
});
130127

131-
returnresult;
128+
returnresult;// eslint-disable-line @typescript-eslint/no-unsafe-return
132129
}asFunctionToMemoize;
133130

134131
mimicFn(memoized,fn,{
135-
ignoreNonConfigurable:true
132+
ignoreNonConfigurable:true,
136133
});
137134

138135
cacheStore.set(memoized,cache);
139136

140137
returnmemoized;
141-
};
142-
143-
export=mem;
138+
}
144139

145140
/**
146141
@returns A [decorator](https://github.com/tc39/proposal-decorators) to memoize class methods or static class methods.
147142
148143
@example
149144
```
150-
importmem = require('mem');
145+
import{memDecorator} from'mem';
151146
152147
class Example {
153148
index = 0
154149
155-
@mem.decorator()
150+
@memDecorator()
156151
counter() {
157152
return ++this.index;
158153
}
@@ -161,49 +156,51 @@ class Example {
161156
class ExampleWithOptions {
162157
index = 0
163158
164-
@mem.decorator({maxAge: 1000})
159+
@memDecorator({maxAge: 1000})
165160
counter() {
166161
return ++this.index;
167162
}
168163
}
169164
```
170165
*/
171-
mem.decorator=<
166+
exportfunctionmemDecorator<
172167
FunctionToMemoizeextendsAnyFunction,
173-
CacheKeyType
168+
CacheKeyType,
174169
>(
175-
options:Options<FunctionToMemoize,CacheKeyType>={}
176-
)=>(
177-
target:any,
178-
propertyKey:string,
179-
descriptor:PropertyDescriptor
180-
):void=>{
181-
constinput=target[propertyKey];
182-
183-
if(typeofinput!=='function'){
184-
thrownewTypeError('The decorated value must be a function');
185-
}
170+
options:Options<FunctionToMemoize,CacheKeyType>={},
171+
){
172+
return(
173+
target:any,
174+
propertyKey:string,
175+
descriptor:PropertyDescriptor,
176+
):void=>{
177+
constinput=target[propertyKey];// eslint-disable-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-member-access
178+
179+
if(typeofinput!=='function'){
180+
thrownewTypeError('The decorated value must be a function');
181+
}
186182

187-
deletedescriptor.value;
188-
deletedescriptor.writable;
183+
deletedescriptor.value;
184+
deletedescriptor.writable;
189185

190-
descriptor.get=function(){
191-
if(!decoratorInstanceMap.has(this)){
192-
constvalue=mem(input,options);
193-
decoratorInstanceMap.set(this,value);
194-
returnvalue;
195-
}
186+
descriptor.get=function(){
187+
if(!decoratorInstanceMap.has(this)){
188+
constvalue=mem(input,options)asFunctionToMemoize;
189+
decoratorInstanceMap.set(this,value);
190+
returnvalue;
191+
}
196192

197-
returndecoratorInstanceMap.get(this);
193+
returndecoratorInstanceMap.get(this)asFunctionToMemoize;
194+
};
198195
};
199-
};
196+
}
200197

201198
/**
202199
Clear all cached data of a memoized function.
203200
204201
@param fn - Memoized function.
205202
*/
206-
mem.clear=(fn:AnyFunction):void=>{
203+
exportfunctionmemClear(fn:AnyFunction):void{
207204
constcache=cacheStore.get(fn);
208205
if(!cache){
209206
thrownewTypeError('Can\'t clear a function that was not memoized!');
@@ -214,4 +211,4 @@ mem.clear = (fn: AnyFunction): void => {
214211
}
215212

216213
cache.clear();
217-
};
214+
}

‎package.json‎

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,19 @@
1010
"email":"sindresorhus@gmail.com",
1111
"url":"https://sindresorhus.com"
1212
},
13+
"type":"module",
14+
"exports":"./dist/index.js",
1315
"engines": {
14-
"node":">=10"
16+
"node":">=12.20"
1517
},
1618
"scripts": {
17-
"test":"xo && npm run build && tsd && ava",
19+
"test":"xo &&ava &&npm run build && tsd",
1820
"build":"del-cli dist && tsc",
1921
"prepack":"npm run build"
2022
},
21-
"main":"dist",
2223
"types":"dist/index.d.ts",
2324
"files": [
24-
"dist/index.js",
25-
"dist/index.d.ts"
25+
"dist"
2626
],
2727
"keywords": [
2828
"memoize",
@@ -39,30 +39,32 @@
3939
],
4040
"dependencies": {
4141
"map-age-cleaner":"^0.1.3",
42-
"mimic-fn":"^3.1.0"
42+
"mimic-fn":"^4.0.0"
4343
},
4444
"devDependencies": {
4545
"@ava/typescript":"^1.1.1",
46-
"@sindresorhus/tsconfig":"^0.7.0",
46+
"@sindresorhus/tsconfig":"^1.0.2",
4747
"@types/serialize-javascript":"^4.0.0",
4848
"ava":"^3.15.0",
4949
"del-cli":"^3.0.1",
5050
"delay":"^4.4.0",
5151
"serialize-javascript":"^5.0.1",
52+
"ts-node":"^10.1.0",
5253
"tsd":"^0.13.1",
53-
"typescript":"^4.0.3",
54-
"xo":"^0.38.2"
54+
"typescript":"^4.3.5",
55+
"xo":"^0.41.0"
5556
},
5657
"ava": {
57-
"files": [
58-
"test.ts"
59-
],
6058
"timeout":"1m",
61-
"typescript": {
62-
"rewritePaths": {
63-
"./":"dist/"
64-
}
65-
}
59+
"extensions": {
60+
"ts":"module"
61+
},
62+
"nonSemVerExperiments": {
63+
"configurableModuleFormat":true
64+
},
65+
"nodeArguments": [
66+
"--loader=ts-node/esm"
67+
]
6668
},
6769
"xo": {
6870
"rules": {

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp