Skip to content

Commit

Permalink
fix(core): Fix stripProtoKeys performance
Browse files Browse the repository at this point in the history
  • Loading branch information
mjgp2 committed Jun 7, 2022
1 parent ad39c3c commit 2cca33a
Showing 1 changed file with 19 additions and 5 deletions.
24 changes: 19 additions & 5 deletions packages/common/pipes/validation.pipe.ts
@@ -1,4 +1,5 @@
import { iterate } from 'iterare';
import { isTypedArray } from 'util/types';
import { Optional } from '../decorators';
import { Injectable } from '../decorators/core';
import { HttpStatus } from '../enums/http-status.enum';
Expand Down Expand Up @@ -190,12 +191,25 @@ export class ValidationPipe implements PipeTransform<any> {
return isNil(value) ? {} : value;
}

protected stripProtoKeys(value: Record<string, any>) {
protected stripProtoKeys(value: any) {
if ( isTypedArray(value) ) {
return;
}
if ( Array.isArray(value) ) {
for ( const v of value ) {
if ( typeof v === 'object' ) {
this.stripProtoKeys(v);
}
}
return;
}
delete value.__proto__;
const keys = Object.keys(value);
iterate(keys)
.filter(key => isObject(value[key]) && value[key])
.forEach(key => this.stripProtoKeys(value[key]));
for ( const key in value ) {
const v = value[key];
if ( typeof v === 'object' ) {
this.stripProtoKeys(v);
}
}
}

protected isPrimitive(value: unknown): boolean {
Expand Down

0 comments on commit 2cca33a

Please sign in to comment.