-
Notifications
You must be signed in to change notification settings - Fork 3
/
isFloat.js
31 lines (29 loc) · 1.16 KB
/
isFloat.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
// https://github.com/validatorjs/validator.js/blob/master/src/lib/isFloat.js
/**
* Check if the input string is a float.
*
* @param {string} str the string input.
* @param {Object} options options is an object which can contain the keys min, max, gt, and/or lt to validate the float is within boundaries (e.g. { min: 7.22, max: 9.55 }) it also has locale as an option.
* @return {boolean} the result of the string input indicating whether or not it's a boolean value.
*/
function isFloat(str, options) {
assertString_(str);
options = options || {};
const float = new RegExp(
`^(?:[-+])?(?:[0-9]+)?(?:\\${
options.locale ? decimal[options.locale] : "."
}[0-9]*)?(?:[eE][\\+\\-]?(?:[0-9]+))?$`
);
if (str === "" || str === "." || str === "-" || str === "+") {
return false;
}
const value = parseFloat(str.replace(",", "."));
return (
float.test(str) &&
(!options.hasOwnProperty("min") || value >= options.min) &&
(!options.hasOwnProperty("max") || value <= options.max) &&
(!options.hasOwnProperty("lt") || value < options.lt) &&
(!options.hasOwnProperty("gt") || value > options.gt)
);
}
const locales = Object.keys(decimal);