Adjust memoize normalize for BigInt (#806)
* Adjust memoize normalizer to cater for BigInt * Update memomize typings * cleanups * Renames
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user