Skip to content

Commit

Permalink
fix(toFloat): verify the string can be safely converted to a float (#…
Browse files Browse the repository at this point in the history
…1226)

This is needed because parseFloat('2020-01-06T14:31:00.135Z') will return 2020, even though it should return NaN. This fixes an issue checking isDivisibleBy('2020-01-06T14:31:00.135Z', 2).
  • Loading branch information
peterdemartini committed Jan 6, 2020
1 parent ea39867 commit 5abe332
Show file tree
Hide file tree
Showing 12 changed files with 172 additions and 136 deletions.
4 changes: 2 additions & 2 deletions es/lib/alpha.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export var alpha = {
'nn-NO': /^[A-ZÆØÅ]+$/i,
'hu-HU': /^[A-ZÁÉÍÓÖŐÚÜŰ]+$/i,
'pl-PL': /^[A-ZĄĆĘŚŁŃÓŻŹ]+$/i,
'pt-PT': /^[A-ZÃÁÀÂÇÉÊÍÕÓÔÚÜ]+$/i,
'pt-PT': /^[A-ZÃÁÀÂÄÇÉÊËÍÏÕÓÔÖÚÜ]+$/i,
'ru-RU': /^[А-ЯЁ]+$/i,
'sl-SI': /^[A-ZČĆĐŠŽ]+$/i,
'sk-SK': /^[A-ZÁČĎÉÍŇÓŠŤÚÝŽĹŔĽÄÔ]+$/i,
Expand Down Expand Up @@ -42,7 +42,7 @@ export var alphanumeric = {
'nl-NL': /^[0-9A-ZÁÉËÏÓÖÜÚ]+$/i,
'nn-NO': /^[0-9A-ZÆØÅ]+$/i,
'pl-PL': /^[0-9A-ZĄĆĘŚŁŃÓŻŹ]+$/i,
'pt-PT': /^[0-9A-ZÃÁÀÂÇÉÊÍÕÓÔÚÜ]+$/i,
'pt-PT': /^[0-9A-ZÃÁÀÂÄÇÉÊËÍÏÕÓÔÖÚÜ]+$/i,
'ru-RU': /^[0-9А-ЯЁ]+$/i,
'sl-SI': /^[0-9A-ZČĆĐŠŽ]+$/i,
'sk-SK': /^[0-9A-ZÁČĎÉÍŇÓŠŤÚÝŽĹŔĽÄÔ]+$/i,
Expand Down
5 changes: 4 additions & 1 deletion es/lib/isMobilePhone.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import assertString from './util/assertString';
/* eslint-disable max-len */

var phones = {
'am-AM': /^(\+?374|0)((10|[9|7][0-9])\d{6}$|[2-4]\d{7}$)/,
'ar-AE': /^((\+?971)|0)?5[024568]\d{7}$/,
'ar-BH': /^(\+?973)?(3|6)\d{7}$/,
'ar-DZ': /^(\+?213|0)(5|6|7)\d{8}$/,
Expand All @@ -24,7 +25,8 @@ var phones = {
'en-GB': /^(\+?44|0)7\d{9}$/,
'en-GG': /^(\+?44|0)1481\d{6}$/,
'en-GH': /^(\+233|0)(20|50|24|54|27|57|26|56|23|28)\d{7}$/,
'en-HK': /^(\+?852\-?)?[456789]\d{3}\-?\d{4}$/,
'en-HK': /^(\+?852[-\s]?)?[456789]\d{3}[-\s]?\d{4}$/,
'en-MO': /^(\+?853[-\s]?)?[6]\d{3}[-\s]?\d{4}$/,
'en-IE': /^(\+?353|0)8[356789]\d{7}$/,
'en-IN': /^(\+?91|0)?[6789]\d{9}$/,
'en-KE': /^(\+?254|0)(7|1)\d{8}$/,
Expand Down Expand Up @@ -94,6 +96,7 @@ var phones = {
phones['en-CA'] = phones['en-US'];
phones['fr-BE'] = phones['nl-BE'];
phones['zh-HK'] = phones['en-HK'];
phones['zh-MO'] = phones['en-MO'];
export default function isMobilePhone(str, locale, options) {
assertString(str);

Expand Down
11 changes: 11 additions & 0 deletions es/lib/isURL.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,17 @@ import assertString from './util/assertString';
import isFQDN from './isFQDN';
import isIP from './isIP';
import merge from './util/merge';
/*
options for isURL method
require_protocol - if set as true isURL will return false if protocol is not present in the URL
require_valid_protocol - isURL will check if the URL's protocol is present in the protocols option
protocols - valid protocols can be modified with this option
require_host - if set as false isURL will not check if host is present in the URL
allow_protocol_relative_urls - if set as true protocol relative URLs will be allowed
*/

var default_url_options = {
protocols: ['http', 'https', 'ftp'],
require_tld: true,
Expand Down
4 changes: 2 additions & 2 deletions es/lib/toFloat.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import assertString from './util/assertString';
import isFloat from './isFloat';
export default function toFloat(str) {
assertString(str);
if (!isFloat(str)) return NaN;
return parseFloat(str);
}
4 changes: 2 additions & 2 deletions lib/alpha.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ var alpha = {
'nn-NO': /^[A-ZÆØÅ]+$/i,
'hu-HU': /^[A-ZÁÉÍÓÖŐÚÜŰ]+$/i,
'pl-PL': /^[A-ZĄĆĘŚŁŃÓŻŹ]+$/i,
'pt-PT': /^[A-ZÃÁÀÂÇÉÊÍÕÓÔÚÜ]+$/i,
'pt-PT': /^[A-ZÃÁÀÂÄÇÉÊËÍÏÕÓÔÖÚÜ]+$/i,
'ru-RU': /^[А-ЯЁ]+$/i,
'sl-SI': /^[A-ZČĆĐŠŽ]+$/i,
'sk-SK': /^[A-ZÁČĎÉÍŇÓŠŤÚÝŽĹŔĽÄÔ]+$/i,
Expand Down Expand Up @@ -49,7 +49,7 @@ var alphanumeric = {
'nl-NL': /^[0-9A-ZÁÉËÏÓÖÜÚ]+$/i,
'nn-NO': /^[0-9A-ZÆØÅ]+$/i,
'pl-PL': /^[0-9A-ZĄĆĘŚŁŃÓŻŹ]+$/i,
'pt-PT': /^[0-9A-ZÃÁÀÂÇÉÊÍÕÓÔÚÜ]+$/i,
'pt-PT': /^[0-9A-ZÃÁÀÂÄÇÉÊËÍÏÕÓÔÖÚÜ]+$/i,
'ru-RU': /^[0-9А-ЯЁ]+$/i,
'sl-SI': /^[0-9A-ZČĆĐŠŽ]+$/i,
'sk-SK': /^[0-9A-ZÁČĎÉÍŇÓŠŤÚÝŽĹŔĽÄÔ]+$/i,
Expand Down
5 changes: 4 additions & 1 deletion lib/isMobilePhone.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { de

/* eslint-disable max-len */
var phones = {
'am-AM': /^(\+?374|0)((10|[9|7][0-9])\d{6}$|[2-4]\d{7}$)/,
'ar-AE': /^((\+?971)|0)?5[024568]\d{7}$/,
'ar-BH': /^(\+?973)?(3|6)\d{7}$/,
'ar-DZ': /^(\+?213|0)(5|6|7)\d{8}$/,
Expand All @@ -34,7 +35,8 @@ var phones = {
'en-GB': /^(\+?44|0)7\d{9}$/,
'en-GG': /^(\+?44|0)1481\d{6}$/,
'en-GH': /^(\+233|0)(20|50|24|54|27|57|26|56|23|28)\d{7}$/,
'en-HK': /^(\+?852\-?)?[456789]\d{3}\-?\d{4}$/,
'en-HK': /^(\+?852[-\s]?)?[456789]\d{3}[-\s]?\d{4}$/,
'en-MO': /^(\+?853[-\s]?)?[6]\d{3}[-\s]?\d{4}$/,
'en-IE': /^(\+?353|0)8[356789]\d{7}$/,
'en-IN': /^(\+?91|0)?[6789]\d{9}$/,
'en-KE': /^(\+?254|0)(7|1)\d{8}$/,
Expand Down Expand Up @@ -104,6 +106,7 @@ var phones = {
phones['en-CA'] = phones['en-US'];
phones['fr-BE'] = phones['nl-BE'];
phones['zh-HK'] = phones['en-HK'];
phones['zh-MO'] = phones['en-MO'];

function isMobilePhone(str, locale, options) {
(0, _assertString.default)(str);
Expand Down
4 changes: 2 additions & 2 deletions lib/toFloat.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ Object.defineProperty(exports, "__esModule", {
});
exports.default = toFloat;

var _assertString = _interopRequireDefault(require("./util/assertString"));
var _isFloat = _interopRequireDefault(require("./isFloat"));

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function toFloat(str) {
(0, _assertString.default)(str);
if (!(0, _isFloat.default)(str)) return NaN;
return parseFloat(str);
}

Expand Down
5 changes: 3 additions & 2 deletions src/lib/toFloat.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import assertString from './util/assertString';
import isFloat from './isFloat';

export default function toFloat(str) {
assertString(str);
if (!isFloat(str)) return NaN;

return parseFloat(str);
}
1 change: 1 addition & 0 deletions test/sanitizers.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ describe('Sanitizers', () => {
'2.': 2.0,
'-2.5': -2.5,
'.5': 0.5,
'2020-01-06T14:31:00.135Z': NaN,
foo: NaN,
},
});
Expand Down
3 changes: 3 additions & 0 deletions test/validators.js
Original file line number Diff line number Diff line change
Expand Up @@ -2335,6 +2335,8 @@ describe('Validators', () => {
'',
'.',
'foo',
'20.foo',
'2020-01-06T14:31:00.135Z',
],
});

Expand Down Expand Up @@ -3187,6 +3189,7 @@ describe('Validators', () => {
'101',
'foo',
'',
'2020-01-06T14:31:00.135Z',
],
});
});
Expand Down
Loading

0 comments on commit 5abe332

Please sign in to comment.