From 573622f92a11fdd126982184dc16a4ab3e03ecb3 Mon Sep 17 00:00:00 2001 From: Kamil Gajowy Date: Tue, 1 Jun 2021 07:47:24 +0200 Subject: [PATCH] refactor: move maybe-type to shared utils --- .../modules/surface-cost/adapters/extract-pu-cost.spec.ts | 4 ++-- .../src/modules/surface-cost/adapters/extract-pu-cost.ts | 8 +++++--- api/libs/utils/src/types/index.ts | 1 + api/libs/utils/src/types/maybe-properties.ts | 1 + 4 files changed, 9 insertions(+), 5 deletions(-) create mode 100644 api/libs/utils/src/types/index.ts create mode 100644 api/libs/utils/src/types/maybe-properties.ts diff --git a/api/apps/geoprocessing/src/modules/surface-cost/adapters/extract-pu-cost.spec.ts b/api/apps/geoprocessing/src/modules/surface-cost/adapters/extract-pu-cost.spec.ts index 5b30b38622..2d0cc835b7 100644 --- a/api/apps/geoprocessing/src/modules/surface-cost/adapters/extract-pu-cost.spec.ts +++ b/api/apps/geoprocessing/src/modules/surface-cost/adapters/extract-pu-cost.spec.ts @@ -26,13 +26,13 @@ describe(`when features miss cost`, () => { describe(`when given GeoJson isn't a FeatureCollection`, () => { it(`throws exception`, () => { expect(() => sut.extract(fixtures.simpleGeometry())).toThrow( - /is supported/, + /Only FeatureCollection is supported/, ); }); }); describe(`when given GeoJson has pu costs`, () => { - it(`throws exception`, () => { + it(`resolves them`, () => { expect(sut.extract(fixtures.geoFeaturesWithData())).toMatchInlineSnapshot(` Array [ Object { diff --git a/api/apps/geoprocessing/src/modules/surface-cost/adapters/extract-pu-cost.ts b/api/apps/geoprocessing/src/modules/surface-cost/adapters/extract-pu-cost.ts index dd21e9560d..0adcf55838 100644 --- a/api/apps/geoprocessing/src/modules/surface-cost/adapters/extract-pu-cost.ts +++ b/api/apps/geoprocessing/src/modules/surface-cost/adapters/extract-pu-cost.ts @@ -1,4 +1,5 @@ import { isDefined } from '@marxan/utils'; +import { MaybeProperties } from '@marxan/utils/types'; import { FeatureCollection, GeoJSON, Geometry } from 'geojson'; import { PlanningUnitCost } from '../ports/planning-unit-cost'; import { PuExtractorPort } from '../ports/pu-extractor/pu-extractor.port'; @@ -7,14 +8,15 @@ type Properties = { cost: number; planningUnitId: string; }; -type MaybeProperties = Partial | undefined | null; + +type MaybeCost = MaybeProperties; export class ExtractPuCost implements PuExtractorPort { extract(geo: GeoJSON): PlanningUnitCost[] { if (!this.isFeatureCollection(geo)) { throw new Error('Only FeatureCollection is supported.'); } - const input: FeatureCollection = geo; + const input: FeatureCollection = geo; const puCosts = input.features .map((feature) => feature.properties) @@ -36,7 +38,7 @@ export class ExtractPuCost implements PuExtractorPort { return geo.type === 'FeatureCollection'; } - private hasCostValues(properties: MaybeProperties): properties is Properties { + private hasCostValues(properties: MaybeCost): properties is Properties { return ( isDefined(properties) && isDefined(properties.cost) && diff --git a/api/libs/utils/src/types/index.ts b/api/libs/utils/src/types/index.ts new file mode 100644 index 0000000000..57cfe27a74 --- /dev/null +++ b/api/libs/utils/src/types/index.ts @@ -0,0 +1 @@ +export { MaybeProperties } from './maybe-properties'; diff --git a/api/libs/utils/src/types/maybe-properties.ts b/api/libs/utils/src/types/maybe-properties.ts new file mode 100644 index 0000000000..c9c2ab203c --- /dev/null +++ b/api/libs/utils/src/types/maybe-properties.ts @@ -0,0 +1 @@ +export type MaybeProperties = Partial | undefined | null;