From d17c7ad31e16a7ab395adf33ccffe67b13eee8d2 Mon Sep 17 00:00:00 2001 From: Andreas Hocevar Date: Thu, 2 Sep 2021 09:44:33 +0200 Subject: [PATCH] Fix return stride of forEachSegment --- src/ol/geom/flat/segments.js | 14 ++++++-------- test/node/ol/geom/flat/segments.test.js | 13 +++++++++++++ 2 files changed, 19 insertions(+), 8 deletions(-) diff --git a/src/ol/geom/flat/segments.js b/src/ol/geom/flat/segments.js index 4137ce0bc1..5b07bc63df 100644 --- a/src/ol/geom/flat/segments.js +++ b/src/ol/geom/flat/segments.js @@ -16,18 +16,16 @@ * @template T */ export function forEach(flatCoordinates, offset, end, stride, callback) { - const point1 = [flatCoordinates[offset], flatCoordinates[offset + 1]]; - const point2 = []; let ret; - for (; offset + stride < end; offset += stride) { - point2[0] = flatCoordinates[offset + stride]; - point2[1] = flatCoordinates[offset + stride + 1]; - ret = callback(point1, point2); + offset += stride; + for (; offset < end; offset += stride) { + ret = callback( + flatCoordinates.slice(offset - stride, offset), + flatCoordinates.slice(offset, offset + stride) + ); if (ret) { return ret; } - point1[0] = point2[0]; - point1[1] = point2[1]; } return false; } diff --git a/test/node/ol/geom/flat/segments.test.js b/test/node/ol/geom/flat/segments.test.js index 0789199080..cc4ad1c478 100644 --- a/test/node/ol/geom/flat/segments.test.js +++ b/test/node/ol/geom/flat/segments.test.js @@ -50,5 +50,18 @@ describe('ol/geom/flat/segments.js', function () { expect(ret).to.be(true); }); }); + it('returns coordinates with the correct stride', function () { + const spy = sinon.spy(); + forEachSegment([0, 0, 0, 1, 1, 1, 2, 2, 2], 0, 9, 3, spy); + expect(spy.callCount).to.be(2); + expect(spy.firstCall.args).to.eql([ + [0, 0, 0], + [1, 1, 1], + ]); + expect(spy.secondCall.args).to.eql([ + [1, 1, 1], + [2, 2, 2], + ]); + }); }); });