Skip to content

Commit

Permalink
refactor: move maybe-type to shared utils
Browse files Browse the repository at this point in the history
  • Loading branch information
kgajowy committed Jun 1, 2021
1 parent 3904d4f commit 573622f
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -7,14 +8,15 @@ type Properties = {
cost: number;
planningUnitId: string;
};
type MaybeProperties = Partial<Properties> | undefined | null;

type MaybeCost = MaybeProperties<Properties>;

export class ExtractPuCost implements PuExtractorPort {
extract(geo: GeoJSON): PlanningUnitCost[] {
if (!this.isFeatureCollection(geo)) {
throw new Error('Only FeatureCollection is supported.');
}
const input: FeatureCollection<Geometry, MaybeProperties> = geo;
const input: FeatureCollection<Geometry, MaybeCost> = geo;

const puCosts = input.features
.map((feature) => feature.properties)
Expand All @@ -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) &&
Expand Down
1 change: 1 addition & 0 deletions api/libs/utils/src/types/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { MaybeProperties } from './maybe-properties';
1 change: 1 addition & 0 deletions api/libs/utils/src/types/maybe-properties.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export type MaybeProperties<T> = Partial<T> | undefined | null;

0 comments on commit 573622f

Please sign in to comment.