Skip to content

Commit

Permalink
feat(isLatLong): add DMS validation (#1340)
Browse files Browse the repository at this point in the history
Update PR for #1158 

closes #1150  #1158 

Co-authored-by: rohankulshreshtha <[email protected]>
  • Loading branch information
parasg1999 and rohankulshreshtha authored Jun 3, 2020
1 parent 8adc4c6 commit 1007097
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 5 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).<br/><br/>`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).<br/><br/>`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`.<br/><br/>`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.<br/><br/>`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.
Expand Down
14 changes: 13 additions & 1 deletion es/lib/isLatLong.js
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]);
}
17 changes: 15 additions & 2 deletions lib/isLatLong.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
}

Expand Down
16 changes: 15 additions & 1 deletion src/lib/isLatLong.js
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]);
}
24 changes: 24 additions & 0 deletions test/validators.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down

0 comments on commit 1007097

Please sign in to comment.