Skip to content

Commit

Permalink
feat(memoize): added types
Browse files Browse the repository at this point in the history
  • Loading branch information
cristinecula committed Oct 7, 2024
1 parent fd6b18e commit 9ee1991
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 55 deletions.
55 changes: 0 additions & 55 deletions src/memoize.js

This file was deleted.

72 changes: 72 additions & 0 deletions src/memoize.ts
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

View workflow job for this annotation

GitHub Actions / build / build

Unexpected any. Specify a different type

Check warning on line 1 in src/memoize.ts

View workflow job for this annotation

GitHub Actions / build / build

Unexpected any. Specify a different type
...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> => {

Check warning on line 18 in src/memoize.ts

View workflow job for this annotation

GitHub Actions / build / build

Unexpected any. Specify a different type
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

View workflow job for this annotation

GitHub Actions / build / build

Unexpected any. Specify a different type

Check warning on line 31 in src/memoize.ts

View workflow job for this annotation

GitHub Actions / build / build

Unexpected any. Specify a different type
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

View workflow job for this annotation

GitHub Actions / build / build

Unexpected any. Specify a different type

Check warning on line 49 in src/memoize.ts

View workflow job for this annotation

GitHub Actions / build / build

Unexpected any. Specify a different type

Check warning on line 49 in src/memoize.ts

View workflow job for this annotation

GitHub Actions / build / build

Unexpected any. Specify a different type
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;
};
};

0 comments on commit 9ee1991

Please sign in to comment.