Skip to content

Commit

Permalink
refactor: split the core functionality into its own subfolder under src
Browse files Browse the repository at this point in the history
  • Loading branch information
M-Scott-Lassiter committed May 24, 2022
1 parent c0becfb commit ea42aed
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 69 deletions.
12 changes: 11 additions & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
39 changes: 39 additions & 0 deletions src/core/coordinates/valid2DCoordinate.js
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
30 changes: 30 additions & 0 deletions src/core/coordinates/valid3DCoordinate.js
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
41 changes: 2 additions & 39 deletions src/matchers/coordinates/isValid2DCoordinate.js
Original file line number Diff line number Diff line change
@@ -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
/**
Expand Down Expand Up @@ -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
31 changes: 2 additions & 29 deletions src/matchers/coordinates/isValid3DCoordinate.js
Original file line number Diff line number Diff line change
@@ -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
/**
Expand Down Expand Up @@ -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

0 comments on commit ea42aed

Please sign in to comment.