fix(common): KeyValuePipe should return empty array for empty objects (#27258)

This lets KeyValuePipe return an empty array (rather than undefined)
when the input is empty.

PR Close #27258
This commit is contained in:
Ingo Bürk 2018-11-24 16:00:13 +01:00 committed by Miško Hevery
parent f9545d1b1a
commit b39efdd9d6
2 changed files with 9 additions and 3 deletions

View File

@ -47,10 +47,8 @@ export interface KeyValue<K, V> {
export class KeyValuePipe implements PipeTransform { export class KeyValuePipe implements PipeTransform {
constructor(private readonly differs: KeyValueDiffers) {} constructor(private readonly differs: KeyValueDiffers) {}
// TODO(issue/24571): remove '!'.
private differ !: KeyValueDiffer<any, any>; private differ !: KeyValueDiffer<any, any>;
// TODO(issue/24571): remove '!'. private keyValues: Array<KeyValue<any, any>> = [];
private keyValues !: Array<KeyValue<any, any>>;
transform<K, V>(input: null, compareFn?: (a: KeyValue<K, V>, b: KeyValue<K, V>) => number): null; transform<K, V>(input: null, compareFn?: (a: KeyValue<K, V>, b: KeyValue<K, V>) => number): null;
transform<V>( transform<V>(

View File

@ -25,6 +25,10 @@ describe('KeyValuePipe', () => {
expect(pipe.transform(fn as any)).toEqual(null); expect(pipe.transform(fn as any)).toEqual(null);
}); });
describe('object dictionary', () => { describe('object dictionary', () => {
it('should return empty array of an empty dictionary', () => {
const pipe = new KeyValuePipe(defaultKeyValueDiffers);
expect(pipe.transform({})).toEqual([]);
});
it('should transform a basic dictionary', () => { it('should transform a basic dictionary', () => {
const pipe = new KeyValuePipe(defaultKeyValueDiffers); const pipe = new KeyValuePipe(defaultKeyValueDiffers);
expect(pipe.transform({1: 2})).toEqual([{key: '1', value: 2}]); expect(pipe.transform({1: 2})).toEqual([{key: '1', value: 2}]);
@ -62,6 +66,10 @@ describe('KeyValuePipe', () => {
}); });
describe('Map', () => { describe('Map', () => {
it('should return an empty array for an empty Map', () => {
const pipe = new KeyValuePipe(defaultKeyValueDiffers);
expect(pipe.transform(new Map())).toEqual([]);
});
it('should transform a basic Map', () => { it('should transform a basic Map', () => {
const pipe = new KeyValuePipe(defaultKeyValueDiffers); const pipe = new KeyValuePipe(defaultKeyValueDiffers);
expect(pipe.transform(new Map([[1, 2]]))).toEqual([{key: 1, value: 2}]); expect(pipe.transform(new Map([[1, 2]]))).toEqual([{key: 1, value: 2}]);