Skip to content

Commit

Permalink
Turf.invariant fails on string value as coordinate (#643)
Browse files Browse the repository at this point in the history
* Add Array check for recursion and assoc. tests

* Add containsNumber to core turf functions

* Update index.d.ts
  • Loading branch information
ingalls authored and DenisCarriere committed Apr 3, 2017
1 parent 5885d3c commit fe1f2c4
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 2 deletions.
4 changes: 3 additions & 1 deletion packages/turf-invariant/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ function containsNumber(coordinates) {
typeof coordinates[1] === 'number') {
return true;
}
if (coordinates[0].length) {

if (Array.isArray(coordinates[0]) && coordinates[0].length) {
return containsNumber(coordinates[0]);
}
throw new Error('coordinates must only contain numbers');
Expand Down Expand Up @@ -138,3 +139,4 @@ module.exports.collectionOf = collectionOf;
module.exports.featureOf = featureOf;
module.exports.getCoord = getCoord;
module.exports.getCoords = getCoords;
module.exports.containsNumber = containsNumber;
12 changes: 12 additions & 0 deletions packages/turf-invariant/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,18 @@ const test = require('tape');
const {point, lineString, polygon} = require('@turf/helpers');
const invariant = require('./');

test('invariant#containsNumber', t => {
t.equals(invariant.containsNumber([1, 1]), true);
t.equals(invariant.containsNumber([[1, 1], [1, 1]]), true);
t.equals(invariant.containsNumber([[[1,1], [1,1]], [1, 1]]), true);

//# Ensure recusive call handles Max callstack exceeded
t.throws(() => {
invariant.containsNumber(['1', 1]);
}, /coordinates must only contain numbers/, 'Must only contain numbers');
t.end();
});

test('invariant#geojsonType', t => {
t.throws(() => {
invariant.geojsonType();
Expand Down
4 changes: 3 additions & 1 deletion packages/turf/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ import {
getCoord,
geojsonType,
featureOf,
collectionOf
collectionOf,
containsNumber
} from '@turf/invariant';
import {
coordEach,
Expand Down Expand Up @@ -146,6 +147,7 @@ export {
geojsonType,
featureOf,
collectionOf,
containsNumber,
truncate,
flatten,
coordEach,
Expand Down
1 change: 1 addition & 0 deletions packages/turf/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ var turf = {
geojsonType: invariant.geojsonType,
featureOf: invariant.featureOf,
collectionOf: invariant.collectionOf,
containsNumber: invariant.containsNumber,
coordEach: meta.coordEach,
coordReduce: meta.coordReduce,
propEach: meta.propEach,
Expand Down

0 comments on commit fe1f2c4

Please sign in to comment.