-
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.
refactor: split the core functionality into its own subfolder under src
- Loading branch information
1 parent
c0becfb
commit ea42aed
Showing
5 changed files
with
84 additions
and
69 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/** | ||
* A helper function used to verify a coordinate has appropriate longitude and latitude values. | ||
* | ||
* @memberof Core.Coordinates | ||
* @param {GeoJSON-Coordinate} coordinate A WGS-84 array of [longitude, latitude] | ||
* @returns {boolean} True if a valid 2D GeoJSON coordinate. If invalid, it will throw an error. | ||
* @throws {Error} Input must be an array of only two elments | ||
* @throws {RangeError} Longitude must be a number between -180 and 180 | ||
* @throws {RangeError} Latitude must be a number between -90 and 90 | ||
*/ | ||
function valid2DCoordinate(coordinate) { | ||
if (!Array.isArray(coordinate) || coordinate.length !== 2) { | ||
throw new Error('Input must be an array of only two elments.') | ||
} | ||
|
||
if ( | ||
typeof coordinate[0] !== 'number' || | ||
coordinate[0] < -180 || | ||
coordinate[0] > 180 || | ||
// eslint-disable-next-line no-self-compare | ||
coordinate[0] !== coordinate[0] // Accounts for NaN | ||
) { | ||
throw new Error('Longitude must be a number between -180 and 180.') | ||
} | ||
|
||
if ( | ||
typeof coordinate[1] !== 'number' || | ||
coordinate[1] < -90 || | ||
coordinate[1] > 90 || | ||
// eslint-disable-next-line no-self-compare | ||
coordinate[1] !== coordinate[1] // Accounts for NaN | ||
) { | ||
throw new Error('Latitude must be a number between -90 and 90.') | ||
} | ||
|
||
return true | ||
} | ||
|
||
exports.valid2DCoordinate = valid2DCoordinate |
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,30 @@ | ||
const { valid2DCoordinate } = require('./valid2DCoordinate') | ||
|
||
/** | ||
* A helper function used to verify a coordinate has appropriate longitude, latitude, and altitude values. | ||
* | ||
* @memberof Core.Coordinates | ||
* @param {GeoJSON-Coordinate} coordinate A WGS-84 array of [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 valid3DCoordinate(coordinate) { | ||
if (!Array.isArray(coordinate) || coordinate.length !== 3) { | ||
throw new Error('Input must be an array of only three elments.') | ||
} | ||
|
||
// The first two elements have to match the same validity requirements as a 2D coordinate. | ||
// Reuse the logic from that function. | ||
valid2DCoordinate([coordinate[0], coordinate[1]]) | ||
|
||
// eslint-disable-next-line no-self-compare | ||
if (typeof coordinate[2] !== 'number' || coordinate[2] !== coordinate[2]) { | ||
// Self compare accounts for NaN | ||
throw new Error('Altitude value must be numeric.') | ||
} | ||
|
||
return true | ||
} | ||
|
||
exports.valid3DCoordinate = valid3DCoordinate |
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