Skip to content

Commit

Permalink
feat(memoize): add optional cache arg for memoizeJ()
Browse files Browse the repository at this point in the history
  • Loading branch information
postspectacular committed Aug 8, 2018
1 parent adc4928 commit 2bc092d
Showing 1 changed file with 8 additions and 7 deletions.
15 changes: 8 additions & 7 deletions packages/memoize/src/memoizej.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { IObjectOf } from "@thi.ng/api/api";
import { Fn1, Fn2, Fn3, Fn4, FnAny } from "./api";

/**
Expand All @@ -12,16 +13,16 @@ import { Fn1, Fn2, Fn3, Fn4, FnAny } from "./api";
* @param fn
* @param cache
*/
export function memoizeJ<A, B>(fn: Fn1<A, B>): Fn1<A, B>;
export function memoizeJ<A, B, C>(fn: Fn2<A, B, C>): Fn2<A, B, C>;
export function memoizeJ<A, B, C, D>(fn: Fn3<A, B, C, D>): Fn3<A, B, C, D>;
export function memoizeJ<A, B, C, D, E>(fn: Fn4<A, B, C, D, E>): Fn4<A, B, C, D, E>;
export function memoizeJ(fn: FnAny): (...args: any[]) => any {
const cache: any = {};
export function memoizeJ<A, B>(fn: Fn1<A, B>, cache?: IObjectOf<B>): Fn1<A, B>;
export function memoizeJ<A, B, C>(fn: Fn2<A, B, C>, cache?: IObjectOf<C>): Fn2<A, B, C>;
export function memoizeJ<A, B, C, D>(fn: Fn3<A, B, C, D>, cache?: IObjectOf<D>): Fn3<A, B, C, D>;
export function memoizeJ<A, B, C, D, E>(fn: Fn4<A, B, C, D, E>, cache?: IObjectOf<E>): Fn4<A, B, C, D, E>;
export function memoizeJ(fn: FnAny, cache?: IObjectOf<any>): FnAny {
!cache && (cache = {});
return (...args: any[]) => {
const key = JSON.stringify(args);
if (key !== undefined) {
return Object.hasOwnProperty(key) ?
return key in cache ?
cache[key] :
(cache[key] = fn.apply(null, args));
}
Expand Down

0 comments on commit 2bc092d

Please sign in to comment.