fix(common): let case conversion pipes accept type unions with null (#36259) (#37447)

The old implementation of case conversion types can handle several
values which are not strings, but the signature did not reflect this.

The new one reports errors when falsy non-string inputs are given to
the pipe (such as `false` or `0`) and has a new signature which
instead reflects the behaviour on `null` and `undefined`.

Fixes #36259

BREAKING CHANGE:
The case conversion pipes no longer let falsy values through. They now
map both `null` and `undefined` to `null` and raise an exception on
invalid input (`0`, `false`, `NaN`) just like most "common pipes". If
your code required falsy values to pass through, you need to handle them
explicitly.

PR Close #37447
This commit is contained in:
Andrea Canciani
2020-03-27 11:22:08 +01:00
committed by Alex Rickabaugh
parent 323be39297
commit c7d5555dfb
3 changed files with 54 additions and 9 deletions

View File

@ -25,8 +25,18 @@ import {LowerCasePipe, TitleCasePipe, UpperCasePipe} from '@angular/common';
expect(pipe.transform('BAr')).toEqual('bar');
});
it('should map null to null', () => {
expect(pipe.transform(null)).toEqual(null);
});
it('should map undefined to null', () => {
expect(pipe.transform(undefined)).toEqual(null);
});
it('should not support numbers', () => {
expect(() => pipe.transform(0 as any)).toThrowError();
});
it('should not support other objects', () => {
expect(() => pipe.transform(<any>{})).toThrowError();
expect(() => pipe.transform({} as any)).toThrowError();
});
});
@ -80,8 +90,18 @@ import {LowerCasePipe, TitleCasePipe, UpperCasePipe} from '@angular/common';
expect(pipe.transform('éric')).toEqual('Éric');
});
it('should map null to null', () => {
expect(pipe.transform(null)).toEqual(null);
});
it('should map undefined to null', () => {
expect(pipe.transform(undefined)).toEqual(null);
});
it('should not support numbers', () => {
expect(() => pipe.transform(0 as any)).toThrowError();
});
it('should not support other objects', () => {
expect(() => pipe.transform(<any>{})).toThrowError();
expect(() => pipe.transform({} as any)).toThrowError();
});
});
@ -101,8 +121,18 @@ import {LowerCasePipe, TitleCasePipe, UpperCasePipe} from '@angular/common';
expect(pipe.transform('bar')).toEqual('BAR');
});
it('should map null to null', () => {
expect(pipe.transform(null)).toEqual(null);
});
it('should map undefined to null', () => {
expect(pipe.transform(undefined)).toEqual(null);
});
it('should not support numbers', () => {
expect(() => pipe.transform(0 as any)).toThrowError();
});
it('should not support other objects', () => {
expect(() => pipe.transform(<any>{})).toThrowError();
expect(() => pipe.transform({} as any)).toThrowError();
});
});
}