-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(isValidBoundingBox): add new matcher
Verifies a two or three dimensional bounding box meets WGS-84 and GeoJSON validity requirements. Resolves: #8
- Loading branch information
1 parent
70bd43e
commit 9a8b7ed
Showing
12 changed files
with
231 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
const { valid2DBoundingBox } = require('./valid2DBoundingBox') | ||
const { valid3DBoundingBox } = require('./valid3DBoundingBox') | ||
|
||
/** | ||
* Verifies either a two or three dimensional bounding box meets WGS-84 and GeoJSON validity requirements. | ||
* | ||
* @memberof Core.BoundingBoxes | ||
* @see https://github.com/M-Scott-Lassiter/jest-geojson/issues/8 | ||
* @param {GeoJSON2DBBox|GeoJSON3DBBox} bboxArray A WGS-84 array of [west, south, east, north] or [west, south, depth, east, north, altitude]. | ||
* | ||
* Longitude must be between -180 to 180. | ||
* Latitude must be between -90 to 90. | ||
* If present, depth and altitude must be a number between -Infinity to Infinity. | ||
* The standard does not specify units altitude represents (i.e. meters, feet, etc.). | ||
* @returns {boolean} True if a valid 2D or 3D GeoJSON bounding box. If invalid, it will throw an error. | ||
* @throws {Error} Input must be an array of only four or six elments | ||
*/ | ||
function validBoundingBox(bboxArray) { | ||
if (!Array.isArray(bboxArray)) { | ||
throw new Error('Input must be an array.') | ||
} | ||
|
||
if (bboxArray.length < 4 || bboxArray.length > 6 || bboxArray.length === 5) { | ||
throw new Error('Input must be an array of either four or six elments.') | ||
} | ||
|
||
if (bboxArray.length === 4) { | ||
valid2DBoundingBox(bboxArray) | ||
} | ||
|
||
if (bboxArray.length === 6) { | ||
valid3DBoundingBox(bboxArray) | ||
} | ||
|
||
return true | ||
} | ||
|
||
exports.validBoundingBox = validBoundingBox |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
const { validBoundingBox } = require('../../core/boundingBoxes/validBoundingBox') | ||
|
||
// eslint-disable-next-line jsdoc/require-returns | ||
/** | ||
* Verifies a two or three dimensional bounding box meets WGS-84 and GeoJSON validity requirements. | ||
* | ||
* @memberof BoundingBoxes | ||
* @see https://github.com/M-Scott-Lassiter/jest-geojson/issues/8 | ||
* @param {number[]} coordinateArray A WGS-84 array of [west, south, east, north] or | ||
* [west, south, depth, east, north, altitude]. | ||
* | ||
* Longitude must be between -180 to 180. | ||
* Latitude must be between -90 to 90. | ||
* If present, depth and altitude must be a number between -Infinity to Infinity. | ||
* The standard does not specify units altitude represents (i.e. meters, feet, etc.). | ||
* @example | ||
* expect([-20, 10, -10, 20]).isValidBoundingBox() | ||
* expect([170, -20, -170, 20]).isValidBoundingBox() | ||
* expect([-10, -20, -100, 20, 10, 0]).isValidBoundingBox() | ||
* expect([170, -20, -22.5, 20, -170, 12345.678]).isValidBoundingBox() | ||
* @example | ||
* expect([-180.01, -10, -160, 10]).not.isValidBoundingBox() // Longitude out of range | ||
* expect([-10, -10, "0", 10, 20, 0]).not.isValidBoundingBox() // Non-numeric value | ||
*/ | ||
function isValidBoundingBox(coordinateArray) { | ||
const { printReceived, matcherHint } = this.utils | ||
const passMessage = | ||
// eslint-disable-next-line prefer-template | ||
matcherHint( | ||
'.not.isValidBoundingBox', | ||
'[west, south, (depth), east, north, (altitude)]', | ||
'' | ||
) + | ||
'\n\n' + | ||
`Expected input to not be a four or six element bounding box array.\n\n` + | ||
`Received: ${printReceived(coordinateArray)}` | ||
|
||
/** | ||
* Combines a custom error message with built in Jest tools to provide a more descriptive error | ||
* meessage to the end user. | ||
* | ||
* @param {string} errorMessage Error message text to return to the user | ||
* @returns {string} Concatenated Jest test result string | ||
*/ | ||
function failMessage(errorMessage) { | ||
return ( | ||
// eslint-disable-next-line prefer-template, no-unused-expressions | ||
matcherHint( | ||
'.isValidBoundingBox', | ||
'[west, south, (depth), east, north, (altitude)]', | ||
'' | ||
) + | ||
'\n\n' + | ||
`${errorMessage}\n\n` + | ||
`Received: ${printReceived(coordinateArray)}` | ||
) | ||
} | ||
|
||
try { | ||
validBoundingBox(coordinateArray) | ||
} catch (err) { | ||
return { pass: false, message: () => failMessage(err.message) } | ||
} | ||
return { pass: true, message: () => passMessage } | ||
} | ||
|
||
exports.isValidBoundingBox = isValidBoundingBox |
33 changes: 33 additions & 0 deletions
33
tests/boundingBoxes/__snapshots__/isValidBoundingBox.test.js.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`Error Snapshot Testing. Throws error: expect([0, -95, 0, 0, 0, 0]).isValidBoundingBox 1`] = ` | ||
"[2mexpect([22m[31m[west, south, (depth), east, north, (altitude)][39m[2m).isValidBoundingBox()[22m | ||
Southern value must be a number between -90 and 90. | ||
Received: [31m[0, -95, 0, 0, 0, 0][39m" | ||
`; | ||
|
||
exports[`Error Snapshot Testing. Throws error: expect([0, 0, 0, 0, 0, 0]).not.isValidBoundingBox 1`] = ` | ||
"[2mexpect([22m[31m[west, south, (depth), east, north, (altitude)][39m[2m).not.isValidBoundingBox()[22m | ||
Expected input to not be a four or six element bounding box array. | ||
Received: [31m[0, 0, 0, 0, 0, 0][39m" | ||
`; | ||
|
||
exports[`Error Snapshot Testing. Throws error: expect([0, 0, 0, 0]).not.isValidBoundingBox 1`] = ` | ||
"[2mexpect([22m[31m[west, south, (depth), east, north, (altitude)][39m[2m).not.isValidBoundingBox()[22m | ||
Expected input to not be a four or six element bounding box array. | ||
Received: [31m[-20, 10, -10, 20][39m" | ||
`; | ||
|
||
exports[`Error Snapshot Testing. Throws error: expect([0, 0, 95, 0]).isValidBoundingBox 1`] = ` | ||
"[2mexpect([22m[31m[west, south, (depth), east, north, (altitude)][39m[2m).isValidBoundingBox()[22m | ||
Northern value must be a number between -90 and 90. | ||
Received: [31m[0, 0, 0, 95][39m" | ||
`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.