From b8d3bb57667581e9e1e7362150c28be67aa28c1c Mon Sep 17 00:00:00 2001 From: Ilia Choly Date: Tue, 17 Apr 2018 14:01:55 -0400 Subject: [PATCH] Add hasFeature method to ol.source.Vector --- src/ol/source/Vector.js | 15 +++++++++++++++ test/spec/ol/source/vector.test.js | 29 +++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+) diff --git a/src/ol/source/Vector.js b/src/ol/source/Vector.js index c389eb5a00..2d11517d6a 100644 --- a/src/ol/source/Vector.js +++ b/src/ol/source/Vector.js @@ -827,6 +827,21 @@ VectorSource.prototype.handleFeatureChange_ = function(event) { VectorEventType.CHANGEFEATURE, feature)); }; +/** + * Returns true if the feature is contained within the source. + * @param {ol.Feature} feature Feature. + * @return {boolean} Has feature. + * @api + */ +VectorSource.prototype.hasFeature = function(feature) { + const id = feature.getId(); + if (id !== undefined) { + return id in this.idIndex_; + } else { + const featureKey = getUid(feature).toString(); + return featureKey in this.undefIdIndex_; + } +}; /** * @return {boolean} Is empty. diff --git a/test/spec/ol/source/vector.test.js b/test/spec/ol/source/vector.test.js index 1886ea6a56..68a0866810 100644 --- a/test/spec/ol/source/vector.test.js +++ b/test/spec/ol/source/vector.test.js @@ -85,6 +85,35 @@ describe('ol.source.Vector', function() { }); + describe('#hasFeature', function() { + + it('returns true for added feature without id', function() { + const feature = new Feature(); + vectorSource.addFeature(feature); + expect(vectorSource.hasFeature(feature)).to.be(true); + }); + + it('returns true for added feature with id', function() { + const feature = new Feature(); + feature.setId('1'); + vectorSource.addFeature(feature); + expect(vectorSource.hasFeature(feature)).to.be(true); + }); + + it('return false for removed feature', function() { + const feature = new Feature(); + vectorSource.addFeature(feature); + vectorSource.removeFeature(feature); + expect(vectorSource.hasFeature(feature)).to.be(false); + }); + + it('returns false for non-added feature', function() { + const feature = new Feature(); + expect(vectorSource.hasFeature(feature)).to.be(false); + }); + + }); + }); describe('when populated with 3 features', function() {