matchers/coordinates/isValid3DCoordinate.js

  1. const { valid3DCoordinate } = require('../../core/coordinates/valid3DCoordinate')
  2. // eslint-disable-next-line jsdoc/require-returns
  3. /**
  4. * Verifies a three element coordinate meets WGS-84 and GeoJSON validity requirements.
  5. *
  6. * @memberof Matchers.Coordinates
  7. * @see https://github.com/M-Scott-Lassiter/jest-geojson/issues/2
  8. * @param {number[]} coordinateArray A three element array of numbers in format [longitude, latitude, altitude].
  9. * Longitude must be between -180 to 180.
  10. * Latitude must be between -90 to 90.
  11. * Altitude must be a number between -Infinity to Infinity.
  12. * The standard does not specify units altitude represents (i.e. meters, feet, etc.).
  13. * @example
  14. * test('Object is valid GeoJSON', () => {
  15. * expect([22, 45.733, 20]).isValid3DCoordinate()
  16. * expect([180, 90, -10000]).isValid3DCoordinate()
  17. * })
  18. * @example
  19. * test('Object is NOT valid GeoJSON', () => {
  20. * expect([22, 100.56, 0]).not.isValid3DCoordinate() // Latitude out of range
  21. * expect([22, 45.733]).isValid3DCoordinate() // 2D coordinate
  22. * expect([22, 45.733, '0']).not.isValid3DCoordinate() // Non-numeric altitude
  23. * // Nested Arrays
  24. * expect([[22, 45.733, 0]]).not.isValid3DCoordinate()
  25. * expect([[22, 45.733, 0], [180, 90, 0]]).not.isValid3DCoordinate()
  26. * })
  27. */
  28. function isValid3DCoordinate(coordinateArray) {
  29. const { printReceived, matcherHint } = this.utils
  30. const passMessage =
  31. // eslint-disable-next-line prefer-template
  32. matcherHint('.not.isValid3DCoordinate', '[longitude, latitude, altitude]', '') +
  33. '\n\n' +
  34. `Expected input to not be a three element array with longitude between (-90 to 90),
  35. latitude between (-180 to 180), and numeric altitude.\n\n` +
  36. `Received: ${printReceived(coordinateArray)}`
  37. /**
  38. * Combines a custom error message with built in Jest tools to provide a more descriptive error
  39. * meessage to the end user.
  40. *
  41. * @param {string} errorMessage Error message text to return to the user
  42. * @returns {string} Concatenated Jest test result string
  43. */
  44. function failMessage(errorMessage) {
  45. return (
  46. // eslint-disable-next-line prefer-template, no-unused-expressions
  47. matcherHint('.isValid3DCoordinate', '[longitude, latitude, altitude]', '') +
  48. '\n\n' +
  49. `${errorMessage}\n\n` +
  50. `Received: ${printReceived(coordinateArray)}`
  51. )
  52. }
  53. try {
  54. valid3DCoordinate(coordinateArray)
  55. } catch (err) {
  56. return { pass: false, message: () => failMessage(err.message) }
  57. }
  58. return { pass: true, message: () => passMessage }
  59. }
  60. exports.isValid3DCoordinate = isValid3DCoordinate