diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 8dc708b..b6e37af 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -87,10 +87,20 @@ npm install ### Project Structure -All distribution files are contained in the `src` folder. The `tests` folder contains the scripts used to verify the matchers work as designed (using the matchers themselves!) +All distribution files are contained in the `src` folder. The `tests` folder contains the scripts used to verify the matchers work as designed (using the matchers themselves!). + +`core` contains the functions that `matchers` use. ``` ├── src +│ ├── core +│ │ ├── boundingBoxes +│ │ ├── coordinates +│ │ ├── features +│ │ ├── featureCollections +│ │ ├── functional +│ │ ├── geometries +│ │ └── geometryCollections │ ├── matchers │ │ ├── boundingBox │ │ ├── coordinate diff --git a/src/core/coordinates/valid2DCoordinate.js b/src/core/coordinates/valid2DCoordinate.js new file mode 100644 index 0000000..de20240 --- /dev/null +++ b/src/core/coordinates/valid2DCoordinate.js @@ -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 diff --git a/src/core/coordinates/valid3DCoordinate.js b/src/core/coordinates/valid3DCoordinate.js new file mode 100644 index 0000000..5f6b293 --- /dev/null +++ b/src/core/coordinates/valid3DCoordinate.js @@ -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 diff --git a/src/matchers/coordinates/isValid2DCoordinate.js b/src/matchers/coordinates/isValid2DCoordinate.js index ae66bc8..27a1f49 100644 --- a/src/matchers/coordinates/isValid2DCoordinate.js +++ b/src/matchers/coordinates/isValid2DCoordinate.js @@ -1,40 +1,4 @@ -/** - * 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 valid2D(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 -} +const { valid2DCoordinate } = require('../../core/coordinates/valid2DCoordinate') // eslint-disable-next-line jsdoc/require-returns /** @@ -81,12 +45,11 @@ function isValid2DCoordinate(coordinateArray) { } try { - valid2D(coordinateArray) + valid2DCoordinate(coordinateArray) } catch (err) { return { pass: false, message: () => failMessage(err.message) } } return { pass: true, message: () => passMessage } } -exports.valid2D = valid2D exports.isValid2DCoordinate = isValid2DCoordinate diff --git a/src/matchers/coordinates/isValid3DCoordinate.js b/src/matchers/coordinates/isValid3DCoordinate.js index 98ad05e..c08c09b 100644 --- a/src/matchers/coordinates/isValid3DCoordinate.js +++ b/src/matchers/coordinates/isValid3DCoordinate.js @@ -1,30 +1,4 @@ -const { valid2D } = require('./isValid2DCoordinate') -/** - * 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 valid3D(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. - valid2D([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 -} +const { valid3DCoordinate } = require('../../core/coordinates/valid3DCoordinate') // eslint-disable-next-line jsdoc/require-returns /** @@ -74,12 +48,11 @@ function isValid3DCoordinate(coordinateArray) { } try { - valid3D(coordinateArray) + valid3DCoordinate(coordinateArray) } catch (err) { return { pass: false, message: () => failMessage(err.message) } } return { pass: true, message: () => passMessage } } -exports.valid3D = valid3D exports.isValid3DCoordinate = isValid3DCoordinate