mirror of
https://github.com/nestjs/nest.git
synced 2026-02-21 23:11:44 +00:00
Also, add another test for `isString` utility, to clarify that it must return `false` for strings made by `String` constructor.
31 lines
750 B
TypeScript
31 lines
750 B
TypeScript
import { Injectable } from '../decorators/core/injectable.decorator';
|
|
import {
|
|
ArgumentMetadata,
|
|
PipeTransform,
|
|
} from '../interfaces/features/pipe-transform.interface';
|
|
import { isNil, isNumber } from '../utils/shared.utils';
|
|
|
|
/**
|
|
* Defines the built-in DefaultValue Pipe
|
|
*
|
|
* @see [Built-in Pipes](https://docs.nestjs.com/pipes#built-in-pipes)
|
|
*
|
|
* @publicApi
|
|
*/
|
|
@Injectable()
|
|
export class DefaultValuePipe<T = any, R = any>
|
|
implements PipeTransform<T, T | R>
|
|
{
|
|
constructor(private readonly defaultValue: R) {}
|
|
|
|
transform(value?: T, _metadata?: ArgumentMetadata): T | R {
|
|
if (
|
|
isNil(value) ||
|
|
(isNumber(value) && isNaN(value as unknown as number))
|
|
) {
|
|
return this.defaultValue;
|
|
}
|
|
return value;
|
|
}
|
|
}
|