Skip to content

Commit

Permalink
fix(nestjs): fix memoize util
Browse files Browse the repository at this point in the history
  • Loading branch information
nartc committed Mar 16, 2021
1 parent 76a817b commit e56887b
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 10 deletions.
2 changes: 1 addition & 1 deletion packages/nestjs/src/lib/pipes/map.pipe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ function createMapPipe(
);
}

return this.mapper.map(
return this.mapper?.map(
value,
to as any,
from as any,
Expand Down
14 changes: 9 additions & 5 deletions packages/nestjs/src/lib/utils/memoize.util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,15 @@ export function memoize(fn: Function) {
const cache: Record<string, unknown> = {};
return (...args: any[]) => {
const n =
args.reduce(
(key, arg) =>
key.concat('|', typeof arg === 'string' ? arg : arg.toString()),
''
) || defaultKey;
args.reduce((key, arg) => {
const argToConcat =
typeof arg === 'string'
? arg
: typeof arg === 'object'
? JSON.stringify(arg)
: arg.toString();
return key.concat('|', argToConcat);
}, '') || defaultKey;
if (n in cache) {
return cache[n];
}
Expand Down
8 changes: 4 additions & 4 deletions packages/nestjs/src/lib/utils/transform.util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { isEmpty } from '@automapper/core';
import type { MapOptions, Mapper } from '@automapper/types';

export function shouldSkipTransform(
mapper: Mapper,
mapper: Mapper | undefined,
to: unknown,
from: unknown
): boolean {
Expand All @@ -11,19 +11,19 @@ export function shouldSkipTransform(

export function transformArray(
value: unknown,
mapper: Mapper,
mapper: Mapper | undefined,
to: any,
from: any,
options?: MapOptions
) {
if (!Array.isArray(value)) return value;
return mapper.mapArray(value, to, from, options);
return mapper?.mapArray(value, to, from, options);
}

export function getTransformOptions(
options?: { isArray?: boolean; mapperName?: string } & MapOptions
): {
mapperName: string;
mapperName?: string;
isArray: boolean;
transformedMapOptions?: MapOptions;
} {
Expand Down

0 comments on commit e56887b

Please sign in to comment.