From 4708b248d5bb76c09014a3ab1732540a854b0078 Mon Sep 17 00:00:00 2001 From: Victor Berchet Date: Fri, 4 Nov 2016 14:49:53 -0700 Subject: [PATCH] fix(common): I18nSelectPipe selects other case on default --- .../common/src/pipes/i18n_select_pipe.ts | 20 ++++++++---- .../test/pipes/i18n_select_pipe_spec.ts | 31 ++++++++----------- 2 files changed, 27 insertions(+), 24 deletions(-) diff --git a/modules/@angular/common/src/pipes/i18n_select_pipe.ts b/modules/@angular/common/src/pipes/i18n_select_pipe.ts index 92ce7f7a0e..d6bb636cae 100644 --- a/modules/@angular/common/src/pipes/i18n_select_pipe.ts +++ b/modules/@angular/common/src/pipes/i18n_select_pipe.ts @@ -7,7 +7,6 @@ */ import {Pipe, PipeTransform} from '@angular/core'; -import {isBlank} from '../facade/lang'; import {InvalidPipeArgumentError} from './invalid_pipe_argument_error'; /** @@ -16,9 +15,10 @@ import {InvalidPipeArgumentError} from './invalid_pipe_argument_error'; * @howToUse `expression | i18nSelect:mapping` * @description * - * Where: - * - `mapping`: is an object that indicates the text that should be displayed + * Where `mapping` is an object that indicates the text that should be displayed * for different values of the provided `expression`. + * If none of the keys of the mapping match the value of the `expression`, then the content + * of the `other` key is returned when present, otherwise an empty string is returned. * * ## Example * @@ -29,12 +29,20 @@ import {InvalidPipeArgumentError} from './invalid_pipe_argument_error'; @Pipe({name: 'i18nSelect', pure: true}) export class I18nSelectPipe implements PipeTransform { transform(value: string, mapping: {[key: string]: string}): string { - if (isBlank(value)) return ''; + if (value == null) return ''; - if (typeof mapping !== 'object' || mapping === null) { + if (typeof mapping !== 'object' || typeof value !== 'string') { throw new InvalidPipeArgumentError(I18nSelectPipe, mapping); } - return mapping[value] || ''; + if (mapping.hasOwnProperty(value)) { + return mapping[value]; + } + + if (mapping.hasOwnProperty('other')) { + return mapping['other']; + } + + return ''; } } diff --git a/modules/@angular/common/test/pipes/i18n_select_pipe_spec.ts b/modules/@angular/common/test/pipes/i18n_select_pipe_spec.ts index 2cecbf1bfd..191a607d62 100644 --- a/modules/@angular/common/test/pipes/i18n_select_pipe_spec.ts +++ b/modules/@angular/common/test/pipes/i18n_select_pipe_spec.ts @@ -8,40 +8,35 @@ import {I18nSelectPipe} from '@angular/common'; import {PipeResolver} from '@angular/compiler/src/pipe_resolver'; -import {beforeEach, describe, expect, it} from '@angular/core/testing/testing_internal'; export function main() { describe('I18nSelectPipe', () => { - var pipe: I18nSelectPipe; - var mapping = {'male': 'Invite him.', 'female': 'Invite her.', 'other': 'Invite them.'}; - - beforeEach(() => { pipe = new I18nSelectPipe(); }); + const pipe: I18nSelectPipe = new I18nSelectPipe(); + const mapping = {'male': 'Invite him.', 'female': 'Invite her.', 'other': 'Invite them.'}; it('should be marked as pure', () => { expect(new PipeResolver().resolve(I18nSelectPipe).pure).toEqual(true); }); describe('transform', () => { - it('should return male text if value is male', () => { - var val = pipe.transform('male', mapping); + it('should return the "male" text if value is "male"', () => { + const val = pipe.transform('male', mapping); expect(val).toEqual('Invite him.'); }); - it('should return female text if value is female', () => { - var val = pipe.transform('female', mapping); + it('should return the "female" text if value is "female"', () => { + const val = pipe.transform('female', mapping); expect(val).toEqual('Invite her.'); }); - it('should return "" if value is anything other than male or female', () => { - var val = pipe.transform('Anything else', mapping); - expect(val).toEqual(''); + it('should return the "other" text if value is neither "male" nor "female"', + () => { expect(pipe.transform('Anything else', mapping)).toEqual('Invite them.'); }); + + it('should return an empty text if value is null or undefined', () => { + expect(pipe.transform(null, mapping)).toEqual(''); + expect(pipe.transform(void 0, mapping)).toEqual(''); }); - it('should use "" if value is undefined', () => { - var val = pipe.transform(void(0), mapping); - expect(val).toEqual(''); - }); - - it('should not support bad arguments', + it('should throw on bad arguments', () => { expect(() => pipe.transform('male', 'hey')).toThrowError(); }); });