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

Commit0b5c364

Browse files
authored
Convert to TypeScript (#64)
1 parent1afa53c commit0b5c364

File tree

6 files changed

+172
-173
lines changed

6 files changed

+172
-173
lines changed

‎.gitignore‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
node_modules
22
yarn.lock
3+
dist

‎index.d.ts‎

Lines changed: 0 additions & 108 deletions
This file was deleted.

‎index.js‎

Lines changed: 0 additions & 56 deletions
This file was deleted.

‎index.ts‎

Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
'use strict';
2+
importmimicFn= require('mimic-fn');
3+
importmapAgeCleaner= require('map-age-cleaner');
4+
5+
constcacheStore=newWeakMap<(...arguments_:any[])=>any>();
6+
interfaceCacheStorage<KeyType,ValueType>{
7+
has:(key:KeyType)=>boolean;
8+
get:(key:KeyType)=>ValueType|undefined;
9+
set:(key:KeyType,value:ValueType)=>void;
10+
delete:(key:KeyType)=>void;
11+
clear?:()=>void;
12+
}
13+
14+
interfaceOptions<
15+
ArgumentsTypeextendsunknown[],
16+
CacheKeyType,
17+
ReturnType
18+
>{
19+
/**
20+
Milliseconds until the cache expires.
21+
22+
@default Infinity
23+
*/
24+
readonlymaxAge?:number;
25+
26+
/**
27+
Determines the cache key for storing the result based on the function arguments. By default, __only the first argument is considered__ and it only works with [primitives](https://developer.mozilla.org/en-US/docs/Glossary/Primitive).
28+
29+
A `cacheKey` function can return any type supported by `Map` (or whatever structure you use in the `cache` option).
30+
31+
You can have it cache **all** the arguments by value with `JSON.stringify`, if they are compatible:
32+
33+
```
34+
import mem = require('mem');
35+
36+
mem(function_, {cacheKey: JSON.stringify});
37+
```
38+
39+
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.
40+
41+
```
42+
import mem = require('mem');
43+
import serializeJavascript = require('serialize-javascript');
44+
45+
mem(function_, {cacheKey: serializeJavascript});
46+
```
47+
48+
@default arguments_ => arguments_[0]
49+
@example arguments_ => JSON.stringify(arguments_)
50+
*/
51+
readonlycacheKey?:(arguments_:ArgumentsType)=>CacheKeyType;
52+
53+
/**
54+
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.
55+
56+
@default new Map()
57+
@example new WeakMap()
58+
*/
59+
readonlycache?:CacheStorage<CacheKeyType,{data:ReturnType;maxAge:number}>;
60+
}
61+
62+
/**
63+
[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.
64+
65+
@param fn - Function to be memoized.
66+
67+
@example
68+
```
69+
import mem = require('mem');
70+
71+
let i = 0;
72+
const counter = () => ++i;
73+
const memoized = mem(counter);
74+
75+
memoized('foo');
76+
//=> 1
77+
78+
// Cached as it's the same arguments
79+
memoized('foo');
80+
//=> 1
81+
82+
// Not cached anymore as the arguments changed
83+
memoized('bar');
84+
//=> 2
85+
86+
memoized('bar');
87+
//=> 2
88+
```
89+
*/
90+
constmem=<
91+
ArgumentsTypeextendsany[],
92+
ReturnTypeextendsany,
93+
CacheKeyType,
94+
FunctionToMemoizeextends(...arguments_:ArgumentsType)=>ReturnType
95+
>(
96+
fn:FunctionToMemoize,{
97+
cacheKey,
98+
cache=newMap(),
99+
maxAge
100+
}:Options<ArgumentsType,CacheKeyType,ReturnType>={}):FunctionToMemoize=>{
101+
if(typeofmaxAge==='number'){
102+
// TODO: drop after https://github.com/SamVerschueren/map-age-cleaner/issues/5
103+
//@ts-expect-error
104+
mapAgeCleaner(cache);
105+
}
106+
107+
constmemoized=function(this:any, ...arguments_){
108+
constkey=cacheKey ?cacheKey(arguments_) :arguments_[0];
109+
110+
constcacheItem=cache.get(key);
111+
if(cacheItem){
112+
returncacheItem.data;
113+
}
114+
115+
constresult=fn.apply(this,arguments_);
116+
117+
cache.set(key,{
118+
data:result,
119+
maxAge:maxAge ?Date.now()+maxAge :Infinity
120+
});
121+
122+
returnresult;
123+
}asFunctionToMemoize;
124+
125+
try{
126+
// The below call will throw in some host environments
127+
// See https://github.com/sindresorhus/mimic-fn/issues/10
128+
mimicFn(memoized,fn);
129+
}catch{}
130+
131+
cacheStore.set(memoized,cache);
132+
133+
returnmemoized;
134+
};
135+
136+
export=mem;
137+
138+
/**
139+
Clear all cached data of a memoized function.
140+
141+
@param fn - Memoized function.
142+
*/
143+
mem.clear=(fn:(...arguments_:any[])=>any):void=>{
144+
if(!cacheStore.has(fn)){
145+
thrownewError('Can\'t clear a function that was not memoized!');
146+
}
147+
148+
constcache=cacheStore.get(fn);
149+
if(typeofcache.clear==='function'){
150+
cache.clear();
151+
}
152+
};

‎package.json‎

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,15 @@
1414
"node":">=10"
1515
},
1616
"scripts": {
17-
"test":"xo && ava && tsd"
17+
"test":"xo && npm run build && tsd && ava",
18+
"build":"del-cli dist && tsc",
19+
"prepack":"npm run build"
1820
},
21+
"main":"dist",
22+
"types":"dist/index.d.ts",
1923
"files": [
20-
"index.js",
21-
"index.d.ts"
24+
"dist/index.js",
25+
"dist/index.d.ts"
2226
],
2327
"keywords": [
2428
"memoize",
@@ -38,16 +42,13 @@
3842
"mimic-fn":"^3.0.0"
3943
},
4044
"devDependencies": {
41-
"@types/serialize-javascript":"^4.0.0",
45+
"@sindresorhus/tsconfig":"^0.7.0",
4246
"ava":"^3.13.0",
47+
"del-cli":"^3.0.1",
4348
"delay":"^4.1.0",
4449
"serialize-javascript":"^5.0.1",
4550
"tsd":"^0.13.1",
51+
"typescript":"^4.0.3",
4652
"xo":"^0.33.1"
47-
},
48-
"xo": {
49-
"rules": {
50-
"@typescript-eslint/member-ordering":"off"
51-
}
5253
}
5354
}

‎tsconfig.json‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"extends":"@sindresorhus/tsconfig",
3+
"compilerOptions": {
4+
"outDir":"dist"
5+
},
6+
"files": [
7+
"index.ts"
8+
]
9+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp