Skip to content

Commit 99c5269

Browse files
JeanMechekirjs
authored andcommitted
feat(common): Support of optional keys for the KeyValue pipe (#48814)
This commit is extending the capabilities of the KeyValue pipe by allowing interfaces with optional keys. fixes #46867 PR Close #48814
1 parent 159be56 commit 99c5269

File tree

3 files changed

+41
-0
lines changed

3 files changed

+41
-0
lines changed

goldens/public-api/common/index.api.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -390,6 +390,8 @@ export class KeyValuePipe implements PipeTransform {
390390
// (undocumented)
391391
transform<K extends string, V>(input: Record<K, V> | ReadonlyMap<K, V> | null | undefined, compareFn?: ((a: KeyValue<K, V>, b: KeyValue<K, V>) => number) | null): Array<KeyValue<K, V>> | null;
392392
// (undocumented)
393+
transform<T, K extends keyof T>(input: T, compareFn?: (a: T[K], b: T[K]) => number): T extends null ? null : Array<T[K]>;
394+
// (undocumented)
393395
static ɵfac: i0.ɵɵFactoryDeclaration<KeyValuePipe, never>;
394396
// (undocumented)
395397
static ɵpipe: i0.ɵɵPipeDeclaration<KeyValuePipe, "keyvalue", true>;

packages/common/src/pipes/keyvalue_pipe.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,10 @@ export class KeyValuePipe implements PipeTransform {
9696
input: Record<K, V> | ReadonlyMap<K, V> | null | undefined,
9797
compareFn?: ((a: KeyValue<K, V>, b: KeyValue<K, V>) => number) | null,
9898
): Array<KeyValue<K, V>> | null;
99+
transform<T, K extends keyof T>(
100+
input: T,
101+
compareFn?: (a: T[K], b: T[K]) => number,
102+
): T extends null ? null : Array<T[K]>;
99103
transform<K, V>(
100104
input: undefined | null | {[key: string]: V; [key: number]: V} | ReadonlyMap<K, V>,
101105
compareFn: ((a: KeyValue<K, V>, b: KeyValue<K, V>) => number) | null = defaultComparator,

packages/common/test/pipes/keyvalue_pipe_spec.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,41 @@ describe('KeyValuePipe', () => {
9999
const pipe = new KeyValuePipe(defaultKeyValueDiffers);
100100
expect(pipe.transform(value)).toEqual(null);
101101
});
102+
103+
it('should accept an object with optional keys', () => {
104+
interface MyInterface {
105+
one: string;
106+
two: number;
107+
three: string;
108+
four: string;
109+
}
110+
const myData: Partial<MyInterface> = {
111+
one: 'One',
112+
two: 2,
113+
three: undefined,
114+
};
115+
116+
const pipe = new KeyValuePipe(defaultKeyValueDiffers);
117+
expect(pipe.transform(myData)?.length).toEqual(3);
118+
119+
const differ = (a: string | number | undefined, b: string | number | undefined): number => {
120+
return 1;
121+
};
122+
expect(pipe.transform(myData, differ)?.length).toEqual(3);
123+
});
124+
125+
it('should accept an nullable object with optional keys', () => {
126+
interface MyInterface {
127+
one?: string;
128+
two?: string;
129+
three?: string;
130+
}
131+
132+
let value!: MyInterface | null;
133+
const pipe = new KeyValuePipe(defaultKeyValueDiffers);
134+
// If null what not supported by this signature we would have gotten a never[] back.
135+
expect(pipe.transform(value)).toEqual(null);
136+
});
102137
});
103138

104139
describe('Map', () => {

0 commit comments

Comments
 (0)