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

More validators #684

Merged
merged 6 commits into from
Aug 22, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
5 changes: 5 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,10 @@ var _isDataURI = require('./lib/isDataURI');

var _isDataURI2 = _interopRequireDefault(_isDataURI);

var _isLatLong = require('./lib/isLatLong');

var _isLatLong2 = _interopRequireDefault(_isLatLong);

var _ltrim = require('./lib/ltrim');

var _ltrim2 = _interopRequireDefault(_ltrim);
Expand Down Expand Up @@ -304,6 +308,7 @@ var validator = {
isISO8601: _isISO2.default,
isBase64: _isBase2.default,
isDataURI: _isDataURI2.default,
isLatLong: _isLatLong2.default,
ltrim: _ltrim2.default,
rtrim: _rtrim2.default,
trim: _trim2.default,
Expand Down
23 changes: 23 additions & 0 deletions lib/isLatLong.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
'use strict';

Object.defineProperty(exports, "__esModule", {
value: true
});

exports.default = function (str) {
(0, _assertString2.default)(str);
if (!str.includes(',')) return false;
var pair = str.split(',');
return lat.test(pair[0]) && long.test(pair[1]);
};

var _assertString = require('./util/assertString');

var _assertString2 = _interopRequireDefault(_assertString);

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+)?)\)?$/;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is an optional whitespace tolerated at the beginning of long, but not at the end of long or the beginning or end of lat?

Copy link
Contributor Author

@0xch4z 0xch4z Aug 22, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because it is pretty common to separate coordinates with a comma and a space, but the space should not be mandatory. It's not common however, to prefix with a space, or end with a space. That's needless and would look incredibly silly.


module.exports = exports['default'];
3 changes: 3 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ import isISO8601 from './lib/isISO8601';
import isBase64 from './lib/isBase64';
import isDataURI from './lib/isDataURI';

import isLatLong from './lib/isLatLong';

import ltrim from './lib/ltrim';
import rtrim from './lib/rtrim';
import trim from './lib/trim';
Expand Down Expand Up @@ -136,6 +138,7 @@ const validator = {
isISO8601,
isBase64,
isDataURI,
isLatLong,
ltrim,
rtrim,
trim,
Expand Down
11 changes: 11 additions & 0 deletions src/lib/isLatLong.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import assertString from './util/assertString';

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 (str) {
assertString(str);
if (!str.includes(',')) return false;
const pair = str.split(',');
return lat.test(pair[0]) && long.test(pair[1]);
}
59 changes: 59 additions & 0 deletions test/validators.js
Original file line number Diff line number Diff line change
Expand Up @@ -3796,4 +3796,63 @@ describe('Validators', function () {
});
/* eslint-enable max-len */
});

it('should validate LatLong', function () {
test({
validator: 'isLatLong',
valid: [
'(-17.738223, 85.605469)',
'(-12.3456789, +12.3456789)',
'(-60.978437, -0.175781)',
'(77.719772, -37.529297)',
'(7.264394, 165.058594)',
'0.955766, -19.863281',
'(31.269161,164.355469)',
'+12.3456789, -12.3456789',
'-15.379543, -137.285156',
'(11.770570, -162.949219)',
'-55.034319, 113.027344',
'58.025555, 36.738281',
'55.720923,-28.652344',
'-90.00000,-180.00000',
'(-71, -146)',
'(-71.616864, -146.616864)',
'-0.55, +0.22',
'90, 180',
'+90, -180',
'-90,+180',
'90,180',
'0, 0',
],
invalid: [
'(020.000000, 010.000000000)',
'89.9999999989, 360.0000000',
'90.1000000, 180.000000',
'+90.000000, -180.00001',
'090.0000, 0180.0000',
'126, -158',
'(-126.400010, -158.400010)',
'-95, -96',
'-95.738043, -96.738043',
'137, -148',
'(-137.5942, -148.5942)',
'(-120, -203)',
'(-119, -196)',
'+119.821728, -196.821728',
'(-110, -223)',
'-110.369532, 223.369532',
'(-120.969949, +203.969949)',,
'-116, -126',
'-116.894222, -126.894222',
'-112, -160',
'-112.96381, -160.96381',
'-90., -180.',
'+90.1, -180.1',
'+,-',
'(,)',
',',
' ',
],
});
});
});
11 changes: 11 additions & 0 deletions validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -1055,6 +1055,16 @@ function isDataURI(str) {
return dataURI.test(str);
}

var lat = /^\(?[+-]?(90(\.0+)?|[1-8]?\d(\.\d+)?)$/;
var long = /^\s?[+-]?(180(\.0+)?|1[0-7]\d(\.\d+)?|\d{1,2}(\.\d+)?)\)?$/;

var isLatLong = function (str) {
assertString(str);
if (!str.includes(',')) return false;
var pair = str.split(',');
return lat.test(pair[0]) && long.test(pair[1]);
};

function ltrim(str, chars) {
assertString(str);
var pattern = chars ? new RegExp('^[' + chars + ']+', 'g') : /^\s+/g;
Expand Down Expand Up @@ -1287,6 +1297,7 @@ var validator = {
isISO8601: isISO8601,
isBase64: isBase64,
isDataURI: isDataURI,
isLatLong: isLatLong,
ltrim: ltrim,
rtrim: rtrim,
trim: trim,
Expand Down
2 changes: 1 addition & 1 deletion validator.min.js

Large diffs are not rendered by default.