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

fix(isLatLong): fix bug on isLatLang #1074

Merged
merged 2 commits into from Aug 2, 2019
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/isLatLong.js
Expand Up @@ -16,6 +16,7 @@ function _default(str) {
(0, _assertString.default)(str);
if (!str.includes(',')) return false;
var pair = str.split(',');
if (pair[0].charAt(0) === '(' && pair[1].charAt(pair[1].length - 1) !== ')' || pair[1].charAt(pair[1].length - 1) === ')' && pair[0].charAt(0) !== '(') return false;
Copy link
Member

Choose a reason for hiding this comment

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

Can re-format/write this for better readability?

if (pair[0].startsWith('(') && !pair[1].endsWith(')')
  || pair[1].endsWith(')') && !pair[0].startsWith('(') return false;

Copy link
Member Author

Choose a reason for hiding this comment

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

I will work on that @profnandaa.

Thank you.

return lat.test(pair[0]) && long.test(pair[1]);
}

Expand Down
4 changes: 4 additions & 0 deletions src/lib/isLatLong.js
Expand Up @@ -7,5 +7,9 @@ export default function (str) {
assertString(str);
if (!str.includes(',')) return false;
const pair = str.split(',');
if (
(pair[0].charAt(0) === '(' && pair[1].charAt(pair[1].length - 1) !== ')') ||
(pair[1].charAt(pair[1].length - 1) === ')' && pair[0].charAt(0) !== '(')
) return false;
Copy link
Collaborator

Choose a reason for hiding this comment

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

This will fail if str has length of zero:

> const str = ""
> const pair = str.split(',')
> pair
[ '' ]
> pair[0].charAt(0)
''
> pair[1].charAt(pair[1].length - 1)
Thrown:
TypeError: Cannot read property 'charAt' of undefined

Copy link
Member Author

@ezkemboi ezkemboi Jul 29, 2019

Choose a reason for hiding this comment

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

@chriso, will it mean that we have to catch empty validations, for instance when a user passes an empty value for either latitude or longitude?
Or we should look on ways to integrate this validation in the regex path?
Maybe you can help me get some bigger picture...

Copy link
Collaborator

@chriso chriso Jul 29, 2019

Choose a reason for hiding this comment

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

Sorry, disregard my point. I see there's an if (!str.includes(',')) return false; guard above which disallows an empty string.

Copy link
Member Author

Choose a reason for hiding this comment

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

The regex ^\(.*\) might do the same validation, but I don't know if it will throw an error.
I am open to possibilities/opinions on the same.

return lat.test(pair[0]) && long.test(pair[1]);
}
2 changes: 2 additions & 0 deletions test/validators.js
Expand Up @@ -6523,6 +6523,8 @@ describe('Validators', () => {
'-112.96381, -160.96381',
'-90., -180.',
'+90.1, -180.1',
'(-17.738223, 85.605469',
'0.955766, -19.863281)',
'+,-',
'(,)',
',',
Expand Down
1 change: 1 addition & 0 deletions validator.js
Expand Up @@ -1739,6 +1739,7 @@ var isLatLong = function (str) {
assertString(str);
if (!str.includes(',')) return false;
var pair = str.split(',');
if (pair[0].charAt(0) === '(' && pair[1].charAt(pair[1].length - 1) !== ')' || pair[1].charAt(pair[1].length - 1) === ')' && pair[0].charAt(0) !== '(') return false;
return lat.test(pair[0]) && _long.test(pair[1]);
};

Expand Down
2 changes: 1 addition & 1 deletion validator.min.js

Large diffs are not rendered by default.