-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fd6b18e
commit 9ee1991
Showing
2 changed files
with
72 additions
and
55 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,72 @@ | ||
type SameAs<T extends (...args: any) => any> = ( | ||
Check warning on line 1 in src/memoize.ts GitHub Actions / build / build
|
||
...args: Parameters<T> | ||
) => ReturnType<T>; | ||
|
||
export const memize = <T extends () => ReturnType<T>>(fn: T): SameAs<T> => { | ||
let called = false, | ||
lastResult: ReturnType<T>; | ||
return function () { | ||
if (called) { | ||
return lastResult; | ||
} | ||
const result = fn(); | ||
lastResult = result; | ||
called = true; | ||
return result; | ||
}; | ||
}, | ||
memoize = <T extends (arg: any) => ReturnType<T>>(fn: T): SameAs<T> => { | ||
let lastArg: Parameters<T>[0], lastResult: ReturnType<T>; | ||
return function (arg: Parameters<T>[0]) { | ||
if (lastArg === arg) { | ||
return lastResult; | ||
} | ||
|
||
const result = fn(arg); | ||
lastResult = result; | ||
lastArg = arg; | ||
return result; | ||
}; | ||
}, | ||
memooize = <T extends (arg1: any, arg2: any) => ReturnType<T>>( | ||
Check warning on line 31 in src/memoize.ts GitHub Actions / build / build
|
||
fn: T, | ||
): SameAs<T> => { | ||
let lastArg1: Parameters<T>[0], | ||
lastArg2: Parameters<T>[1], | ||
lastResult: ReturnType<T>; | ||
return function (arg1: Parameters<T>[0], arg2: Parameters<T>[1]) { | ||
if (lastArg1 === arg1 && lastArg2 === arg2) { | ||
return lastResult; | ||
} | ||
|
||
const result = fn(arg1, arg2); | ||
lastResult = result; | ||
lastArg1 = arg1; | ||
lastArg2 = arg2; | ||
return result; | ||
}; | ||
}, | ||
memoooize = <T extends (arg1: any, arg2: any, arg3: any) => ReturnType<T>>( | ||
Check warning on line 49 in src/memoize.ts GitHub Actions / build / build
Check warning on line 49 in src/memoize.ts GitHub Actions / build / build
|
||
fn: T, | ||
): SameAs<T> => { | ||
let lastArg1: Parameters<T>[0], | ||
lastArg2: Parameters<T>[1], | ||
lastArg3: Parameters<T>[2], | ||
lastResult: ReturnType<T>; | ||
return function ( | ||
arg1: Parameters<T>[0], | ||
arg2: Parameters<T>[1], | ||
arg3: Parameters<T>[2], | ||
) { | ||
if (lastArg1 === arg1 && lastArg2 === arg2 && lastArg3 === arg3) { | ||
return lastResult; | ||
} | ||
|
||
const result = fn(arg1, arg2, arg3); | ||
lastResult = result; | ||
lastArg1 = arg1; | ||
lastArg2 = arg2; | ||
lastArg3 = arg3; | ||
return result; | ||
}; | ||
}; |