From 288fd0b74c37477b6addfd7ec01eeedcd4623f38 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maximilian=20Kr=C3=B6g?= Date: Sat, 19 Dec 2020 17:58:56 +0100 Subject: [PATCH] Improve Draw type to mode conversion Throw error if Draw is configured with an invalid type. --- src/ol/interaction/Draw.js | 30 +++++++++++++-------------- test/spec/ol/interaction/draw.test.js | 2 +- 2 files changed, 15 insertions(+), 17 deletions(-) diff --git a/src/ol/interaction/Draw.js b/src/ol/interaction/Draw.js index b547efa42f..0787e56212 100644 --- a/src/ol/interaction/Draw.js +++ b/src/ol/interaction/Draw.js @@ -1227,23 +1227,21 @@ export function createBox() { * @return {Mode} Drawing mode. */ function getMode(type) { - let mode; - if (type === GeometryType.POINT || type === GeometryType.MULTI_POINT) { - mode = Mode.POINT; - } else if ( - type === GeometryType.LINE_STRING || - type === GeometryType.MULTI_LINE_STRING - ) { - mode = Mode.LINE_STRING; - } else if ( - type === GeometryType.POLYGON || - type === GeometryType.MULTI_POLYGON - ) { - mode = Mode.POLYGON; - } else if (type === GeometryType.CIRCLE) { - mode = Mode.CIRCLE; + switch (type) { + case GeometryType.POINT: + case GeometryType.MULTI_POINT: + return Mode.POINT; + case GeometryType.LINE_STRING: + case GeometryType.MULTI_LINE_STRING: + return Mode.LINE_STRING; + case GeometryType.POLYGON: + case GeometryType.MULTI_POLYGON: + return Mode.POLYGON; + case GeometryType.CIRCLE: + return Mode.CIRCLE; + default: + throw new Error('Invalid type: ' + type); } - return /** @type {!Mode} */ (mode); } export default Draw; diff --git a/test/spec/ol/interaction/draw.test.js b/test/spec/ol/interaction/draw.test.js index 0aad9df692..9db0498682 100644 --- a/test/spec/ol/interaction/draw.test.js +++ b/test/spec/ol/interaction/draw.test.js @@ -1381,7 +1381,7 @@ describe('ol.interaction.Draw', function () { describe('#getOverlay', function () { it('returns the feature overlay layer', function () { - const draw = new Draw({}); + const draw = new Draw({type: 'Point'}); expect(draw.getOverlay()).to.eql(draw.overlay_); }); });