From a23973eb360298ee69d6e95f6571cb235d0dd850 Mon Sep 17 00:00:00 2001 From: Romain Gilliotte Date: Mon, 13 Jun 2022 20:23:35 +0200 Subject: [PATCH 1/2] feat: allow projections to union with null (no-op) --- .../datasource-toolkit/src/interfaces/query/projection/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/datasource-toolkit/src/interfaces/query/projection/index.ts b/packages/datasource-toolkit/src/interfaces/query/projection/index.ts index 49f5bca68b..e35b913f7c 100644 --- a/packages/datasource-toolkit/src/interfaces/query/projection/index.ts +++ b/packages/datasource-toolkit/src/interfaces/query/projection/index.ts @@ -31,7 +31,7 @@ export default class Projection extends Array { union(...otherProjections: (Projection | string[])[]): Projection { const fields = [this, ...otherProjections].reduce( - (memo, projection) => [...memo, ...projection], + (memo, projection) => (projection ? [...memo, ...projection] : memo), [], ); From 05a4e75befbb6bd0f7dd3abb3d617231240eacd0 Mon Sep 17 00:00:00 2001 From: Romain Gilliotte Date: Tue, 14 Jun 2022 10:09:53 +0200 Subject: [PATCH 2/2] test: coverage --- .../test/interfaces/projection.test.ts | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/packages/datasource-toolkit/test/interfaces/projection.test.ts b/packages/datasource-toolkit/test/interfaces/projection.test.ts index be0cbde924..2c9e5214f2 100644 --- a/packages/datasource-toolkit/test/interfaces/projection.test.ts +++ b/packages/datasource-toolkit/test/interfaces/projection.test.ts @@ -146,4 +146,19 @@ describe('Projection', () => { }); }); }); + + describe('union', () => { + it('should work with two projections', () => { + const projection1 = new Projection('id', 'title'); + const projection2 = new Projection('id', 'amount'); + + expect(projection1.union(projection2)).toStrictEqual(new Projection('id', 'title', 'amount')); + }); + + it('should work with a null projection', () => { + const projection = new Projection('id', 'title'); + + expect(projection.union(null)).toStrictEqual(projection); + }); + }); });