Skip to content

Commit

Permalink
fix comma-separated bbox value parsing (#160)
Browse files Browse the repository at this point in the history
  • Loading branch information
Phil Varner authored Feb 18, 2022
1 parent f9d11c9 commit d114c48
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 3 deletions.
7 changes: 4 additions & 3 deletions libs/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ const extractBbox = function (params) {
if (typeof bbox === 'string') {
try {
bboxArray = JSON.parse(bbox)
} catch(e) {
bboxArray = bbox.split(',')
} catch (e) {
bboxArray = bbox.split(',').map(parseFloat)
}
} else {
bboxArray = bbox
Expand Down Expand Up @@ -629,5 +629,6 @@ module.exports = {
searchItems,
API,
parsePath,
extractIntersects
extractIntersects,
extractBbox
}
30 changes: 30 additions & 0 deletions tests/test_api_bbox.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
const test = require('ava')
const api = require('../libs/api')

test('extractBboxNull', (t) => {
const params = {}
const intersectsGeometry = api.extractBbox(params)
t.falsy(intersectsGeometry,
'Returns undefined when no bbox parameter')
})

test('extractBbox JSON array', (t) => {
const params = { bbox: [0, 0, 1, 1] }
const intersectsGeometry = api.extractBbox(params)
t.is(intersectsGeometry.coordinates[0].length, 5)
t.is(intersectsGeometry.coordinates[0][0][0], 0)
})

test('extractBbox comma-separated value', (t) => {
const params = { bbox: '0,0,1,1' }
const intersectsGeometry = api.extractBbox(params)
t.is(intersectsGeometry.coordinates[0].length, 5)
t.is(intersectsGeometry.coordinates[0][0][0], 0)
})

test('extractBbox comma-separated value with whitespace', (t) => {
const params = { bbox: '0 , 0.3 , 1.0,\t1' }
const intersectsGeometry = api.extractBbox(params)
t.is(intersectsGeometry.coordinates[0].length, 5)
t.is(intersectsGeometry.coordinates[0][0][0], 0)
})

0 comments on commit d114c48

Please sign in to comment.