-
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(isValidCoordinate): add new matcher function
This combines the functionality of `isValid2DCoordinate` and `isValid3DCoordinate`. Tests for this matcher are included with tests for the individual 2D and 3D matchers. Resolves: #4
- Loading branch information
1 parent
ea42aed
commit d7e5b70
Showing
9 changed files
with
253 additions
and
79 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
const { valid2DCoordinate } = require('./valid2DCoordinate') | ||
const { valid3DCoordinate } = require('./valid3DCoordinate') | ||
|
||
/** | ||
* A helper function used to verify a coordinate has appropriate longitude, latitude, and altitude values. | ||
* | ||
* @memberof Core.Coordinates | ||
* @param {GeoJSON2DCoordinate|GeoJSON3DCoordinate} coordinate A WGS-84 array of [longitude, latitude] or [longitude, latitude, alititude] | ||
* @returns {boolean} True if a valid 3D GeoJSON coordinate. If invalid, it will throw an error. | ||
// * @throws {Error} Input must be an array of only three elments | ||
// * @throws {Error} Altitude value must be numeric | ||
*/ | ||
function validCoordinate(coordinate) { | ||
if (!Array.isArray(coordinate) || coordinate.length < 2 || coordinate.length > 3) { | ||
throw new Error('Input must be an array of either two or three elments.') | ||
} | ||
|
||
if (coordinate.length === 2) { | ||
valid2DCoordinate(coordinate) | ||
} | ||
|
||
if (coordinate.length === 3) { | ||
valid3DCoordinate(coordinate) | ||
} | ||
|
||
return true | ||
} | ||
|
||
exports.validCoordinate = validCoordinate |
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,62 @@ | ||
const { validCoordinate } = require('../../core/coordinates/validCoordinate') | ||
|
||
// eslint-disable-next-line jsdoc/require-returns | ||
/** | ||
* Verifies a two or three element coordinate meets WGS-84 and GeoJSON validity requirements. | ||
* | ||
* @memberof Coordinates | ||
* @param {number[]} coordinateArray A two or three element array of numbers in format | ||
* [longitude, latitude] or [longitude, latitude, altitude]. | ||
* | ||
* Longitude must be between -180 to 180. | ||
* Latitude must be between -90 to 90. | ||
* Altitude must be a number between -Infinity to Infinity. | ||
* The standard does not specify units altitude represents (i.e. meters, feet, etc.). | ||
* @example | ||
* expect([22, 45.733]).isValidCoordinate() | ||
* expect([180, 90]).isValidCoordinate() | ||
* expect([22, 45.733, 20]).isValidCoordinate() | ||
* expect([180, 90, -10000]).isValidCoordinate() | ||
* @example | ||
* expect([220, 56]).not.isValidCoordinate() // Longitude out of range | ||
* expect([22, 45.733, '0']).not.isValidCoordinate() | ||
* // Nested Arrays | ||
* expect([[22, 45.733, 0]]).not.isValidCoordinate() | ||
* expect([[22, 45.733, 0], [180, 90, 0]]).not.isValidCoordinate() | ||
*/ | ||
function isValidCoordinate(coordinateArray) { | ||
const { printReceived, matcherHint } = this.utils | ||
const passMessage = | ||
// eslint-disable-next-line prefer-template | ||
matcherHint('.not.isValidCoordinate', '[longitude, latitude, (altitude)]', '') + | ||
'\n\n' + | ||
`Expected input to not be a two or three element array with longitude between (-90 to 90), | ||
latitude between (-180 to 180), and (if a 3D coordinate) numeric altitude.\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('.isValidCoordinate', '[longitude, latitude, (altitude)]', '') + | ||
'\n\n' + | ||
`${errorMessage}\n\n` + | ||
`Received: ${printReceived(coordinateArray)}` | ||
) | ||
} | ||
|
||
try { | ||
validCoordinate(coordinateArray) | ||
} catch (err) { | ||
return { pass: false, message: () => failMessage(err.message) } | ||
} | ||
return { pass: true, message: () => passMessage } | ||
} | ||
|
||
exports.isValidCoordinate = isValidCoordinate |
35 changes: 35 additions & 0 deletions
35
tests/coordinates/__snapshots__/isValidCoordinate.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,35 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`Error Snapshot Testing. Throws error: expect([0, 0, 0]).isValidCoordinate 1`] = ` | ||
"[2mexpect([22m[31m[longitude, latitude, (altitude)][39m[2m).isValidCoordinate()[22m | ||
Latitude must be a number between -90 and 90. | ||
Received: [31m[0, 95, 0][39m" | ||
`; | ||
|
||
exports[`Error Snapshot Testing. Throws error: expect([0, 0, 0]).not.isValidCoordinate 1`] = ` | ||
"[2mexpect([22m[31m[longitude, latitude, (altitude)][39m[2m).not.isValidCoordinate()[22m | ||
Expected input to not be a two or three element array with longitude between (-90 to 90), | ||
latitude between (-180 to 180), and (if a 3D coordinate) numeric altitude. | ||
Received: [31m[0, 0, 0][39m" | ||
`; | ||
|
||
exports[`Error Snapshot Testing. Throws error: expect([0, 0]).isValidCoordinate 1`] = ` | ||
"[2mexpect([22m[31m[longitude, latitude, (altitude)][39m[2m).isValidCoordinate()[22m | ||
Latitude must be a number between -90 and 90. | ||
Received: [31m[0, 95][39m" | ||
`; | ||
|
||
exports[`Error Snapshot Testing. Throws error: expect([0, 0]).not.isValidCoordinate 1`] = ` | ||
"[2mexpect([22m[31m[longitude, latitude, (altitude)][39m[2m).not.isValidCoordinate()[22m | ||
Expected input to not be a two or three element array with longitude between (-90 to 90), | ||
latitude between (-180 to 180), and (if a 3D coordinate) numeric altitude. | ||
Received: [31m[0, 0][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
Oops, something went wrong.