Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(toBeLineStringGeometry): add new matcher
Verifies an object is a valid GeoJSON LineString Geometry. Resolves: #11
- Loading branch information
1 parent
4db12b0
commit 54416a5
Showing
12 changed files
with
536 additions
and
15 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
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,83 @@ | ||
const { validCoordinate } = require('../coordinates/validCoordinate') | ||
|
||
/** | ||
* Verifies an object is a valid GeoJSON LineString Geometry. This geometry requires a | ||
* 'type' property that must equal "LineString", and a 'coordinates' property that contains | ||
* an array of valid WGS-84 GeoJSON coordinate(s). The coordinates may be an empty array, | ||
* but may not be an array of empty arrays. | ||
* | ||
* Foreign members are allowed with the exceptions thrown below. | ||
* | ||
* @memberof Core.Geometries | ||
* @see https://github.com/M-Scott-Lassiter/jest-geojson/issues/11 | ||
* @param {object} geometryObject a GeoJSON Multi Point Geometry object | ||
* @returns {boolean} True if a valid GeoJSON Multi Point Geometry. If invalid, it will throw an error. | ||
* @throws {Error} Argument not an object | ||
* @throws {Error} Must have a type property with value 'LineString' | ||
* @throws {Error} forbidden from having a property 'geometry', 'properties', or 'features' | ||
* @example | ||
* const linestring = { | ||
"type": "LineString", | ||
"coordinates": [ | ||
[ | ||
[180.0, 40.0], [180.0, 50.0], [170.0, 50.0], | ||
[170.0, 40.0], [180.0, 40.0] | ||
] | ||
] | ||
} | ||
const point = { | ||
type: "Point", | ||
coordinates: [100.0, 0.0] | ||
} | ||
console.log(lineStringGeometry(linestring)) // true | ||
console.log(lineStringGeometry(point)) // throws error | ||
*/ | ||
function lineStringGeometry(geometryObject) { | ||
if ( | ||
typeof geometryObject !== 'object' || | ||
Array.isArray(geometryObject) || | ||
geometryObject === null | ||
) { | ||
throw new Error('Argument must be a GeoJSON LineString Geometry object.') | ||
} | ||
|
||
if (!('coordinates' in geometryObject)) { | ||
throw new Error(`GeoJSON LineString Geometry must contain a 'coordinates' property.`) | ||
} | ||
|
||
if (geometryObject.type !== 'LineString') { | ||
throw new Error(`Must have a type property with value 'LineString'`) | ||
} | ||
|
||
if ('geometry' in geometryObject) { | ||
throw new Error( | ||
`GeoJSON LineString Geometry objects are forbidden from having a property 'geometry'.` | ||
) | ||
} | ||
|
||
if ('properties' in geometryObject) { | ||
throw new Error( | ||
`GeoJSON LineString Geometry objects are forbidden from having a property 'properties'.` | ||
) | ||
} | ||
|
||
if ('features' in geometryObject) { | ||
throw new Error( | ||
`GeoJSON LineString Geometry objects are forbidden from having a property 'features'.` | ||
) | ||
} | ||
|
||
// // Geometry objects are allowed to have empty arrays as coordinates, however validCoordinate may not. | ||
// If coordinates is an empty array, we're done. Otherwise, check for coordinate validity. | ||
if (!Array.isArray(geometryObject.coordinates) && geometryObject.coordinates.length !== 1) { | ||
throw new Error('Coordinates property must be an array of valid GeoJSON coordinates') | ||
} | ||
for (let i = 0; i < geometryObject.coordinates.length; i++) { | ||
validCoordinate(geometryObject.coordinates[i]) | ||
} | ||
|
||
return true | ||
} | ||
|
||
exports.lineStringGeometry = lineStringGeometry |
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,68 @@ | ||
const { lineStringGeometry } = require('../../core/geometries/lineStringGeometry') | ||
|
||
// eslint-disable-next-line jsdoc/require-returns | ||
/** | ||
* Verifies an object is a valid GeoJSON LineString Geometry. This geometry requires a | ||
* 'type' property that must equal "LineString", and a 'coordinates' property that contains | ||
* an array of valid WGS-84 GeoJSON coordinate(s). The coordinates may be an empty array, | ||
* but may not be an array of empty arrays. | ||
* | ||
* Foreign members are allowed with the exception of 'geometry', 'properties', or 'features'. | ||
* | ||
* @memberof Matchers.Geometries | ||
* @see https://github.com/M-Scott-Lassiter/jest-geojson/issues/11 | ||
* @param {object} geometryObject a GeoJSON MultiPoint Geometry object | ||
* @example | ||
const linestring = { | ||
"type": "LineString", | ||
"coordinates": [ | ||
[ | ||
[180.0, 40.0], [180.0, 50.0], [170.0, 50.0], | ||
[170.0, 40.0], [180.0, 40.0] | ||
] | ||
] | ||
} | ||
const point = { | ||
type: "Point", | ||
coordinates: [100.0, 0.0] | ||
} | ||
expect(linestring).toBeLineStringGeometry() | ||
expect(point).not.toBeLineStringGeometry() | ||
*/ | ||
function toBeLineStringGeometry(geometryObject) { | ||
const { printReceived, matcherHint } = this.utils | ||
const passMessage = | ||
// eslint-disable-next-line prefer-template | ||
matcherHint('.not.toBeLineStringGeometry', 'GeometryObject', '') + | ||
'\n\n' + | ||
`Expected input to not be a valid GeoJSON LineString geometry.\n\n` + | ||
`Received: ${printReceived(geometryObject)}` | ||
|
||
/** | ||
* 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('.toBeLineStringGeometry', 'GeometryObject', '') + | ||
'\n\n' + | ||
`${errorMessage}\n\n` + | ||
`Received: ${printReceived(geometryObject)}` | ||
) | ||
} | ||
|
||
try { | ||
lineStringGeometry(geometryObject) | ||
} catch (err) { | ||
return { pass: false, message: () => failMessage(err.message) } | ||
} | ||
return { pass: true, message: () => passMessage } | ||
} | ||
|
||
exports.toBeLineStringGeometry = toBeLineStringGeometry |
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
17 changes: 17 additions & 0 deletions
17
tests/geometries/__snapshots__/toBeLineStringGeometry.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,17 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`Error Snapshot Testing. Throws error: expect({type: 'LineString', coordinates: [[0, 0]]}).not.toBeLineStringGeometry 1`] = ` | ||
"[2mexpect([22m[31mGeometryObject[39m[2m).not.toBeLineStringGeometry()[22m | ||
Expected input to not be a valid GeoJSON LineString geometry. | ||
Received: [31m{\\"coordinates\\": [[0, 0]], \\"type\\": \\"LineString\\"}[39m" | ||
`; | ||
|
||
exports[`Error Snapshot Testing. Throws error: expect(false).toBeLineStringGeometry() 1`] = ` | ||
"[2mexpect([22m[31mGeometryObject[39m[2m).toBeLineStringGeometry()[22m | ||
Argument must be a GeoJSON LineString Geometry object. | ||
Received: [31mfalse[39m" | ||
`; |
Oops, something went wrong.