-
-
Notifications
You must be signed in to change notification settings - Fork 54
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Require Node.js 12.20 and move to ESM #76
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
270c061
Require Node.js 12.20 and move to ESM
Richienb 45f3d3c
Update build workflow
Richienb ffd58b0
Update .github/workflows/main.yml
Richienb e97f2a5
Fix lint
Richienb 6c510d0
Fix tests
Richienb 65c1a54
Deal with most of the lint errors
Richienb File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,11 @@ | ||
'use strict'; | ||
import mimicFn = require('mimic-fn'); | ||
import mapAgeCleaner = require('map-age-cleaner'); | ||
import mimicFn from 'mimic-fn'; | ||
import mapAgeCleaner from 'map-age-cleaner'; | ||
|
||
type AnyFunction = (...arguments_: any) => any; | ||
|
||
const decoratorInstanceMap = new WeakMap(); | ||
|
||
const cacheStore = new WeakMap<AnyFunction>(); | ||
const cacheStore = new WeakMap<AnyFunction, CacheStorage<any, any>>(); | ||
|
||
interface CacheStorageContent<ValueType> { | ||
data: ValueType; | ||
|
@@ -23,7 +22,7 @@ interface CacheStorage<KeyType, ValueType> { | |
|
||
interface Options< | ||
FunctionToMemoize extends AnyFunction, | ||
CacheKeyType | ||
CacheKeyType, | ||
> { | ||
/** | ||
Milliseconds until the cache expires. | ||
|
@@ -40,16 +39,16 @@ interface Options< | |
You can have it cache **all** the arguments by value with `JSON.stringify`, if they are compatible: | ||
|
||
``` | ||
import mem = require('mem'); | ||
import mem from 'mem'; | ||
|
||
mem(function_, {cacheKey: JSON.stringify}); | ||
``` | ||
|
||
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. | ||
|
||
``` | ||
import mem = require('mem'); | ||
import serializeJavascript = require('serialize-javascript'); | ||
import mem from 'mem'; | ||
import serializeJavascript from 'serialize-javascript'; | ||
|
||
mem(function_, {cacheKey: serializeJavascript}); | ||
``` | ||
|
@@ -75,7 +74,7 @@ interface Options< | |
|
||
@example | ||
``` | ||
import mem = require('mem'); | ||
import mem from 'mem'; | ||
|
||
let i = 0; | ||
const counter = () => ++i; | ||
|
@@ -96,63 +95,59 @@ memoized('bar'); | |
//=> 2 | ||
``` | ||
*/ | ||
const mem = < | ||
export default function mem< | ||
FunctionToMemoize extends AnyFunction, | ||
CacheKeyType | ||
CacheKeyType, | ||
>( | ||
fn: FunctionToMemoize, | ||
{ | ||
cacheKey, | ||
cache = new Map(), | ||
maxAge | ||
}: Options<FunctionToMemoize, CacheKeyType> = {} | ||
): FunctionToMemoize => { | ||
maxAge, | ||
}: Options<FunctionToMemoize, CacheKeyType> = {}, | ||
): FunctionToMemoize { | ||
if (typeof maxAge === 'number') { | ||
// TODO: Drop after https://github.com/SamVerschueren/map-age-cleaner/issues/5 | ||
// @ts-expect-error | ||
mapAgeCleaner(cache); | ||
mapAgeCleaner(cache as unknown as Map<CacheKeyType, ReturnType<FunctionToMemoize>>); | ||
} | ||
|
||
const memoized = function (this: any, ...arguments_) { | ||
const key = cacheKey ? cacheKey(arguments_) : arguments_[0]; | ||
const memoized = function (this: any, ...arguments_: Parameters<FunctionToMemoize>): ReturnType<FunctionToMemoize> { | ||
const key = cacheKey ? cacheKey(arguments_) : arguments_[0] as CacheKeyType; | ||
|
||
const cacheItem = cache.get(key); | ||
if (cacheItem) { | ||
return cacheItem.data; | ||
return cacheItem.data; // eslint-disable-line @typescript-eslint/no-unsafe-return | ||
} | ||
|
||
const result = fn.apply(this, arguments_); | ||
const result = fn.apply(this, arguments_) as ReturnType<FunctionToMemoize>; | ||
|
||
cache.set(key, { | ||
data: result, | ||
maxAge: maxAge ? Date.now() + maxAge : Number.POSITIVE_INFINITY | ||
maxAge: maxAge ? Date.now() + maxAge : Number.POSITIVE_INFINITY, | ||
}); | ||
|
||
return result; | ||
return result; // eslint-disable-line @typescript-eslint/no-unsafe-return | ||
} as FunctionToMemoize; | ||
|
||
mimicFn(memoized, fn, { | ||
ignoreNonConfigurable: true | ||
ignoreNonConfigurable: true, | ||
}); | ||
|
||
cacheStore.set(memoized, cache); | ||
|
||
return memoized; | ||
}; | ||
|
||
export = mem; | ||
} | ||
|
||
/** | ||
@returns A [decorator](https://github.com/tc39/proposal-decorators) to memoize class methods or static class methods. | ||
|
||
@example | ||
``` | ||
import mem = require('mem'); | ||
import {memDecorator} from 'mem'; | ||
|
||
class Example { | ||
index = 0 | ||
|
||
@mem.decorator() | ||
@memDecorator() | ||
counter() { | ||
return ++this.index; | ||
} | ||
|
@@ -161,49 +156,51 @@ class Example { | |
class ExampleWithOptions { | ||
index = 0 | ||
|
||
@mem.decorator({maxAge: 1000}) | ||
@memDecorator({maxAge: 1000}) | ||
counter() { | ||
return ++this.index; | ||
} | ||
} | ||
``` | ||
*/ | ||
mem.decorator = < | ||
export function memDecorator< | ||
FunctionToMemoize extends AnyFunction, | ||
CacheKeyType | ||
CacheKeyType, | ||
>( | ||
options: Options<FunctionToMemoize, CacheKeyType> = {} | ||
) => ( | ||
target: any, | ||
propertyKey: string, | ||
descriptor: PropertyDescriptor | ||
): void => { | ||
const input = target[propertyKey]; | ||
|
||
if (typeof input !== 'function') { | ||
throw new TypeError('The decorated value must be a function'); | ||
} | ||
options: Options<FunctionToMemoize, CacheKeyType> = {}, | ||
) { | ||
return ( | ||
target: any, | ||
propertyKey: string, | ||
descriptor: PropertyDescriptor, | ||
): void => { | ||
const input = target[propertyKey]; // eslint-disable-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-member-access | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These errors must be ignored because |
||
|
||
if (typeof input !== 'function') { | ||
throw new TypeError('The decorated value must be a function'); | ||
} | ||
|
||
delete descriptor.value; | ||
delete descriptor.writable; | ||
delete descriptor.value; | ||
delete descriptor.writable; | ||
|
||
descriptor.get = function () { | ||
if (!decoratorInstanceMap.has(this)) { | ||
const value = mem(input, options); | ||
decoratorInstanceMap.set(this, value); | ||
return value; | ||
} | ||
descriptor.get = function () { | ||
if (!decoratorInstanceMap.has(this)) { | ||
const value = mem(input, options) as FunctionToMemoize; | ||
decoratorInstanceMap.set(this, value); | ||
return value; | ||
} | ||
|
||
return decoratorInstanceMap.get(this); | ||
return decoratorInstanceMap.get(this) as FunctionToMemoize; | ||
}; | ||
}; | ||
}; | ||
} | ||
|
||
/** | ||
Clear all cached data of a memoized function. | ||
|
||
@param fn - Memoized function. | ||
*/ | ||
mem.clear = (fn: AnyFunction): void => { | ||
export function memClear(fn: AnyFunction): void { | ||
const cache = cacheStore.get(fn); | ||
if (!cache) { | ||
throw new TypeError('Can\'t clear a function that was not memoized!'); | ||
|
@@ -214,4 +211,4 @@ mem.clear = (fn: AnyFunction): void => { | |
} | ||
|
||
cache.clear(); | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because the return value of
AnyFunction
isany
andFunctionToMemoize extends AnyFunction
, the linter assumes thatReturnType<FunctionToMemoize>
isany
. However, the error is thatany
is not assignable toReturnType<FunctionToMemoize>
:This might be a bug with typescript-eslint.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Open an issue ;)