Skip to content

Commit

Permalink
feat: Add all resolvers unit tests #12
Browse files Browse the repository at this point in the history
  • Loading branch information
regevbr committed Apr 12, 2020
1 parent fa6cf66 commit 1e3bf92
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 11 deletions.
37 changes: 31 additions & 6 deletions src/utils/memoize/memoize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ const cacheProp = Symbol.for(`[memoize]`);

export type GenericFunction<A extends any[] = any[], R = any> = (...args: A) => R;

const defaultKeyBuilder = (v: any): any => {
return v;
const defaultKeyBuilder: GenericFunction<[], string> = (): string => {
return `__no_args__`;
};

const getCache = (target: any): Record<string, Map<any, any>> => {
Expand All @@ -24,8 +24,12 @@ const getKeyCache = (target: any, key: string): Map<any, any> => {
return dict[key];
};

const memoizeFn = (namespace: string, func: GenericFunction, keyBuilder: GenericFunction): GenericFunction => {
return function (this: any, ...args: any[]): any {
const memoizeFn = <A extends any[] = any[]>(
namespace: string,
func: GenericFunction,
keyBuilder: GenericFunction<A, string>
): GenericFunction => {
return function (this: any, ...args: A): any {
const cache = getKeyCache(this, namespace);
const key = keyBuilder.apply(this, args);
if (cache.has(key)) {
Expand All @@ -37,13 +41,34 @@ const memoizeFn = (namespace: string, func: GenericFunction, keyBuilder: Generic
};
};

export const memoize = <
type MemoizeReturn<
T extends Function,
A extends any[] = T extends (...args: infer AReal) => any ? AReal : any[],
R = T extends (...args: any) => infer RReal ? RReal : any
> = (_: object, propertyKey: string, descriptor: TypedPropertyDescriptor<GenericFunction<A, R>>) => void;

type Memoize = {
<
T extends Function,
A extends [] = T extends (...args: []) => any ? [] : never,
R = T extends (...args: any) => infer RReal ? RReal : any
>(): MemoizeReturn<T, A, R>;
<
T extends Function,
A extends any[] = T extends (...args: infer AReal) => any ? AReal : any[],
R = T extends (...args: any) => infer RReal ? RReal : any
>(
keyBuilder: GenericFunction<A, string>
): MemoizeReturn<T, A, R>;
};

export const memoize: Memoize = <
T extends Function,
A extends any[] = T extends (...args: infer AReal) => any ? AReal : any[],
R = T extends (...args: any) => infer RReal ? RReal : any
>(
keyBuilder?: GenericFunction<A, string>
) => {
): MemoizeReturn<T, A, R> => {
return (_: object, propertyKey: string, descriptor: TypedPropertyDescriptor<GenericFunction<A, R>>): void => {
descriptor.value = memoizeFn(propertyKey, descriptor.value!, keyBuilder || defaultKeyBuilder);
};
Expand Down
6 changes: 3 additions & 3 deletions src/utils/nodeVersions/impl/nodeVersions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export class NodeVersions extends INodeVersions {
this.logger = loggerFactory.getLogger(`Node Versions`);
}

@memoize((options: INodeVersionsLtsOptions): string => options.codename.toLowerCase())
@memoize(({ codename }: INodeVersionsLtsOptions): string => codename.toLowerCase())
public async resolveLtsVersion({ codename }: INodeVersionsLtsOptions): Promise<string | undefined> {
codename = codename.toLowerCase();
this.logger.debug(`Resolving LTS version of ${codename}`);
Expand All @@ -57,7 +57,7 @@ export class NodeVersions extends INodeVersions {
return lts;
}

@memoize((options: INodeVersionsLatestOptions): string => options.date.toJSON())
@memoize(({ date }: INodeVersionsLatestOptions): string => date.toJSON())
public async resolveLatestLtsVersion({ date }: INodeVersionsLatestOptions): Promise<string | undefined> {
this.logger.debug(`Resolving latest lts version in ${date.toJSON()}`);
const versions = await this.getAllVersions();
Expand All @@ -76,7 +76,7 @@ export class NodeVersions extends INodeVersions {
return latest;
}

@memoize((options: INodeVersionsLatestOptions): string => options.date.toJSON())
@memoize(({ date }: INodeVersionsLatestOptions): string => date.toJSON())
public async resolveStableVersion({ date }: INodeVersionsLatestOptions): Promise<string | undefined> {
this.logger.debug(`Resolving stable version in ${date.toJSON()}`);
const versions = await this.getAllVersions();
Expand Down
4 changes: 2 additions & 2 deletions test/src/utils/memoize/memoize.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ describe(`memoize`, () => {
}

@memoize()
public method1(...args: any[]): string {
return this.method1Mock(...args);
public method1(arg: string): string {
return this.method1Mock(arg);
}

@memoize((...args: any[]) => args.join(`:`))
Expand Down

0 comments on commit 1e3bf92

Please sign in to comment.