1- 'use strict' ;
2- import mimicFn = require( 'mimic-fn' ) ;
3- import mapAgeCleaner = require( 'map-age-cleaner' ) ;
1+ import mimicFn from 'mimic-fn' ;
2+ import mapAgeCleaner from 'map-age-cleaner' ;
43
54type AnyFunction = ( ...arguments_ :any ) => any ;
65
76const decoratorInstanceMap = new WeakMap ( ) ;
87
9- const cacheStore = new WeakMap < AnyFunction > ( ) ;
8+ const cacheStore = new WeakMap < AnyFunction , CacheStorage < any , any > > ( ) ;
109
1110interface CacheStorageContent < ValueType > {
1211data :ValueType ;
@@ -23,7 +22,7 @@ interface CacheStorage<KeyType, ValueType> {
2322
2423interface Options <
2524FunctionToMemoize extends AnyFunction ,
26- CacheKeyType
25+ CacheKeyType ,
2726> {
2827/**
2928Milliseconds until the cache expires.
@@ -40,16 +39,16 @@ interface Options<
4039You 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
4544mem(function_, {cacheKey: JSON.stringify});
4645```
4746
4847Or 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
5453mem(function_, {cacheKey: serializeJavascript});
5554```
@@ -75,7 +74,7 @@ interface Options<
7574
7675@example
7776```
78- import mem= require( 'mem') ;
77+ import memfrom 'mem';
7978
8079let i = 0;
8180const counter = () => ++i;
@@ -96,63 +95,59 @@ memoized('bar');
9695//=> 2
9796```
9897*/
99- const mem = <
98+ export default function mem <
10099FunctionToMemoize extends AnyFunction ,
101- CacheKeyType
100+ CacheKeyType ,
102101> (
103102fn :FunctionToMemoize ,
104103{
105104cacheKey,
106105cache= new Map ( ) ,
107- maxAge
108- } :Options < FunctionToMemoize , CacheKeyType > = { }
109- ) :FunctionToMemoize => {
106+ maxAge,
107+ } :Options < FunctionToMemoize , CacheKeyType > = { } ,
108+ ) :FunctionToMemoize {
110109if ( typeof maxAge === 'number' ) {
111- // TODO: Drop after https://github.com/SamVerschueren/map-age-cleaner/issues/5
112- //@ts -expect-error
113- mapAgeCleaner ( cache ) ;
110+ mapAgeCleaner ( cache as unknown as Map < CacheKeyType , ReturnType < FunctionToMemoize > > ) ;
114111}
115112
116- const memoized = function ( this :any , ...arguments_ ) {
117- const key = cacheKey ?cacheKey ( arguments_ ) :arguments_ [ 0 ] ;
113+ const memoized = function ( this :any , ...arguments_ : Parameters < FunctionToMemoize > ) : ReturnType < FunctionToMemoize > {
114+ const key = cacheKey ?cacheKey ( arguments_ ) :arguments_ [ 0 ] as CacheKeyType ;
118115
119116const cacheItem = cache . get ( key ) ;
120117if ( cacheItem ) {
121- return cacheItem . data ;
118+ return cacheItem . data ; // eslint-disable-line @typescript-eslint/no-unsafe-return
122119}
123120
124- const result = fn . apply ( this , arguments_ ) ;
121+ const result = fn . apply ( this , arguments_ ) as ReturnType < FunctionToMemoize > ;
125122
126123cache . set ( key , {
127124data :result ,
128- maxAge :maxAge ?Date . now ( ) + maxAge :Number . POSITIVE_INFINITY
125+ maxAge :maxAge ?Date . now ( ) + maxAge :Number . POSITIVE_INFINITY ,
129126} ) ;
130127
131- return result ;
128+ return result ; // eslint-disable-line @typescript-eslint/no-unsafe-return
132129} as FunctionToMemoize ;
133130
134131mimicFn ( memoized , fn , {
135- ignoreNonConfigurable :true
132+ ignoreNonConfigurable :true ,
136133} ) ;
137134
138135cacheStore . set ( memoized , cache ) ;
139136
140137return memoized ;
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
152147class Example {
153148index = 0
154149
155- @mem .decorator ()
150+ @memDecorator ()
156151counter() {
157152return ++this.index;
158153}
@@ -161,49 +156,51 @@ class Example {
161156class ExampleWithOptions {
162157index = 0
163158
164- @mem .decorator ({maxAge: 1000})
159+ @memDecorator ({maxAge: 1000})
165160counter() {
166161return ++this.index;
167162}
168163}
169164```
170165*/
171- mem . decorator = <
166+ export function memDecorator <
172167FunctionToMemoize extends AnyFunction ,
173- CacheKeyType
168+ CacheKeyType ,
174169> (
175- options :Options < FunctionToMemoize , CacheKeyType > = { }
176- ) => (
177- target :any ,
178- propertyKey :string ,
179- descriptor :PropertyDescriptor
180- ) :void => {
181- const input = target [ propertyKey ] ;
182-
183- if ( typeof input !== 'function' ) {
184- throw new TypeError ( '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+ const input = target [ propertyKey ] ; // eslint-disable-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-member-access
178+
179+ if ( typeof input !== 'function' ) {
180+ throw new TypeError ( 'The decorated value must be a function' ) ;
181+ }
186182
187- delete descriptor . value ;
188- delete descriptor . writable ;
183+ delete descriptor . value ;
184+ delete descriptor . writable ;
189185
190- descriptor . get = function ( ) {
191- if ( ! decoratorInstanceMap . has ( this ) ) {
192- const value = mem ( input , options ) ;
193- decoratorInstanceMap . set ( this , value ) ;
194- return value ;
195- }
186+ descriptor . get = function ( ) {
187+ if ( ! decoratorInstanceMap . has ( this ) ) {
188+ const value = mem ( input , options ) as FunctionToMemoize ;
189+ decoratorInstanceMap . set ( this , value ) ;
190+ return value ;
191+ }
196192
197- return decoratorInstanceMap . get ( this ) ;
193+ return decoratorInstanceMap . get ( this ) as FunctionToMemoize ;
194+ } ;
198195} ;
199- } ;
196+ }
200197
201198/**
202199Clear all cached data of a memoized function.
203200
204201@param fn - Memoized function.
205202*/
206- mem . clear = ( fn :AnyFunction ) :void => {
203+ export function memClear ( fn :AnyFunction ) :void {
207204const cache = cacheStore . get ( fn ) ;
208205if ( ! cache ) {
209206throw new TypeError ( 'Can\'t clear a function that was not memoized!' ) ;
@@ -214,4 +211,4 @@ mem.clear = (fn: AnyFunction): void => {
214211}
215212
216213cache . clear ( ) ;
217- } ;
214+ }