diff --git a/README.md b/README.md index c8c8aa6ee..76c23d479 100644 --- a/README.md +++ b/README.md @@ -127,7 +127,7 @@ Validator | Description **isISSN(str [, options])** | check if the string is an [ISSN](https://en.wikipedia.org/wiki/International_Standard_Serial_Number).

`options` is an object which defaults to `{ case_sensitive: false, require_hyphen: false }`. If `case_sensitive` is true, ISSNs with a lowercase `'x'` as the check digit are rejected. **isJSON(str [, options])** | check if the string is valid JSON (note: uses JSON.parse).

`options` is an object which defaults to `{ allow_primitives: false }`. If `allow_primitives` is true, the primitives 'true', 'false' and 'null' are accepted as valid JSON values. **isJWT(str)** | check if the string is valid JWT token. -**isLatLong(str)** | check if the string is a valid latitude-longitude coordinate in the format `lat,long` or `lat, long`. +**isLatLong(str [, options])** | check if the string is a valid latitude-longitude coordinate in the format `lat,long` or `lat, long`.

`options` is an object that defaults to `{ checkDMS: false }`. Pass `checkDMS` as `true` to validate DMS(degrees, minutes, and seconds) latiture-longitude format. **isLength(str [, options])** | check if the string's length falls in a range.

`options` is an object which defaults to `{min:0, max: undefined}`. Note: this function takes into account surrogate pairs. **isLocale(str)** | check if the string is a locale **isLowercase(str)** | check if the string is lowercase. diff --git a/es/lib/isLatLong.js b/es/lib/isLatLong.js index e081755f6..7362c1d01 100644 --- a/es/lib/isLatLong.js +++ b/es/lib/isLatLong.js @@ -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]); } \ No newline at end of file diff --git a/lib/isLatLong.js b/lib/isLatLong.js index 01ac57649..e288812bb 100644 --- a/lib/isLatLong.js +++ b/lib/isLatLong.js @@ -3,20 +3,33 @@ Object.defineProperty(exports, "__esModule", { value: true }); -exports.default = _default; +exports.default = isLatLong; var _assertString = _interopRequireDefault(require("./util/assertString")); +var _merge = _interopRequireDefault(require("./util/merge")); + function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } var lat = /^\(?[+-]?(90(\.0+)?|[1-8]?\d(\.\d+)?)$/; var long = /^\s?[+-]?(180(\.0+)?|1[0-7]\d(\.\d+)?|\d{1,2}(\.\d+)?)\)?$/; +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 +}; -function _default(str) { +function isLatLong(str, options) { (0, _assertString.default)(str); + options = (0, _merge.default)(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]); } diff --git a/src/lib/isLatLong.js b/src/lib/isLatLong.js index 5adb44242..5c53c23aa 100644 --- a/src/lib/isLatLong.js +++ b/src/lib/isLatLong.js @@ -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]); } diff --git a/test/validators.js b/test/validators.js index bc0e4858d..ae4b87c09 100755 --- a/test/validators.js +++ b/test/validators.js @@ -8017,6 +8017,30 @@ describe('Validators', () => { ' ', ], }); + + test({ + validator: 'isLatLong', + args: [{ + checkDMS: true, + }], + valid: [ + '40° 26′ 46″ N, 79° 58′ 56″ W', + '40° 26′ 46″ S, 79° 58′ 56″ E', + '90° 0′ 0″ S, 180° 0′ 0″ E', + '40° 26′ 45.9996″ N, 79° 58′ 55.2″ E', + '40° 26′ 46″ n, 79° 58′ 56″ w', + '40°26′46″s, 79°58′56″e', + '11° 0′ 0.005″ S, 180° 0′ 0″ E', + '40°26′45.9996″N, 79°58′55.2″E', + + ], + invalid: [ + '100° 26′ 46″ N, 79° 70′ 56″ W', + '40° 89′ 46″ S, 79° 58′ 100″ E', + '40° 26.445′ 45″ N, 79° 58′ 55.2″ E', + '40° 46″ N, 79° 58′ 56″ W', + ], + }); }); it('should validate postal code', () => {