2016-06-23 09:47:54 -07: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
|
|
|
|
*/
|
|
|
|
|
2016-06-08 16:38:52 -07:00
|
|
|
import {Pipe, PipeTransform} from '@angular/core';
|
2017-01-27 13:19:00 -08:00
|
|
|
import {invalidPipeArgumentError} from './invalid_pipe_argument_error';
|
2016-02-26 10:02:52 -08:00
|
|
|
|
|
|
|
/**
|
2016-09-08 21:41:09 -07:00
|
|
|
* @ngModule CommonModule
|
|
|
|
* @description
|
2016-02-26 10:02:52 -08:00
|
|
|
*
|
2018-03-29 11:12:34 +01:00
|
|
|
* Generic selector that displays the string that matches the current value.
|
|
|
|
*
|
2018-03-29 17:04:47 +01:00
|
|
|
* If none of the keys of the `mapping` match the `value`, then the content
|
|
|
|
* of the `other` key is returned when present, otherwise an empty string is returned.
|
2016-02-26 10:02:52 -08:00
|
|
|
*
|
2018-05-03 09:38:05 +01:00
|
|
|
* @usageNotes
|
|
|
|
*
|
|
|
|
* ### Example
|
2016-02-26 10:02:52 -08:00
|
|
|
*
|
2016-09-08 21:41:09 -07:00
|
|
|
* {@example common/pipes/ts/i18n_pipe.ts region='I18nSelectPipeComponent'}
|
2016-05-27 11:24:05 -07:00
|
|
|
*
|
2018-10-19 12:12:20 +01:00
|
|
|
* @publicApi
|
2016-02-26 10:02:52 -08:00
|
|
|
*/
|
|
|
|
@Pipe({name: 'i18nSelect', pure: true})
|
|
|
|
export class I18nSelectPipe implements PipeTransform {
|
2018-03-29 17:04:47 +01:00
|
|
|
/**
|
|
|
|
* @param value a string to be internationalized.
|
|
|
|
* @param mapping an object that indicates the text that should be displayed
|
|
|
|
* for different values of the provided `value`.
|
|
|
|
*/
|
2017-03-24 09:54:02 -07:00
|
|
|
transform(value: string|null|undefined, mapping: {[key: string]: string}): string {
|
2016-11-04 14:49:53 -07:00
|
|
|
if (value == null) return '';
|
2016-06-23 11:44:05 -07:00
|
|
|
|
2016-11-04 14:49:53 -07:00
|
|
|
if (typeof mapping !== 'object' || typeof value !== 'string') {
|
2017-01-27 13:19:00 -08:00
|
|
|
throw invalidPipeArgumentError(I18nSelectPipe, mapping);
|
2016-02-26 10:02:52 -08:00
|
|
|
}
|
|
|
|
|
2016-11-04 14:49:53 -07:00
|
|
|
if (mapping.hasOwnProperty(value)) {
|
|
|
|
return mapping[value];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mapping.hasOwnProperty('other')) {
|
|
|
|
return mapping['other'];
|
|
|
|
}
|
|
|
|
|
|
|
|
return '';
|
2016-02-26 10:02:52 -08:00
|
|
|
}
|
|
|
|
}
|