mirror of
https://github.com/angular/angular.git
synced 2026-09-14 13:54:52 +08:00
refactor(common): extract argument assertion
Deduplicates string argument validation logic across pipes
by introducing a local assertion helper
(cherry picked from commit 66e8385b6a)
This commit is contained in:
committed by
Andrew Kushnir
parent
caab23dfe6
commit
6f5c233f1d
@@ -6,7 +6,7 @@
|
||||
* found in the LICENSE file at https://angular.dev/license
|
||||
*/
|
||||
|
||||
import {Pipe, PipeTransform} from '@angular/core';
|
||||
import {Pipe, PipeTransform, Type} from '@angular/core';
|
||||
|
||||
import {invalidPipeArgumentError} from './invalid_pipe_argument_error';
|
||||
|
||||
@@ -39,9 +39,7 @@ export class LowerCasePipe implements PipeTransform {
|
||||
transform(value: string | null | undefined): string | null;
|
||||
transform(value: string | null | undefined): string | null {
|
||||
if (value == null) return null;
|
||||
if (typeof value !== 'string') {
|
||||
throw invalidPipeArgumentError(LowerCasePipe, value);
|
||||
}
|
||||
assertPipeArgument(LowerCasePipe, value);
|
||||
return value.toLowerCase();
|
||||
}
|
||||
}
|
||||
@@ -89,9 +87,7 @@ export class TitleCasePipe implements PipeTransform {
|
||||
transform(value: string | null | undefined): string | null;
|
||||
transform(value: string | null | undefined): string | null {
|
||||
if (value == null) return null;
|
||||
if (typeof value !== 'string') {
|
||||
throw invalidPipeArgumentError(TitleCasePipe, value);
|
||||
}
|
||||
assertPipeArgument(TitleCasePipe, value);
|
||||
|
||||
return value.replace(
|
||||
unicodeWordMatch,
|
||||
@@ -121,9 +117,13 @@ export class UpperCasePipe implements PipeTransform {
|
||||
transform(value: string | null | undefined): string | null;
|
||||
transform(value: string | null | undefined): string | null {
|
||||
if (value == null) return null;
|
||||
if (typeof value !== 'string') {
|
||||
throw invalidPipeArgumentError(UpperCasePipe, value);
|
||||
}
|
||||
assertPipeArgument(UpperCasePipe, value);
|
||||
return value.toUpperCase();
|
||||
}
|
||||
}
|
||||
|
||||
function assertPipeArgument(pipe: Type<any>, value: Object): void {
|
||||
if (typeof value !== 'string') {
|
||||
throw invalidPipeArgumentError(pipe, value);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user