Skip to content

Commit

Permalink
forgot to commit throw tests;
Browse files Browse the repository at this point in the history
replaced geomType function with invariant.getGeomType;
  • Loading branch information
stebogit committed Jul 21, 2017
1 parent af7f88b commit e0ffac5
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 16 deletions.
24 changes: 8 additions & 16 deletions packages/turf-line-split/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@ var rbush = require('geojson-rbush');
var helpers = require('@turf/helpers');
var flatten = require('@turf/flatten');
var truncate = require('@turf/truncate');
var getCoords = require('@turf/invariant').getCoords;
var invariant = require('@turf/invariant');
var pointOnLine = require('@turf/point-on-line');
var lineSegment = require('@turf/line-segment');
var lineIntersect = require('@turf/line-intersect');
var featureCollection = helpers.featureCollection;
var getCoords = invariant.getCoords;
var lineString = helpers.lineString;
var getGeomType = invariant.getGeomType;
var featureEach = meta.featureEach;
var featureReduce = meta.featureReduce;
var featureCollection = helpers.featureCollection;

/**
* Split a LineString by another GeoJSON Feature.
Expand Down Expand Up @@ -42,14 +44,15 @@ var featureReduce = meta.featureReduce;
* var addToMap = [line, splitter]
*/
module.exports = function (line, splitter) {
if (geomType(line) !== 'LineString') throw new Error('<line> must be LineString');
if (geomType(splitter) === 'FeatureCollection') throw new Error('<splitter> cannot be a FeatureCollection');
if (getGeomType(line) !== 'LineString') throw new Error('<line> must be LineString');
var splitterType = getGeomType(splitter);
if (splitterType === 'FeatureCollection') throw new Error('<splitter> cannot be a FeatureCollection');

// remove excessive decimals from splitter
// to avoid possible approximation issues in rbush
truncate(splitter, 6, 3, true);

switch (geomType(splitter)) {
switch (splitterType) {
case 'Point':
return splitLineWithPoint(line, splitter);
case 'MultiPoint':
Expand All @@ -64,17 +67,6 @@ module.exports = function (line, splitter) {
}
};

/**
* Retrieves Geometry Type from GeoJSON
*
* @private
* @param {Feature<any>} geojson Feature
* @returns {string} Geometry Type
*/
function geomType(geojson) {
return (geojson.geometry) ? geojson.geometry.type : geojson.type;
}

/**
* Split LineString with MultiPoint
*
Expand Down
13 changes: 13 additions & 0 deletions packages/turf-line-split/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,19 @@ test('turf-line-split - splitter exactly on end of line', t => {
t.end();
});

test('turf-line-split -- throws', t => {
const pt = point([9, 50]);
const line = lineString([[7, 50], [8, 50], [9, 50]]);

t.throws(() => lineSplit(null, pt), '<geojson> is required');
t.throws(() => lineSplit(line, null), '<geojson> is required');
t.throws(() => lineSplit(pt, pt), '<line> must be LineString');
t.throws(() => lineSplit(line, featureCollection([pt, line])), '<splitter> cannot be a FeatureCollection');

t.end();
});


/**
* Colorize FeatureCollection
*
Expand Down

0 comments on commit e0ffac5

Please sign in to comment.