Skip to content

Commit

Permalink
Add option to return false instead of throwing for unsupported geomet…
Browse files Browse the repository at this point in the history
…ries.
  • Loading branch information
dpmcmlxxvi committed May 19, 2019
1 parent 5e1df4e commit 8f1fdea
Show file tree
Hide file tree
Showing 91 changed files with 89 additions and 38 deletions.
2 changes: 1 addition & 1 deletion lib/de9im.esm.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion lib/de9im.esm.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion lib/de9im.umd.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion lib/de9im.umd.js.map

Large diffs are not rendered by default.

9 changes: 7 additions & 2 deletions src/contains/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ import within from '../within';
* @description Check if geojson #1 contains geojson #2.
* @param {GeoJSON} geojson1 GeoJSON #1.
* @param {GeoJSON} geojson2 GeoJSON #2.
* @param {Boolean} [error=true] If true unsupported geometries throw an
* error, otherwise they return false.
* @return {Boolean} True if contained otherwise false.
*/
const contains = (geojson1, geojson2) => {
const contains = (geojson1, geojson2, error=true) => {
const type1 = util.invariant.type(geojson1);
const type2 = util.invariant.type(geojson2);
const type = `${type1}-${type2}`;
Expand All @@ -20,7 +22,10 @@ const contains = (geojson1, geojson2) => {
case 'Polygon-Polygon':
return within(geojson2, geojson1);
default:
throw new Error(`${type1} contains ${type2} not supported.`);
if (error) {
throw new Error(`${type1} contains ${type2} not supported.`);
}
return false;
}
};

Expand Down
9 changes: 7 additions & 2 deletions src/coveredby/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ import util from '../util';
* @description Check if geojson #1 is coveredby geojson #2.
* @param {GeoJSON} geojson1 GeoJSON #1.
* @param {GeoJSON} geojson2 GeoJSON #2.
* @param {Boolean} [error=true] If true unsupported geometries throw an
* error, otherwise they return false.
* @return {Boolean} True if coveredby otherwise false.
*/
const coveredby = (geojson1, geojson2) => {
const coveredby = (geojson1, geojson2, error=true) => {
const type1 = util.invariant.type(geojson1);
const type2 = util.invariant.type(geojson2);
const type = `${type1}-${type2}`;
Expand All @@ -27,7 +29,10 @@ const coveredby = (geojson1, geojson2) => {
case 'Polygon-Polygon':
return polygon.coveredbyPolygon(geojson1, geojson2);
default:
throw new Error(`${type1} coveredby ${type2} not supported.`);
if (error) {
throw new Error(`${type1} coveredby ${type2} not supported.`);
}
return false;
}
};

Expand Down
9 changes: 7 additions & 2 deletions src/covers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ import util from '../util';
* @description Check if geojson #1 covers geojson #2.
* @param {GeoJSON} geojson1 GeoJSON #1.
* @param {GeoJSON} geojson2 GeoJSON #2.
* @param {Boolean} [error=true] If true unsupported geometries throw an
* error, otherwise they return false.
* @return {Boolean} True if covers otherwise false.
*/
const covers = (geojson1, geojson2) => {
const covers = (geojson1, geojson2, error=true) => {
const type1 = util.invariant.type(geojson1);
const type2 = util.invariant.type(geojson2);
const type = `${type1}-${type2}`;
Expand All @@ -20,7 +22,10 @@ const covers = (geojson1, geojson2) => {
case 'Polygon-Polygon':
return coveredby(geojson2, geojson1);
default:
throw new Error(`${type1} covers ${type2} not supported.`);
if (error) {
throw new Error(`${type1} covers ${type2} not supported.`);
}
return false;
}
};

Expand Down
9 changes: 7 additions & 2 deletions src/crosses/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ import util from '../util';
* @description Check if geojson #1 crosses geojson #2.
* @param {GeoJSON} geojson1 GeoJSON #1.
* @param {GeoJSON} geojson2 GeoJSON #2.
* @param {Boolean} [error=true] If true unsupported geometries throw an
* error, otherwise they return false.
* @return {Boolean} True if crosses otherwise false.
*/
const crosses = (geojson1, geojson2) => {
const crosses = (geojson1, geojson2, error=true) => {
const type1 = util.invariant.type(geojson1);
const type2 = util.invariant.type(geojson2);
const type = `${type1}-${type2}`;
Expand All @@ -29,7 +31,10 @@ const crosses = (geojson1, geojson2) => {
case 'Polygon-Point':
return polygon.crossesPoint(geojson1, geojson2);
default:
throw new Error(`${type1} crosses ${type2} not supported.`);
if (error) {
throw new Error(`${type1} crosses ${type2} not supported.`);
}
return false;
}
};

Expand Down
9 changes: 7 additions & 2 deletions src/disjoint/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ import util from '../util';
* @description Check if geojson #1 is disjoint geojson #2.
* @param {GeoJSON} geojson1 GeoJSON #1.
* @param {GeoJSON} geojson2 GeoJSON #2.
* @param {Boolean} [error=true] If true unsupported geometries throw an
* error, otherwise they return false.
* @return {Boolean} True if disjoint otherwise false.
*/
const disjoint = (geojson1, geojson2) => {
const disjoint = (geojson1, geojson2, error=true) => {
const type1 = util.invariant.type(geojson1);
const type2 = util.invariant.type(geojson2);
const type = `${type1}-${type2}`;
Expand All @@ -23,7 +25,10 @@ const disjoint = (geojson1, geojson2) => {
case 'Polygon-Polygon':
return intersects(geojson1, geojson2) === false;
default:
throw new Error(`${type1} disjoint ${type2} not supported.`);
if (error) {
throw new Error(`${type1} disjoint ${type2} not supported.`);
}
return false;
}
};

Expand Down
9 changes: 7 additions & 2 deletions src/equals/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ import within from '../within';
* @description Check if geojson #1 equals geojson #2.
* @param {GeoJSON} geojson1 GeoJSON #1.
* @param {GeoJSON} geojson2 GeoJSON #2.
* @param {Boolean} [error=true] If true unsupported geometries throw an
* error, otherwise they return false.
* @return {Boolean} True if equals otherwise false.
*/
const equals = (geojson1, geojson2) => {
const equals = (geojson1, geojson2, error=true) => {
const type1 = util.invariant.type(geojson1);
const type2 = util.invariant.type(geojson2);
const type = `${type1}-${type2}`;
Expand All @@ -18,7 +20,10 @@ const equals = (geojson1, geojson2) => {
case 'Polygon-Polygon':
return within(geojson1, geojson2) && contains(geojson1, geojson2);
default:
throw new Error(`${type1} equals ${type2} not supported.`);
if (error) {
throw new Error(`${type1} equals ${type2} not supported.`);
}
return false;
}
};

Expand Down
9 changes: 7 additions & 2 deletions src/intersects/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ import util from '../util';
* @description Check if geojson #1 intersects geojson #2.
* @param {GeoJSON} geojson1 GeoJSON #1.
* @param {GeoJSON} geojson2 GeoJSON #2.
* @param {Boolean} [error=true] If true unsupported geometries throw an
* error, otherwise they return false.
* @return {Boolean} True if intersects otherwise false.
*/
const intersects = (geojson1, geojson2) => {
const intersects = (geojson1, geojson2, error=true) => {
const type1 = util.invariant.type(geojson1);
const type2 = util.invariant.type(geojson2);
const type = `${type1}-${type2}`;
Expand All @@ -33,7 +35,10 @@ const intersects = (geojson1, geojson2) => {
case 'Polygon-Polygon':
return polygon.intersectsPolygon(geojson1, geojson2);
default:
throw new Error(`${type1} intersects ${type2} not supported.`);
if (error) {
throw new Error(`${type1} intersects ${type2} not supported.`);
}
return false;
}
};

Expand Down
9 changes: 7 additions & 2 deletions src/overlaps/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ import util from '../util';
* @description Check if geojson #1 overlaps geojson #2.
* @param {GeoJSON} geojson1 GeoJSON #1.
* @param {GeoJSON} geojson2 GeoJSON #2.
* @param {Boolean} [error=true] If true unsupported geometries throw an
* error, otherwise they return false.
* @return {Boolean} True if overlaps otherwise false.
*/
const overlaps = (geojson1, geojson2) => {
const overlaps = (geojson1, geojson2, error=true) => {
const type1 = util.invariant.type(geojson1);
const type2 = util.invariant.type(geojson2);
const type = `${type1}-${type2}`;
Expand All @@ -21,7 +23,10 @@ const overlaps = (geojson1, geojson2) => {
case 'Polygon-Polygon':
return polygon.overlapsPolygon(geojson1, geojson2);
default:
throw new Error(`${type1} overlaps ${type2} not supported.`);
if (error) {
throw new Error(`${type1} overlaps ${type2} not supported.`);
}
return false;
}
};

Expand Down
9 changes: 7 additions & 2 deletions src/touches/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ import util from '../util';
* @description Check if geojson #1 touches geojson #2.
* @param {GeoJSON} geojson1 GeoJSON #1.
* @param {GeoJSON} geojson2 GeoJSON #2.
* @param {Boolean} [error=true] If true unsupported geometries throw an
* error, otherwise they return false.
* @return {Boolean} True if touches otherwise false.
*/
const touches = (geojson1, geojson2) => {
const touches = (geojson1, geojson2, error=true) => {
const type1 = util.invariant.type(geojson1);
const type2 = util.invariant.type(geojson2);
const type = `${type1}-${type2}`;
Expand All @@ -31,7 +33,10 @@ const touches = (geojson1, geojson2) => {
case 'Polygon-Polygon':
return polygon.touchesPolygon(geojson1, geojson2);
default:
throw new Error(`${type1} touches ${type2} not supported.`);
if (error) {
throw new Error(`${type1} touches ${type2} not supported.`);
}
return false;
}
};

Expand Down
9 changes: 7 additions & 2 deletions src/within/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ import util from '../util';
* @description Check if geojson #1 is within geojson #2.
* @param {GeoJSON} geojson1 GeoJSON #1.
* @param {GeoJSON} geojson2 GeoJSON #2.
* @param {Boolean} [error=true] If true unsupported geometries throw an
* error, otherwise they return false.
* @return {Boolean} True if within otherwise false.
*/
const within = (geojson1, geojson2) => {
const within = (geojson1, geojson2, error=true) => {
const type1 = util.invariant.type(geojson1);
const type2 = util.invariant.type(geojson2);
const type = `${type1}-${type2}`;
Expand All @@ -27,7 +29,10 @@ const within = (geojson1, geojson2) => {
case 'Polygon-Polygon':
return polygon.withinPolygon(geojson1, geojson2);
default:
throw new Error(`${type1} within ${type2} not supported.`);
if (error) {
throw new Error(`${type1} within ${type2} not supported.`);
}
return false;
}
};

Expand Down
29 changes: 15 additions & 14 deletions test/de9im.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ const ext = '*.geojson';
// Test each predicate function
Object.keys(de9im).forEach((predicate) => {
test('de9im-' + predicate, (t) => {
// Test true and false fixtures
['true', 'false'].forEach((type) => {
// Test true, false, and throws fixtures
['true', 'false', 'throws'].forEach((type) => {
const pattern = path.join(__dirname, 'data', 'de9im', predicate, type,
'**', ext);
glob.sync(pattern).forEach((filepath) => {
Expand All @@ -20,19 +20,20 @@ Object.keys(de9im).forEach((predicate) => {
const feature1 = geojson.features[0];
const feature2 = geojson.features[1];

let result = null;
try {
result = de9im[predicate](feature1, feature2);
} catch (e) {
if (e.message.includes('not supported')) {
result = false;
}
}

// Test for given true/false type.
// Test for given true/false/throws type.
const message = '[' + type + '] ' + name;
const ok = t[type];
ok(result, message);
switch (type) {
case 'true':
t.ok(de9im[predicate](feature1, feature2), message);
break;
case 'false':
t.notOk(de9im[predicate](feature1, feature2), message);
break;
case 'throws':
t.throws(() => de9im[predicate](feature1, feature2), message);
t.notOk(de9im[predicate](feature1, feature2, false), message);
break;
}
});
});
t.end();
Expand Down

0 comments on commit 8f1fdea

Please sign in to comment.