From c46161a416094b933f0bc31476bf40e874ce8435 Mon Sep 17 00:00:00 2001 From: "Sakamoto, Kazunori" Date: Fri, 3 Jan 2025 17:06:17 +0900 Subject: [PATCH] refactor: refactor code --- src/memoize.ts | 6 +++--- src/memoizeOne.ts | 6 +++--- src/memoizeWithPersistentCache.ts | 15 ++++----------- 3 files changed, 10 insertions(+), 17 deletions(-) diff --git a/src/memoize.ts b/src/memoize.ts index 42675a2..8f8dcd2 100644 --- a/src/memoize.ts +++ b/src/memoize.ts @@ -46,12 +46,12 @@ export function memoizeFactory({ context?: | ClassMethodDecoratorContext Return> | ClassGetterDecoratorContext - ): (this: This, ...args: Args) => Return { + ) { const cache = new Map(); caches?.push(cache); return context?.kind === 'getter' - ? function (this: This): Return { + ? function (this: This) { console.log(`Entering getter ${String(context.name)}.`); const hash = calcHash(this, []); @@ -76,7 +76,7 @@ export function memoizeFactory({ console.log(`Exiting getter ${String(context.name)}.`); return result; } - : function (this: This, ...args: Args): Return { + : function (this: This, ...args: Args) { console.log(`Entering ${context ? `method ${String(context.name)}` : 'function'}(${calcHash(this, args)}).`); const hash = calcHash(this, args); diff --git a/src/memoizeOne.ts b/src/memoizeOne.ts index 78c05b8..6625a49 100644 --- a/src/memoizeOne.ts +++ b/src/memoizeOne.ts @@ -55,13 +55,13 @@ export function memoizeOneFactory({ context?: | ClassMethodDecoratorContext Return> | ClassGetterDecoratorContext - ): (this: This, ...args: Args) => Return { + ) { let lastCache: Return; let lastCachedAt: number; let lastHash: string; return context?.kind === 'getter' - ? function (this: This): Return { + ? function (this: This) { console.log(`Entering getter ${String(context.name)}.`); const hash = calcHash(this, []); @@ -75,7 +75,7 @@ export function memoizeOneFactory({ console.log(`Exiting getter ${String(context.name)}.`); return lastCache; } - : function (this: This, ...args: Args): Return { + : function (this: This, ...args: Args) { console.log(`Entering ${context ? `method ${String(context.name)}` : 'function'}(${JSON.stringify(args)}).`); const hash = calcHash(this, args); diff --git a/src/memoizeWithPersistentCache.ts b/src/memoizeWithPersistentCache.ts index a64e939..bf0ced1 100644 --- a/src/memoizeWithPersistentCache.ts +++ b/src/memoizeWithPersistentCache.ts @@ -34,25 +34,18 @@ export function memoizeWithPersistentCacheFactory({ tryReadingCache: (persistentKey: string, hash: string) => [number, unknown] | undefined; }) { // eslint-disable-next-line @typescript-eslint/no-explicit-any - return function ( - persistentKey: string - ): ( - target: ((this: This, ...args: Args) => Return) | ((...args: Args) => Return) | keyof This, - context?: - | ClassMethodDecoratorContext Return> - | ClassGetterDecoratorContext - ) => (this: This, ...args: Args) => Return { + return function (persistentKey: string) { return function memoize( target: ((this: This, ...args: Args) => Return) | ((...args: Args) => Return) | keyof This, context?: | ClassMethodDecoratorContext Return> | ClassGetterDecoratorContext - ): (this: This, ...args: Args) => Return { + ) { const cache = new Map(); caches?.push(cache); return context?.kind === 'getter' - ? function (this: This): Return { + ? function (this: This) { console.log(`Entering getter ${String(context.name)}.`); const hash = calcHash(this, []); @@ -126,7 +119,7 @@ export function memoizeWithPersistentCacheFactory({ console.log(`Exiting getter ${String(context.name)}.`); return result as Return; } - : function (this: This, ...args: { [K in keyof Args]: Args[K] }): Return { + : function (this: This, ...args: { [K in keyof Args]: Args[K] }) { console.log( `Entering ${context ? `method ${String(context.name)}` : 'function'}(${calcHash(this, args)}).` );