From 358d9a69861609b7be05cd4a7d94e33ddad9e284 Mon Sep 17 00:00:00 2001 From: otto-dev Date: Sun, 27 Sep 2020 19:24:46 +1000 Subject: [PATCH] Allow 'deg' in lat/lon format --- Makefile | 2 +- src/coordinates.coffee | 3 +-- src/validator.coffee | 11 ++++++++--- test/parser.test.coffee | 6 ++++++ 4 files changed, 16 insertions(+), 6 deletions(-) diff --git a/Makefile b/Makefile index 0e58f2a..42d0fb1 100644 --- a/Makefile +++ b/Makefile @@ -7,5 +7,5 @@ test: @nodemon --exec "make test-once --quiet" --ext "coffee,js" --quiet test-once: - @mocha ---require coffeescript/register --ext "coffee,js" + @mocha --require coffeescript/register --ext "coffee,js" 'test/**/*.coffee' diff --git a/src/coordinates.coffee b/src/coordinates.coffee index ca728fa..e0d60da 100644 --- a/src/coordinates.coffee +++ b/src/coordinates.coffee @@ -31,8 +31,7 @@ class Coordinates extractCoordinateNumbers: (coordinates) -> return coordinates.match(/-?\d+(\.\d+)?/g) - - + extractLatitude: -> latitude = @coordinateNumbersToDecimal(@latitudeNumbers) if @latitudeIsNegative() diff --git a/src/validator.coffee b/src/validator.coffee index 04babf1..7392836 100644 --- a/src/validator.coffee +++ b/src/validator.coffee @@ -10,9 +10,10 @@ class Validator validate: (coordinates) -> - @checkContainsNoLetters(coordinates) - @checkValidOrientation(coordinates) - @checkNumbers(coordinates) + cleaned = @removeKnownStrings(coordinates) + @checkContainsNoLetters(cleaned) + @checkValidOrientation(cleaned) + @checkNumbers(cleaned) checkContainsNoLetters: (coordinates) -> @@ -21,6 +22,10 @@ class Validator throw new Error('Coordinate contains invalid alphanumeric characters.') + removeKnownStrings: (coordinates) -> + coordinates.replace(/\bdeg\b/gi, '') + + checkValidOrientation: (coordinates) -> validOrientation = /^[^nsew]*[ns]?[^nsew]*[ew]?[^nsew]*$/i.test(coordinates) if not validOrientation diff --git a/test/parser.test.coffee b/test/parser.test.coffee index 482c780..b42e6af 100644 --- a/test/parser.test.coffee +++ b/test/parser.test.coffee @@ -26,6 +26,8 @@ expectation = "40°25’6\"N 74°38’28\"W" "40°25’6\" -74°38’28\"" "40d 25’ 6\" N 74d 38’ 28\" W" + "40 deg 25' 6\" N, 74 deg 38’ 28\" W" + "40 deg 25' 6\", -74 deg 38’ 28\"" "40.4183318N 74.6411133W" "40° 25.0999, -74° 38.4668" ] @@ -54,6 +56,8 @@ reversedExpectation = "40°25’6\"S 74°38’28\"E" "-40°25’6\" 74°38’28\"" "40d 25’ 6\" S 74d 38’ 28\" E" + "40 deg 25' 6\" S, 74 deg 38’ 28\" E" + "-40 deg 25' 6\", 74 deg 38’ 28\"" "40.4183318S 74.6411133E" "40.4183318S 74.6411133" "-40° 25.0999, 74° 38.4668" @@ -81,6 +85,8 @@ invalidFormats = [ "40.1° W 60.1° W" "40.1° N 60.1° N" "-40.4183318, 12.345, 74.6411133" + "-40 degrees 25' 6\", 74 degrees 38’ 28\"" + "-40 degN 25' 6\", 74 degW 38’ 28\"" ]