Adjust memoize normalize for BigInt (#806)
* Adjust memoize normalizer to cater for BigInt * Update memomize typings * cleanups * Renames
This commit is contained in:
+1
-1
@@ -21,7 +21,7 @@
|
||||
},
|
||||
"devDependencies": {
|
||||
"@babel/core": "^7.12.10",
|
||||
"@polkadot/dev": "^0.61.1",
|
||||
"@polkadot/dev": "^0.61.2",
|
||||
"@polkadot/ts": "^0.3.58",
|
||||
"@types/jest": "^26.0.19"
|
||||
},
|
||||
|
||||
@@ -68,4 +68,10 @@ describe('memoize', (): void => {
|
||||
expect(fn(1, 3)).toEqual(4);
|
||||
expect(spy).toHaveBeenCalledTimes(4);
|
||||
});
|
||||
|
||||
it('normalizes BigInt values as well', (): void => {
|
||||
const fn = memoize((a: BigInt, b: BigInt) => BigInt(a) + BigInt(b));
|
||||
|
||||
expect(fn(1n, 2n).toString()).toEqual('3');
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1,24 +1,33 @@
|
||||
// Copyright 2017-2020 @polkadot/util authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
/* eslint-disable @typescript-eslint/no-explicit-any */
|
||||
|
||||
import type { Memoized } from './types';
|
||||
|
||||
import { isBigInt } from './is/bigInt';
|
||||
import { isUndefined } from './is/undefined';
|
||||
|
||||
interface Options {
|
||||
getInstanceId?: () => string,
|
||||
normalize?: (args: any[]) => string;
|
||||
getInstanceId?: () => string;
|
||||
}
|
||||
|
||||
const INSTANCEID = () => 'none';
|
||||
function defaultGetId (): string {
|
||||
return 'none';
|
||||
}
|
||||
|
||||
export function memoize <T> (fn: (...args: any[]) => T, { getInstanceId = INSTANCEID }: Options = {}): Memoized<T> {
|
||||
function normalize (args: unknown[]): string {
|
||||
return JSON.stringify(args, (_, value: unknown) =>
|
||||
isBigInt(value)
|
||||
? value.toString()
|
||||
: value
|
||||
);
|
||||
}
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
export function memoize <T, F extends (...args: any[]) => T> (fn: F, { getInstanceId = defaultGetId }: Options = {}): Memoized<F> {
|
||||
const cache: Record<string, Record<string, T>> = {};
|
||||
|
||||
const memoized = (...args: any[]): T => {
|
||||
const stringParams = JSON.stringify({ args });
|
||||
const memoized = (...args: unknown[]): T => {
|
||||
const stringParams = normalize(args);
|
||||
const instanceId = getInstanceId();
|
||||
|
||||
if (!cache[instanceId]) {
|
||||
@@ -32,8 +41,8 @@ export function memoize <T> (fn: (...args: any[]) => T, { getInstanceId = INSTAN
|
||||
return cache[instanceId][stringParams];
|
||||
};
|
||||
|
||||
memoized.unmemoize = (...args: any[]): void => {
|
||||
const stringParams = JSON.stringify({ args });
|
||||
memoized.unmemoize = (...args: unknown[]): void => {
|
||||
const stringParams = normalize(args);
|
||||
const instanceId = getInstanceId();
|
||||
|
||||
if (cache[instanceId] && !isUndefined(cache[instanceId][stringParams])) {
|
||||
@@ -41,5 +50,5 @@ export function memoize <T> (fn: (...args: any[]) => T, { getInstanceId = INSTAN
|
||||
}
|
||||
};
|
||||
|
||||
return memoized;
|
||||
return memoized as Memoized<F>;
|
||||
}
|
||||
|
||||
@@ -48,9 +48,6 @@ export interface Time {
|
||||
milliseconds: number;
|
||||
}
|
||||
|
||||
export interface Memoized <T> {
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
(...args: any[]): T;
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
unmemoize: (...args: any[]) => void;
|
||||
export type Memoized<F> = F & {
|
||||
unmemoize: (...args: unknown[]) => void;
|
||||
}
|
||||
|
||||
@@ -1686,9 +1686,9 @@ __metadata:
|
||||
languageName: node
|
||||
linkType: hard
|
||||
|
||||
"@polkadot/dev@npm:^0.61.1":
|
||||
version: 0.61.1
|
||||
resolution: "@polkadot/dev@npm:0.61.1"
|
||||
"@polkadot/dev@npm:^0.61.2":
|
||||
version: 0.61.2
|
||||
resolution: "@polkadot/dev@npm:0.61.2"
|
||||
dependencies:
|
||||
"@babel/cli": ^7.12.10
|
||||
"@babel/core": ^7.12.10
|
||||
@@ -1775,7 +1775,7 @@ __metadata:
|
||||
polkadot-exec-typedoc: scripts/polkadot-exec-typedoc.cjs
|
||||
polkadot-exec-vuepress: scripts/polkadot-exec-vuepress.cjs
|
||||
polkadot-exec-webpack: scripts/polkadot-exec-webpack.cjs
|
||||
checksum: b483cff04845f5acb075618a93dbe4472d4a63c027a9b5cbcb6015da930e9942bebad5f1215a47f82ca844e260d60cefec65bdd492b008a0fcc3eff23ae7c948
|
||||
checksum: aada176bd714442e309941c42f0cf48466599aa1e0cbca894e00c91b6efd2eb6b11a677f1e607d46f3245a780934d5f0b4b4029ee2124e8d41cb5f6c96423b21
|
||||
languageName: node
|
||||
linkType: hard
|
||||
|
||||
@@ -9000,7 +9000,7 @@ fsevents@~2.1.2:
|
||||
resolution: "root-workspace-0b6124@workspace:."
|
||||
dependencies:
|
||||
"@babel/core": ^7.12.10
|
||||
"@polkadot/dev": ^0.61.1
|
||||
"@polkadot/dev": ^0.61.2
|
||||
"@polkadot/ts": ^0.3.58
|
||||
"@types/jest": ^26.0.19
|
||||
languageName: unknown
|
||||
|
||||
Reference in New Issue
Block a user