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

Commit4756892

Browse files
committed
Rename package frommem tomemoize
1 parent8f207cf commit4756892

File tree

5 files changed

+98
-87
lines changed

5 files changed

+98
-87
lines changed

‎index.ts‎

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -37,18 +37,18 @@ export type Options<
3737
You can have it cache **all** the arguments by value with `JSON.stringify`, if they are compatible:
3838
3939
```
40-
importmem from 'mem';
40+
importmemoize from 'memoize';
4141
42-
mem(function_, {cacheKey: JSON.stringify});
42+
memoize(function_, {cacheKey: JSON.stringify});
4343
```
4444
4545
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.
4646
4747
```
48-
importmem from 'mem';
48+
importmemoize from 'memoize';
4949
import serializeJavascript from 'serialize-javascript';
5050
51-
mem(function_, {cacheKey: serializeJavascript});
51+
memoize(function_, {cacheKey: serializeJavascript});
5252
```
5353
5454
@default arguments_ => arguments_[0]
@@ -72,11 +72,11 @@ export type Options<
7272
7373
@example
7474
```
75-
importmem from 'mem';
75+
importmemoize from 'memoize';
7676
7777
let index = 0;
7878
const counter = () => ++index;
79-
const memoized =mem(counter);
79+
const memoized =memoize(counter);
8080
8181
memoized('foo');
8282
//=> 1
@@ -93,7 +93,7 @@ memoized('bar');
9393
//=> 2
9494
```
9595
*/
96-
exportdefaultfunctionmem<
96+
exportdefaultfunctionmemoize<
9797
FunctionToMemoizeextendsAnyFunction,
9898
CacheKeyType,
9999
>(
@@ -163,12 +163,12 @@ export default function mem<
163163
164164
@example
165165
```
166-
import {memDecorator} from 'mem';
166+
import {memoizeDecorator} from 'memoize';
167167
168168
class Example {
169169
index = 0
170170
171-
@memDecorator()
171+
@memoizeDecorator()
172172
counter() {
173173
return ++this.index;
174174
}
@@ -177,14 +177,14 @@ class Example {
177177
class ExampleWithOptions {
178178
index = 0
179179
180-
@memDecorator({maxAge: 1000})
180+
@memoizeDecorator({maxAge: 1000})
181181
counter() {
182182
return ++this.index;
183183
}
184184
}
185185
```
186186
*/
187-
exportfunctionmemDecorator<
187+
exportfunctionmemoizeDecorator<
188188
FunctionToMemoizeextendsAnyFunction,
189189
CacheKeyType,
190190
>(
@@ -208,7 +208,7 @@ export function memDecorator<
208208

209209
descriptor.get=function(){
210210
if(!instanceMap.has(this)){
211-
constvalue=mem(input,options)asFunctionToMemoize;
211+
constvalue=memoize(input,options)asFunctionToMemoize;
212212
instanceMap.set(this,value);
213213
returnvalue;
214214
}
@@ -223,7 +223,7 @@ Clear all cached data of a memoized function.
223223
224224
@param fn - The memoized function.
225225
*/
226-
exportfunctionmemClear(fn:AnyFunction):void{
226+
exportfunctionmemoizeClear(fn:AnyFunction):void{
227227
constcache=cacheStore.get(fn);
228228
if(!cache){
229229
thrownewTypeError('Can\'t clear a function that was not memoized!');
@@ -233,7 +233,7 @@ export function memClear(fn: AnyFunction): void {
233233
thrownewTypeError('The cache Map can\'t be cleared!');
234234
}
235235

236-
cache.clear?.();
236+
cache.clear();
237237

238238
for(consttimerofcacheTimerStore.get(fn)??[]){
239239
clearTimeout(timer);

‎package.json‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
{
2-
"name":"mem",
2+
"name":"memoize",
33
"version":"9.0.2",
44
"description":"Memoize functions - An optimization used to speed up consecutive function calls by caching the result of calls with identical input",
55
"license":"MIT",
6-
"repository":"sindresorhus/mem",
7-
"funding":"https://github.com/sindresorhus/mem?sponsor=1",
6+
"repository":"sindresorhus/memoize",
7+
"funding":"https://github.com/sindresorhus/memoize?sponsor=1",
88
"author": {
99
"name":"Sindre Sorhus",
1010
"email":"sindresorhus@gmail.com",

‎readme.md‎

Lines changed: 39 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#mem
1+
#memoize
22

33
>[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
44
@@ -13,17 +13,17 @@ If you want to memoize Promise-returning functions (like `async` functions), you
1313
##Install
1414

1515
```sh
16-
npm installmem
16+
npm installmemoize
1717
```
1818

1919
##Usage
2020

2121
```js
22-
importmemfrom'mem';
22+
importmemoizefrom'memoize';
2323

2424
let index=0;
2525
constcounter= ()=>++index;
26-
constmemoized=mem(counter);
26+
constmemoized=memoize(counter);
2727

2828
memoized('foo');
2929
//=> 1
@@ -49,11 +49,11 @@ memoized('bar', 'foo');
4949
But you might want to use[p-memoize](https://github.com/sindresorhus/p-memoize) for more Promise-specific behaviors.
5050

5151
```js
52-
importmemfrom'mem';
52+
importmemoizefrom'memoize';
5353

5454
let index=0;
5555
constcounter=async ()=>++index;
56-
constmemoized=mem(counter);
56+
constmemoized=memoize(counter);
5757

5858
console.log(awaitmemoized());
5959
//=> 1
@@ -64,29 +64,31 @@ console.log(await memoized());
6464
```
6565

6666
```js
67-
importmemfrom'mem';
67+
importmemoizefrom'memoize';
6868
importgotfrom'got';
6969
importdelayfrom'delay';
7070

71-
constmemGot=mem(got, {maxAge:1000});
71+
constmemoizedGot=memoize(got, {maxAge:1000});
7272

73-
awaitmemGot('https://sindresorhus.com');
73+
awaitmemoizedGot('https://sindresorhus.com');
7474

7575
// This call is cached
76-
awaitmemGot('https://sindresorhus.com');
76+
awaitmemoizedGot('https://sindresorhus.com');
7777

7878
awaitdelay(2000);
7979

8080
// This call is not cached as the cache has expired
81-
awaitmemGot('https://sindresorhus.com');
81+
awaitmemoizedGot('https://sindresorhus.com');
8282
```
8383

8484
###Caching strategy
8585

8686
By default, only the first argument is compared via exact equality (`===`) to determine whether a call is identical.
8787

8888
```js
89-
constpower=mem((a,b)=>Math.power(a, b));
89+
importmemoizefrom'memoize';
90+
91+
constpower=memoize((a,b)=>Math.power(a, b));
9092

9193
power(2,2);// => 4, stored in cache with the key 2 (number)
9294
power(2,3);// => 4, retrieved from cache at key 2 (number), it's wrong
@@ -95,7 +97,9 @@ power(2, 3); // => 4, retrieved from cache at key 2 (number), it's wrong
9597
You will have to use the`cache` and`cacheKey` options appropriate to your function. In this specific case, the following could work:
9698

9799
```js
98-
constpower=mem((a,b)=>Math.power(a, b), {
100+
importmemoizefrom'memoize';
101+
102+
constpower=memoize((a,b)=>Math.power(a, b), {
99103
cacheKey:arguments_=>arguments_.join(',')
100104
});
101105

@@ -110,7 +114,9 @@ More advanced examples follow.
110114
If your function accepts an object, it won't be memoized out of the box:
111115

112116
```js
113-
constheavyMemoizedOperation=mem(heavyOperation);
117+
importmemoizefrom'memoize';
118+
119+
constheavyMemoizedOperation=memoize(heavyOperation);
114120

115121
heavyMemoizedOperation({full:true});// Stored in cache with the object as key
116122
heavyMemoizedOperation({full:true});// Stored in cache with the object as key, again
@@ -120,7 +126,9 @@ heavyMemoizedOperation({full: true}); // Stored in cache with the object as key,
120126
You might want to serialize or hash them, for example using`JSON.stringify` or something like[serialize-javascript](https://github.com/yahoo/serialize-javascript), which can also serialize`RegExp`,`Date` and so on.
121127

122128
```js
123-
constheavyMemoizedOperation=mem(heavyOperation, {cacheKey:JSON.stringify});
129+
importmemoizefrom'memoize';
130+
131+
constheavyMemoizedOperation=memoize(heavyOperation, {cacheKey:JSON.stringify});
124132

125133
heavyMemoizedOperation({full:true});// Stored in cache with the key '[{"full":true}]' (string)
126134
heavyMemoizedOperation({full:true});// Retrieved from cache
@@ -129,7 +137,9 @@ heavyMemoizedOperation({full: true}); // Retrieved from cache
129137
The same solution also works if it accepts multiple serializable objects:
130138

131139
```js
132-
constheavyMemoizedOperation=mem(heavyOperation, {cacheKey:JSON.stringify});
140+
importmemoizefrom'memoize';
141+
142+
constheavyMemoizedOperation=memoize(heavyOperation, {cacheKey:JSON.stringify});
133143

134144
heavyMemoizedOperation('hello', {full:true});// Stored in cache with the key '["hello",{"full":true}]' (string)
135145
heavyMemoizedOperation('hello', {full:true});// Retrieved from cache
@@ -140,11 +150,12 @@ heavyMemoizedOperation('hello', {full: true}); // Retrieved from cache
140150
If your function accepts multiple arguments that aren't supported by`JSON.stringify` (e.g. DOM elements and functions), you can instead extend the initial exact equality (`===`) to work on multiple arguments using[`many-keys-map`](https://github.com/fregante/many-keys-map):
141151

142152
```js
153+
importmemoizefrom'memoize';
143154
importManyKeysMapfrom'many-keys-map';
144155

145156
constaddListener= (emitter,eventName,listener)=>emitter.on(eventName, listener);
146157

147-
constaddOneListener=mem(addListener, {
158+
constaddOneListener=memoize(addListener, {
148159
cacheKey:arguments_=> arguments_,// Use *all* the arguments as key
149160
cache:newManyKeysMap()// Correctly handles all the arguments for exact equality
150161
});
@@ -158,7 +169,7 @@ Better yet, if your function’s arguments are compatible with `WeakMap`, you sh
158169

159170
##API
160171

161-
###mem(fn, options?)
172+
###memoize(fn, options?)
162173

163174
####fn
164175

@@ -212,15 +223,15 @@ Notes:
212223

213224
Type:`object`
214225

215-
Same as options for`mem()`.
226+
Same as options for`memoize()`.
216227

217228
```ts
218-
import {memDecorator}from'mem';
229+
import {memoizeDecorator}from'memoize';
219230

220231
classExample {
221232
index=0
222233

223-
@memDecorator()
234+
@memoizeDecorator()
224235
counter() {
225236
return++this.index;
226237
}
@@ -229,14 +240,14 @@ class Example {
229240
classExampleWithOptions {
230241
index=0
231242

232-
@memDecorator({maxAge:1000})
243+
@memoizeDecorator({maxAge:1000})
233244
counter() {
234245
return++this.index;
235246
}
236247
}
237248
```
238249

239-
###memClear(fn)
250+
###memoizeClear(fn)
240251

241252
Clear all cached data of a memoized function.
242253

@@ -255,16 +266,16 @@ If you want to know how many times your cache had a hit or a miss, you can make
255266
####Example
256267

257268
```js
258-
importmemfrom'mem';
269+
importmemoizefrom'memoize';
259270
importStatsMapfrom'stats-map';
260271
importgotfrom'got';
261272

262273
constcache=newStatsMap();
263-
constmemGot=mem(got, {cache});
274+
constmemoizedGot=memoize(got, {cache});
264275

265-
awaitmemGot('https://sindresorhus.com');
266-
awaitmemGot('https://sindresorhus.com');
267-
awaitmemGot('https://sindresorhus.com');
276+
awaitmemoizedGot('https://sindresorhus.com');
277+
awaitmemoizedGot('https://sindresorhus.com');
278+
awaitmemoizedGot('https://sindresorhus.com');
268279

269280
console.log(cache.stats);
270281
//=> {hits: 2, misses: 1}

‎test-d/index.test-d.ts‎

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
11
import{expectType}from'tsd';
2-
importmem,{memClear}from'../index.js';
2+
importmemoize,{memoizeClear}from'../index.js';
33

44
// eslint-disable-next-line unicorn/prefer-native-coercion-functions -- Required `string` type
55
constfn=(text:string)=>Boolean(text);
66

7-
expectType<typeoffn>(mem(fn));
8-
expectType<typeoffn>(mem(fn,{maxAge:1}));
9-
expectType<typeoffn>(mem(fn,{cacheKey:([firstArgument]:[string])=>firstArgument}));
7+
expectType<typeoffn>(memoize(fn));
8+
expectType<typeoffn>(memoize(fn,{maxAge:1}));
9+
expectType<typeoffn>(memoize(fn,{cacheKey:([firstArgument]:[string])=>firstArgument}));
1010
expectType<typeoffn>(
11-
mem(fn,{
11+
memoize(fn,{
1212
// The cacheKey returns an array. This isn't deduplicated by a regular Map, but it's valid. The correct solution would be to use ManyKeysMap to deduplicate it correctly
1313
cacheKey:(arguments_:[string])=>arguments_,
1414
cache:newMap<[string],{data:boolean;maxAge:number}>(),
1515
}),
1616
);
1717
expectType<typeoffn>(
1818
// The `firstArgument` of `fn` is of type `string`, so it's used
19-
mem(fn,{cache:newMap<string,{data:boolean;maxAge:number}>()}),
19+
memoize(fn,{cache:newMap<string,{data:boolean;maxAge:number}>()}),
2020
);
2121

2222
/* Overloaded function tests */
@@ -26,29 +26,29 @@ function overloadedFn(parameter: boolean): boolean {
2626
returnparameter;
2727
}
2828

29-
expectType<typeofoverloadedFn>(mem(overloadedFn));
30-
expectType<true>(mem(overloadedFn)(true));
31-
expectType<false>(mem(overloadedFn)(false));
29+
expectType<typeofoverloadedFn>(memoize(overloadedFn));
30+
expectType<true>(memoize(overloadedFn)(true));
31+
expectType<false>(memoize(overloadedFn)(false));
3232

33-
memClear(fn);
33+
memoizeClear(fn);
3434

3535
// `cacheKey` tests.
3636
// The argument should match the memoized function’s parameters
3737
// eslint-disable-next-line unicorn/prefer-native-coercion-functions -- Required `string` type
38-
mem((text:string)=>Boolean(text),{
38+
memoize((text:string)=>Boolean(text),{
3939
cacheKey(arguments_){
4040
expectType<[string]>(arguments_);
4141
},
4242
});
4343

44-
mem(()=>1,{
44+
memoize(()=>1,{
4545
cacheKey(arguments_){
4646
expectType<[]>(arguments_);// eslint-disable-line @typescript-eslint/ban-types
4747
},
4848
});
4949

5050
// Ensures that the various cache functions infer their arguments type from the return type of `cacheKey`
51-
mem((_arguments:{key:string})=>1,{
51+
memoize((_arguments:{key:string})=>1,{
5252
cacheKey(arguments_:[{key:string}]){
5353
expectType<[{key:string}]>(arguments_);
5454
returnnewDate();

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp