Skip to content

Commit 5abe332

Browse files
fix(toFloat): verify the string can be safely converted to a float (#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).
1 parent ea39867 commit 5abe332

12 files changed

+172
-136
lines changed

es/lib/alpha.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export var alpha = {
1313
'nn-NO': /^[A-ZÆØÅ]+$/i,
1414
'hu-HU': /^[A-ZÁÉÍÓÖŐÚÜŰ]+$/i,
1515
'pl-PL': /^[A-ZĄĆĘŚŁŃÓŻŹ]+$/i,
16-
'pt-PT': /^[A-ZÃÁÀÂÇÉÊÍÕÓÔÚÜ]+$/i,
16+
'pt-PT': /^[A-ZÃÁÀÂÄÇÉÊËÍÏÕÓÔÖÚÜ]+$/i,
1717
'ru-RU': /^[А-ЯЁ]+$/i,
1818
'sl-SI': /^[A-ZČĆĐŠŽ]+$/i,
1919
'sk-SK': /^[A-ZÁČĎÉÍŇÓŠŤÚÝŽĹŔĽÄÔ]+$/i,
@@ -42,7 +42,7 @@ export var alphanumeric = {
4242
'nl-NL': /^[0-9A-ZÁÉËÏÓÖÜÚ]+$/i,
4343
'nn-NO': /^[0-9A-ZÆØÅ]+$/i,
4444
'pl-PL': /^[0-9A-ZĄĆĘŚŁŃÓŻŹ]+$/i,
45-
'pt-PT': /^[0-9A-ZÃÁÀÂÇÉÊÍÕÓÔÚÜ]+$/i,
45+
'pt-PT': /^[0-9A-ZÃÁÀÂÄÇÉÊËÍÏÕÓÔÖÚÜ]+$/i,
4646
'ru-RU': /^[0-9А-ЯЁ]+$/i,
4747
'sl-SI': /^[0-9A-ZČĆĐŠŽ]+$/i,
4848
'sk-SK': /^[0-9A-ZÁČĎÉÍŇÓŠŤÚÝŽĹŔĽÄÔ]+$/i,

es/lib/isMobilePhone.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import assertString from './util/assertString';
22
/* eslint-disable max-len */
33

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

es/lib/isURL.js

+11
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,17 @@ import assertString from './util/assertString';
22
import isFQDN from './isFQDN';
33
import isIP from './isIP';
44
import merge from './util/merge';
5+
/*
6+
options for isURL method
7+
8+
require_protocol - if set as true isURL will return false if protocol is not present in the URL
9+
require_valid_protocol - isURL will check if the URL's protocol is present in the protocols option
10+
protocols - valid protocols can be modified with this option
11+
require_host - if set as false isURL will not check if host is present in the URL
12+
allow_protocol_relative_urls - if set as true protocol relative URLs will be allowed
13+
14+
*/
15+
516
var default_url_options = {
617
protocols: ['http', 'https', 'ftp'],
718
require_tld: true,

es/lib/toFloat.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import assertString from './util/assertString';
1+
import isFloat from './isFloat';
22
export default function toFloat(str) {
3-
assertString(str);
3+
if (!isFloat(str)) return NaN;
44
return parseFloat(str);
55
}

lib/alpha.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ var alpha = {
1919
'nn-NO': /^[A-ZÆØÅ]+$/i,
2020
'hu-HU': /^[A-ZÁÉÍÓÖŐÚÜŰ]+$/i,
2121
'pl-PL': /^[A-ZĄĆĘŚŁŃÓŻŹ]+$/i,
22-
'pt-PT': /^[A-ZÃÁÀÂÇÉÊÍÕÓÔÚÜ]+$/i,
22+
'pt-PT': /^[A-ZÃÁÀÂÄÇÉÊËÍÏÕÓÔÖÚÜ]+$/i,
2323
'ru-RU': /^[А-ЯЁ]+$/i,
2424
'sl-SI': /^[A-ZČĆĐŠŽ]+$/i,
2525
'sk-SK': /^[A-ZÁČĎÉÍŇÓŠŤÚÝŽĹŔĽÄÔ]+$/i,
@@ -49,7 +49,7 @@ var alphanumeric = {
4949
'nl-NL': /^[0-9A-ZÁÉËÏÓÖÜÚ]+$/i,
5050
'nn-NO': /^[0-9A-ZÆØÅ]+$/i,
5151
'pl-PL': /^[0-9A-ZĄĆĘŚŁŃÓŻŹ]+$/i,
52-
'pt-PT': /^[0-9A-ZÃÁÀÂÇÉÊÍÕÓÔÚÜ]+$/i,
52+
'pt-PT': /^[0-9A-ZÃÁÀÂÄÇÉÊËÍÏÕÓÔÖÚÜ]+$/i,
5353
'ru-RU': /^[0-9А-ЯЁ]+$/i,
5454
'sl-SI': /^[0-9A-ZČĆĐŠŽ]+$/i,
5555
'sk-SK': /^[0-9A-ZÁČĎÉÍŇÓŠŤÚÝŽĹŔĽÄÔ]+$/i,

lib/isMobilePhone.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { de
1212

1313
/* eslint-disable max-len */
1414
var phones = {
15+
'am-AM': /^(\+?374|0)((10|[9|7][0-9])\d{6}$|[2-4]\d{7}$)/,
1516
'ar-AE': /^((\+?971)|0)?5[024568]\d{7}$/,
1617
'ar-BH': /^(\+?973)?(3|6)\d{7}$/,
1718
'ar-DZ': /^(\+?213|0)(5|6|7)\d{8}$/,
@@ -34,7 +35,8 @@ var phones = {
3435
'en-GB': /^(\+?44|0)7\d{9}$/,
3536
'en-GG': /^(\+?44|0)1481\d{6}$/,
3637
'en-GH': /^(\+233|0)(20|50|24|54|27|57|26|56|23|28)\d{7}$/,
37-
'en-HK': /^(\+?852\-?)?[456789]\d{3}\-?\d{4}$/,
38+
'en-HK': /^(\+?852[-\s]?)?[456789]\d{3}[-\s]?\d{4}$/,
39+
'en-MO': /^(\+?853[-\s]?)?[6]\d{3}[-\s]?\d{4}$/,
3840
'en-IE': /^(\+?353|0)8[356789]\d{7}$/,
3941
'en-IN': /^(\+?91|0)?[6789]\d{9}$/,
4042
'en-KE': /^(\+?254|0)(7|1)\d{8}$/,
@@ -104,6 +106,7 @@ var phones = {
104106
phones['en-CA'] = phones['en-US'];
105107
phones['fr-BE'] = phones['nl-BE'];
106108
phones['zh-HK'] = phones['en-HK'];
109+
phones['zh-MO'] = phones['en-MO'];
107110

108111
function isMobilePhone(str, locale, options) {
109112
(0, _assertString.default)(str);

lib/toFloat.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@ Object.defineProperty(exports, "__esModule", {
55
});
66
exports.default = toFloat;
77

8-
var _assertString = _interopRequireDefault(require("./util/assertString"));
8+
var _isFloat = _interopRequireDefault(require("./isFloat"));
99

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

1212
function toFloat(str) {
13-
(0, _assertString.default)(str);
13+
if (!(0, _isFloat.default)(str)) return NaN;
1414
return parseFloat(str);
1515
}
1616

src/lib/toFloat.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
import assertString from './util/assertString';
1+
import isFloat from './isFloat';
22

33
export default function toFloat(str) {
4-
assertString(str);
4+
if (!isFloat(str)) return NaN;
5+
56
return parseFloat(str);
67
}

test/sanitizers.js

+1
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ describe('Sanitizers', () => {
137137
'2.': 2.0,
138138
'-2.5': -2.5,
139139
'.5': 0.5,
140+
'2020-01-06T14:31:00.135Z': NaN,
140141
foo: NaN,
141142
},
142143
});

test/validators.js

+3
Original file line numberDiff line numberDiff line change
@@ -2335,6 +2335,8 @@ describe('Validators', () => {
23352335
'',
23362336
'.',
23372337
'foo',
2338+
'20.foo',
2339+
'2020-01-06T14:31:00.135Z',
23382340
],
23392341
});
23402342

@@ -3187,6 +3189,7 @@ describe('Validators', () => {
31873189
'101',
31883190
'foo',
31893191
'',
3192+
'2020-01-06T14:31:00.135Z',
31903193
],
31913194
});
31923195
});

0 commit comments

Comments
 (0)