2016-12-08 16:33:24 -08:00
|
|
|
/**
|
|
|
|
|
* @license
|
|
|
|
|
* Copyright Google Inc. All Rights Reserved.
|
|
|
|
|
*
|
|
|
|
|
* Use of this source code is governed by an MIT-style license that can be
|
|
|
|
|
* found in the LICENSE file at https://angular.io/license
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
import {Pipe, PipeTransform} from '@angular/core';
|
2017-01-27 13:19:00 -08:00
|
|
|
import {invalidPipeArgumentError} from './invalid_pipe_argument_error';
|
2016-12-08 16:33:24 -08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Transforms text to lowercase.
|
|
|
|
|
*
|
2016-12-22 21:25:21 +00:00
|
|
|
* {@example common/pipes/ts/lowerupper_pipe.ts region='LowerUpperPipe' }
|
2016-12-08 16:33:24 -08:00
|
|
|
*
|
|
|
|
|
* @stable
|
|
|
|
|
*/
|
|
|
|
|
@Pipe({name: 'lowercase'})
|
|
|
|
|
export class LowerCasePipe implements PipeTransform {
|
|
|
|
|
transform(value: string): string {
|
|
|
|
|
if (!value) return value;
|
|
|
|
|
if (typeof value !== 'string') {
|
2017-01-27 13:19:00 -08:00
|
|
|
throw invalidPipeArgumentError(LowerCasePipe, value);
|
2016-12-08 16:33:24 -08:00
|
|
|
}
|
|
|
|
|
return value.toLowerCase();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-12-16 16:24:26 -07:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Helper method to transform a single word to titlecase.
|
|
|
|
|
*
|
|
|
|
|
* @stable
|
|
|
|
|
*/
|
|
|
|
|
function titleCaseWord(word: string) {
|
|
|
|
|
if (!word) return word;
|
|
|
|
|
return word[0].toUpperCase() + word.substr(1).toLowerCase();
|
|
|
|
|
}
|
|
|
|
|
|
2016-12-08 16:33:24 -08:00
|
|
|
/**
|
|
|
|
|
* Transforms text to titlecase.
|
|
|
|
|
*
|
|
|
|
|
* @stable
|
|
|
|
|
*/
|
|
|
|
|
@Pipe({name: 'titlecase'})
|
|
|
|
|
export class TitleCasePipe implements PipeTransform {
|
|
|
|
|
transform(value: string): string {
|
|
|
|
|
if (!value) return value;
|
|
|
|
|
if (typeof value !== 'string') {
|
2017-01-27 13:19:00 -08:00
|
|
|
throw invalidPipeArgumentError(TitleCasePipe, value);
|
2016-12-08 16:33:24 -08:00
|
|
|
}
|
|
|
|
|
|
2016-12-16 16:24:26 -07:00
|
|
|
return value.split(/\b/g).map(word => titleCaseWord(word)).join('');
|
2016-12-08 16:33:24 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Transforms text to uppercase.
|
|
|
|
|
*
|
|
|
|
|
* @stable
|
|
|
|
|
*/
|
|
|
|
|
@Pipe({name: 'uppercase'})
|
|
|
|
|
export class UpperCasePipe implements PipeTransform {
|
|
|
|
|
transform(value: string): string {
|
|
|
|
|
if (!value) return value;
|
|
|
|
|
if (typeof value !== 'string') {
|
2017-01-27 13:19:00 -08:00
|
|
|
throw invalidPipeArgumentError(UpperCasePipe, value);
|
2016-12-08 16:33:24 -08:00
|
|
|
}
|
|
|
|
|
return value.toUpperCase();
|
|
|
|
|
}
|
|
|
|
|
}
|