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(isLat, isLong) #1830: implemented isLat and iLong #1863

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,9 @@ 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.
**isLat(str [, options])** | check if the string is a valid latitude coordinate in the format `lat`.<br/><br/>`options` is an object that defaults to `{ checkDMS: false }`. Pass `checkDMS` as `true` to validate DMS(degrees, minutes, and seconds) latitude format.
**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) latitude-longitude format.
**isLong(str [, options])** | check if the string is a valid longitude coordinate in the format `long`.<br/><br/>`options` is an object that defaults to `{ checkDMS: false }`. Pass `checkDMS` as `true` to validate DMS(degrees, minutes, and seconds) 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.
**isLicensePlate(str [, locale])** | check if string matches the format of a country's license plate.<br/><br/>(locale is one of `['cs-CZ', 'de-DE', 'de-LI', 'fi-FI', pt-PT', 'sq-AL', 'pt-BR']` or `any`)
**isLocale(str)** | check if the string is a locale
Expand Down
4 changes: 4 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,9 @@ import isMagnetURI from './lib/isMagnetURI';

import isMimeType from './lib/isMimeType';

import isLat from './lib/isLat';
import isLatLong from './lib/isLatLong';
import isLong from './lib/isLong';
import isPostalCode, { locales as isPostalCodeLocales } from './lib/isPostalCode';

import ltrim from './lib/ltrim';
Expand Down Expand Up @@ -206,7 +208,9 @@ const validator = {
isDataURI,
isMagnetURI,
isMimeType,
isLat,
isLatLong,
isLong,
ltrim,
rtrim,
trim,
Expand Down
17 changes: 17 additions & 0 deletions src/lib/isLat.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import assertString from './util/assertString';
import merge from './util/merge';

const lat = /^\(?[+-]?(90(\.0+)?|[1-8]?\d(\.\d+)?)$/;

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 defaultLatLongOptions = {
checkDMS: false,
};

export default function isLat(str, options) {
assertString(str);
options = merge(options, defaultLatLongOptions);

return options.checkDMS ? latDMS.test(str) : lat.test(str);
}
19 changes: 3 additions & 16 deletions src/lib/isLatLong.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,14 @@
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+)?)\)?$/;

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,
};
import isLat from './isLat';
import isLong from './isLong';

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]);
return isLat(pair[0], options) && isLong(pair[1], options);
}
16 changes: 16 additions & 0 deletions src/lib/isLong.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import assertString from './util/assertString';
import merge from './util/merge';

const long = /^\s?[+-]?(180(\.0+)?|1[0-7]\d(\.\d+)?|\d{1,2}(\.\d+)?)\)?$/;
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 isLong(str, options) {
assertString(str);
options = merge(options, defaultLatLongOptions);

return options.checkDMS ? longDMS.test(str) : long.test(str);
}
143 changes: 143 additions & 0 deletions test/validators.js
Original file line number Diff line number Diff line change
Expand Up @@ -10045,6 +10045,82 @@ describe('Validators', () => {
/* eslint-enable max-len */
});

it('should valid Lat', () => {
test({
validator: 'isLat',
valid: [
'-12.3456789',
'-60.978437',
'77.719772',
'7.264394',
'31.269161',
'+12.3456789',
'-15.379543',
'11.770570',
'-55.034319',
'58.025555',
'55.720923',
'-90.00000',
'-71',
'-71.616864',
'-0.55',
'90',
'+90',
'-90',
'90',
'0',
],
invalid: [
'020.000000',
'90.1000000',
'090.0000',
'126',
'-126.400010',
'-95',
'-95.738043',
'137',
'-137.5942',
'-120',
'-119',
'+119.821728',
'-110',
'-110.369532',
'-120.969949',
'-116',
'-116.894222',
'-112',
'-112.96381',
'-90.',
'+90.1',
'+',
'',
' ',
],
});
test({
validator: 'isLat',
args: [{
checkDMS: true,
}],
valid: [
'40° 26′ 46″ N',
'40° 26′ 46″ S',
'90° 0′ 0″ S',
'40° 26′ 45.9996″ N',
'40° 26′ 46″ n',
'40°26′46″s',
'11° 0′ 0.005″ S',
'40°26′45.9996″N',

],
invalid: [
'100° 26′ 46″ N',
'40° 89′ 46″ S',
'40° 26.445′ 45″ N',
'40° 46″ N',
],
});
});

it('should validate LatLong', () => {
test({
Expand Down Expand Up @@ -10131,6 +10207,73 @@ describe('Validators', () => {
});
});

it('should validate Long', () => {
test({
validator: 'isLong',
valid: [
'85.605469',
'+12.3456789',
'-0.175781',
'-37.529297',
'165.058594',
'-19.863281',
'164.355469',
'-12.3456789',
'-137.285156',
'-162.949219',
'113.027344',
'36.738281',
'-28.652344',
'-180.00000',
'-146',
'-146.616864',
'+0.22',
'180',
'-180',
'+180',
'180',
'0',
],
invalid: [
'010.000000000',
'360.0000000',
'-180.00001',
'0180.0000',
'-203',
'-196',
'-196.821728',
'-223',
'223.369532',
'+203.969949',
'-180.',
'-180.1',
'-',
'',
' ',
],
});

test({
validator: 'isLong',
args: [{
checkDMS: true,
}],
valid: [
'79° 58′ 56″ W',
'79° 58′ 56″ E',
'180° 0′ 0″ E',
'79° 58′ 56″ w',
'79°58′56″e',
'180° 0′ 0″ E',
'79°58′55.2″E',
],
invalid: [
'79° 70′ 56″ W',
'79° 58′ 100″ E',
],
});
});

it('should validate postal code', () => {
const fixtures = [
{
Expand Down