Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(isLatLong): Added DMS validation #1340

Merged
merged 20 commits into from
Jun 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
c63b44f
Fixes #1303
parasg1999 May 25, 2020
c8419de
Added postal code for nepal
parasg1999 May 27, 2020
f513c29
Added NP locale to postal code
parasg1999 May 27, 2020
4d7828d
Added country code to passport READNE
parasg1999 May 27, 2020
c669f38
Merge branch 'master' of https://github.com/validatorjs/validator.js …
parasg1999 May 27, 2020
5715d5b
Merge branch 'master' of https://github.com/validatorjs/validator.js
parasg1999 May 27, 2020
b068987
Merge branch 'master' of https://github.com/validatorjs/validator.js
parasg1999 May 27, 2020
5ddff83
Merge branch 'master' of https://github.com/parasg1999/validator.js
parasg1999 May 27, 2020
778dfd7
Added norway identity card
parasg1999 May 28, 2020
6246c60
Merge branch 'master' of https://github.com/validatorjs/validator.js
parasg1999 May 28, 2020
f288d61
Merge branch 'feature' of https://github.com/parasg1999/validator.js
parasg1999 May 28, 2020
3418d28
Fix order in readme
parasg1999 May 28, 2020
006f552
Merge branch 'master' of https://github.com/validatorjs/validator.js
parasg1999 May 30, 2020
e35e2f0
Added ignoreCase to contains
parasg1999 May 30, 2020
2f019c2
Resolved merge conflicts
parasg1999 May 30, 2020
af08558
Update README
parasg1999 May 30, 2020
e8fa246
Merge branch 'master' of https://github.com/validatorjs/validator.js
parasg1999 May 30, 2020
715492f
Merge branch 'feature'
parasg1999 May 30, 2020
97352a5
Merge branch 'master' of https://github.com/validatorjs/validator.js
parasg1999 May 31, 2020
176dfd5
Added DMS to isLatLong
parasg1999 Jun 2, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -7978,6 +7978,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