isLatLong.js 910 B

12345678910111213141516171819202122
  1. import assertString from './util/assertString';
  2. import merge from './util/merge';
  3. var lat = /^\(?[+-]?(90(\.0+)?|[1-8]?\d(\.\d+)?)$/;
  4. var _long = /^\s?[+-]?(180(\.0+)?|1[0-7]\d(\.\d+)?|\d{1,2}(\.\d+)?)\)?$/;
  5. var latDMS = /^(([1-8]?\d)\D+([1-5]?\d|60)\D+([1-5]?\d|60)(\.\d+)?|90\D+0\D+0)\D+[NSns]?$/i;
  6. 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;
  7. var defaultLatLongOptions = {
  8. checkDMS: false
  9. };
  10. export default function isLatLong(str, options) {
  11. assertString(str);
  12. options = merge(options, defaultLatLongOptions);
  13. if (!str.includes(',')) return false;
  14. var pair = str.split(',');
  15. if (pair[0].startsWith('(') && !pair[1].endsWith(')') || pair[1].endsWith(')') && !pair[0].startsWith('(')) return false;
  16. if (options.checkDMS) {
  17. return latDMS.test(pair[0]) && longDMS.test(pair[1]);
  18. }
  19. return lat.test(pair[0]) && _long.test(pair[1]);
  20. }