-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(isLatLong): add DMS validation (#1340)
Update PR for #1158 closes #1150 #1158 Co-authored-by: rohankulshreshtha <[email protected]>
- Loading branch information
1 parent
8adc4c6
commit 1007097
Showing
5 changed files
with
68 additions
and
5 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,22 @@ | ||
import assertString from './util/assertString'; | ||
import merge from './util/merge'; | ||
var lat = /^\(?[+-]?(90(\.0+)?|[1-8]?\d(\.\d+)?)$/; | ||
var _long = /^\s?[+-]?(180(\.0+)?|1[0-7]\d(\.\d+)?|\d{1,2}(\.\d+)?)\)?$/; | ||
export default function (str) { | ||
var latDMS = /^(([1-8]?\d)\D+([1-5]?\d|60)\D+([1-5]?\d|60)(\.\d+)?|90\D+0\D+0)\D+[NSns]?$/i; | ||
var longDMS = /^\s*([1-7]?\d{1,2}\D+([1-5]?\d|60)\D+([1-5]?\d|60)(\.\d+)?|180\D+0\D+0)\D+[EWew]?$/i; | ||
var defaultLatLongOptions = { | ||
checkDMS: false | ||
}; | ||
export default function isLatLong(str, options) { | ||
assertString(str); | ||
options = merge(options, defaultLatLongOptions); | ||
if (!str.includes(',')) return false; | ||
var pair = str.split(','); | ||
if (pair[0].startsWith('(') && !pair[1].endsWith(')') || pair[1].endsWith(')') && !pair[0].startsWith('(')) return false; | ||
|
||
if (options.checkDMS) { | ||
return latDMS.test(pair[0]) && longDMS.test(pair[1]); | ||
} | ||
|
||
return lat.test(pair[0]) && _long.test(pair[1]); | ||
} |
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 |
---|---|---|
@@ -1,13 +1,27 @@ | ||
import assertString from './util/assertString'; | ||
import merge from './util/merge'; | ||
|
||
const lat = /^\(?[+-]?(90(\.0+)?|[1-8]?\d(\.\d+)?)$/; | ||
const long = /^\s?[+-]?(180(\.0+)?|1[0-7]\d(\.\d+)?|\d{1,2}(\.\d+)?)\)?$/; | ||
|
||
export default function isLatLong(str) { | ||
const latDMS = /^(([1-8]?\d)\D+([1-5]?\d|60)\D+([1-5]?\d|60)(\.\d+)?|90\D+0\D+0)\D+[NSns]?$/i; | ||
const longDMS = /^\s*([1-7]?\d{1,2}\D+([1-5]?\d|60)\D+([1-5]?\d|60)(\.\d+)?|180\D+0\D+0)\D+[EWew]?$/i; | ||
|
||
const defaultLatLongOptions = { | ||
checkDMS: false, | ||
}; | ||
|
||
export default function isLatLong(str, options) { | ||
assertString(str); | ||
options = merge(options, defaultLatLongOptions); | ||
|
||
if (!str.includes(',')) return false; | ||
const pair = str.split(','); | ||
if ((pair[0].startsWith('(') && !pair[1].endsWith(')')) | ||
|| (pair[1].endsWith(')') && !pair[0].startsWith('('))) return false; | ||
|
||
if (options.checkDMS) { | ||
return latDMS.test(pair[0]) && longDMS.test(pair[1]); | ||
} | ||
return lat.test(pair[0]) && long.test(pair[1]); | ||
} |
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