diff --git a/packages/turf-invariant/index.js b/packages/turf-invariant/index.js index 4693f8f7a8..06d9a7a061 100644 --- a/packages/turf-invariant/index.js +++ b/packages/turf-invariant/index.js @@ -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'); @@ -138,3 +139,4 @@ module.exports.collectionOf = collectionOf; module.exports.featureOf = featureOf; module.exports.getCoord = getCoord; module.exports.getCoords = getCoords; +module.exports.containsNumber = containsNumber; diff --git a/packages/turf-invariant/test.js b/packages/turf-invariant/test.js index 3f31f57370..9faa0c40d8 100644 --- a/packages/turf-invariant/test.js +++ b/packages/turf-invariant/test.js @@ -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(); diff --git a/packages/turf/index.d.ts b/packages/turf/index.d.ts index 20e7ebdae2..3b04989ec4 100644 --- a/packages/turf/index.d.ts +++ b/packages/turf/index.d.ts @@ -12,7 +12,8 @@ import { getCoord, geojsonType, featureOf, - collectionOf + collectionOf, + containsNumber } from '@turf/invariant'; import { coordEach, @@ -146,6 +147,7 @@ export { geojsonType, featureOf, collectionOf, + containsNumber, truncate, flatten, coordEach, diff --git a/packages/turf/index.js b/packages/turf/index.js index 09a37dcacd..6059f3e699 100644 --- a/packages/turf/index.js +++ b/packages/turf/index.js @@ -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,