all files / mapbox-gl-draw/src/lib/ move_features.js

100% Statements 27/27
91.67% Branches 11/12
100% Functions 8/8
100% Lines 20/20
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34   63×   35× 63×   63× 211×       211×   127× 63×     63× 14× 49× 24× 25× 23×     63×      
const constrainFeatureMovement = require('./constrain_feature_movement');
const Constants = require('../constants');
 
module.exports = function(features, delta) {
  const constrainedDelta = constrainFeatureMovement(features.map(feature => feature.toGeoJSON()), delta);
 
  features.forEach(feature => {
    const currentCoordinates = feature.getCoordinates();
 
    const moveCoordinate = (coord) => {
      const point = {
        lng: coord[0] + constrainedDelta.lng,
        lat: coord[1] + constrainedDelta.lat
      };
      return [point.lng, point.lat];
    };
    const moveRing = (ring) => ring.map(coord => moveCoordinate(coord));
    const moveMultiPolygon = (multi) => multi.map(ring => moveRing(ring));
 
    let nextCoordinates;
    if (feature.type === Constants.geojsonTypes.POINT) {
      nextCoordinates = moveCoordinate(currentCoordinates);
    } else if (feature.type === Constants.geojsonTypes.LINE_STRING || feature.type === Constants.geojsonTypes.MULTI_POINT) {
      nextCoordinates = currentCoordinates.map(moveCoordinate);
    } else if (feature.type === Constants.geojsonTypes.POLYGON || feature.type === Constants.geojsonTypes.MULTI_LINE_STRING) {
      nextCoordinates = currentCoordinates.map(moveRing);
    } else if (feature.type === Constants.geojsonTypes.MULTI_POLYGON) E{
      nextCoordinates = currentCoordinates.map(moveMultiPolygon);
    }
 
    feature.incomingCoords(nextCoordinates);
  });
};