From 867c080e97f5acfbfff3f048727292f3a9333f56 Mon Sep 17 00:00:00 2001 From: Kai Volland Date: Wed, 10 Apr 2019 15:05:19 +0200 Subject: [PATCH 1/4] Small enhancements - Removes "async" - Adds else branch if no FeatureCollection found - Removes "string" as allowed input type --- src/ShapefileDataParser.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/ShapefileDataParser.ts b/src/ShapefileDataParser.ts index 2eee50a..5a3017a 100644 --- a/src/ShapefileDataParser.ts +++ b/src/ShapefileDataParser.ts @@ -33,13 +33,15 @@ export class ShapefileDataParser implements DataParser { * * @param inputData */ - async readData(array: string | Buffer | ArrayBuffer): Promise { + readData(array: Buffer | ArrayBuffer): Promise { return new Promise((resolve, reject) => { try { shpjs(array) .then((geojson: any) => { if (geojson.type === 'FeatureCollection') { resolve(this._geoJsonParser.readData(geojson)); + } else { + reject('Could not get FeatureCollection from shapefile.'); } }) .catch((e: any) => { From 2d306cb1535f15be0aa96ee9088a20c7387ea94d Mon Sep 17 00:00:00 2001 From: Kai Volland Date: Wed, 10 Apr 2019 15:41:59 +0200 Subject: [PATCH 2/4] Removes unused package --- package.json | 1 - 1 file changed, 1 deletion(-) diff --git a/package.json b/package.json index 7b22ab7..1493cff 100644 --- a/package.json +++ b/package.json @@ -28,7 +28,6 @@ "dependencies": { "@types/geojson": "7946.0.7", "@types/jest": "24.0.11", - "@types/json-schema": "7.0.3", "@types/node": "11.13.2", "@types/shpjs": "3.4.0", "geostyler-data": "1.0.0", From c02c258b488ad49a59ca9ae7aea877666a5e9110 Mon Sep 17 00:00:00 2001 From: Kai Volland Date: Wed, 10 Apr 2019 15:42:59 +0200 Subject: [PATCH 3/4] Removes and refactors code --- src/ShapefileDataParser.ts | 33 +++++++++------------------------ 1 file changed, 9 insertions(+), 24 deletions(-) diff --git a/src/ShapefileDataParser.ts b/src/ShapefileDataParser.ts index 5a3017a..46d8e3c 100644 --- a/src/ShapefileDataParser.ts +++ b/src/ShapefileDataParser.ts @@ -1,4 +1,5 @@ import { DataParser, VectorData } from 'geostyler-data'; +import { GeoJSON } from 'geojson'; import GeoJsonDataParser from 'geostyler-geojson-parser'; // @ts-ignore // Typing is currently wrong. See PR: https://github.com/DefinitelyTyped/DefinitelyTyped/pull/34623 import shpjs from 'shpjs'; @@ -15,18 +16,10 @@ export class ShapefileDataParser implements DataParser { title = 'Shapefile Data Parser'; - sourceProjection: string; - - targetProjection: string; - _geoJsonParser: any; constructor(sourceProj?: string, targetProj?: string) { - this._geoJsonParser = new GeoJsonDataParser(); - if (sourceProj && targetProj) { - this.sourceProjection = sourceProj; - this.targetProjection = targetProj; - } + this._geoJsonParser = new GeoJsonDataParser(sourceProj, targetProj); } /** @@ -35,21 +28,13 @@ export class ShapefileDataParser implements DataParser { */ readData(array: Buffer | ArrayBuffer): Promise { return new Promise((resolve, reject) => { - try { - shpjs(array) - .then((geojson: any) => { - if (geojson.type === 'FeatureCollection') { - resolve(this._geoJsonParser.readData(geojson)); - } else { - reject('Could not get FeatureCollection from shapefile.'); - } - }) - .catch((e: any) => { - reject(e); - }); - } catch (e) { - reject(e); - } + shpjs(array) + .then((geojson: GeoJSON) => { + resolve(this._geoJsonParser.readData(geojson)); + }) + .catch((e: any) => { + reject(e); + }); }); } From 141cbcf87020f6e2f22cee1390ac1494c07a7f0b Mon Sep 17 00:00:00 2001 From: Kai Volland Date: Wed, 10 Apr 2019 15:43:06 +0200 Subject: [PATCH 4/4] Adds tests --- src/ShapefileDataParser.spec.ts | 55 ++++++++++++++++++++++++++------- 1 file changed, 43 insertions(+), 12 deletions(-) diff --git a/src/ShapefileDataParser.spec.ts b/src/ShapefileDataParser.spec.ts index 52afd95..f73b0ba 100644 --- a/src/ShapefileDataParser.spec.ts +++ b/src/ShapefileDataParser.spec.ts @@ -69,24 +69,55 @@ beforeEach(() => { parser = new ShapefileDataParser(); }); -it('ShapefileDataParser is defined', () => { - expect(ShapefileDataParser).toBeDefined(); -}); +describe('ShapefileDataParser', () => { + + it('…is defined', () => { + expect(ShapefileDataParser).toBeDefined(); + }); -describe('ShapefileDataParser implements DataParser', () => { + describe('implements DataParser', () => { + + it('…readData is defined', () => { + expect(parser.readData).toBeDefined(); + }); - it('readData is defined', () => { - expect(parser.readData).toBeDefined(); }); -}); + describe('constructor', () => { -describe('readData implementation', () => { + it('…creates a GeoJsonDataParser with the passed projections', () => { + const parserWithProjection = new ShapefileDataParser('EPSG:3857', 'EPSG:4326'); + expect(parserWithProjection._geoJsonParser.sourceProjection).toBe('EPSG:3857'); + expect(parserWithProjection._geoJsonParser.targetProjection).toBe('EPSG:4326'); + }); - it('can read a Buffer', async () => { - const buffer = readFileSync(path.resolve(__dirname, '../data/point.zip')); - const data = await parser.readData(buffer); - expect(data).toEqual(expectedData); }); + describe('readData implementation', () => { + + it('…can read a Buffer', async () => { + const buffer = readFileSync(path.resolve(__dirname, '../data/point.zip')); + const data = await parser.readData(buffer); + expect(data).toEqual(expectedData); + }); + + it('…rejects the promise if called with invalid (Array)Buffer', (done) => { + expect.assertions(1); + const buffer = new ArrayBuffer(0); + parser.readData(buffer) + .catch((e) => { + expect(e).toBeDefined(); + }).finally(done); + }); + + it('…rejects the promise if called with invalid Argument', (done) => { + expect.assertions(1); + const buffer = undefined as unknown as ArrayBuffer; + parser.readData(buffer) + .catch((e) => { + expect(e).toBeDefined(); + }).finally(done); + }); + + }); });