Skip to content

Commit

Permalink
Add support for greater/less than in isFloat(). Closes #544.
Browse files Browse the repository at this point in the history
  • Loading branch information
hkwu committed Oct 5, 2016
1 parent 8c59fd1 commit cb09637
Show file tree
Hide file tree
Showing 7 changed files with 171 additions and 14 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

- New locales
([#585](https://github.com/chriso/validator.js/pull/585))
- Added support for greater or less than in `isFloat()`
([#544](https://github.com/chriso/validator.js/issues/544))

#### 6.0.0

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ Passing anything other than a string is an error.
- **isEmail(str [, options])** - check if the string is an email. `options` is an object which defaults to `{ allow_display_name: false, allow_utf8_local_part: true, require_tld: true }`. If `allow_display_name` is set to true, the validator will also match `Display Name <email-address>`. If `allow_utf8_local_part` is set to false, the validator will not allow any non-English UTF8 character in email address' local part. If `require_tld` is set to false, e-mail addresses without having TLD in their domain will also be matched.
- **isEmpty(str)** - check if the string has a length of zero.
- **isFQDN(str [, options])** - check if the string is a fully qualified domain name (e.g. domain.com). `options` is an object which defaults to `{ require_tld: true, allow_underscores: false, allow_trailing_dot: false }`.
- **isFloat(str [, options])** - check if the string is a float. `options` is an object which can contain the keys `min` and/or `max` to validate the float is within boundaries (e.g. `{ min: 7.22, max: 9.55 }`).
- **isFloat(str [, options])** - check if the string is a float. `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 }`). `min` and `max` are equivalent to 'greater or equal' and 'less or equal', respectively while `gt` and `lt` are their strict counterparts.
- **isFullWidth(str)** - check if the string contains any full-width chars.
- **isHalfWidth(str)** - check if the string contains any half-width chars.
- **isHexColor(str)** - check if the string is a hexadecimal color.
Expand Down
2 changes: 1 addition & 1 deletion lib/isFloat.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@ function isFloat(str, options) {
if (str === '' || str === '.') {
return false;
}
return float.test(str) && (!options.hasOwnProperty('min') || str >= options.min) && (!options.hasOwnProperty('max') || str <= options.max);
return float.test(str) && (!options.hasOwnProperty('min') || str >= options.min) && (!options.hasOwnProperty('max') || str <= options.max) && (!options.hasOwnProperty('lt') || str < options.lt) && (!options.hasOwnProperty('gt') || str > options.gt);
}
module.exports = exports['default'];
4 changes: 3 additions & 1 deletion src/lib/isFloat.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,7 @@ export default function isFloat(str, options) {
}
return float.test(str) &&
(!options.hasOwnProperty('min') || str >= options.min) &&
(!options.hasOwnProperty('max') || str <= options.max);
(!options.hasOwnProperty('max') || str <= options.max) &&
(!options.hasOwnProperty('lt') || str < options.lt) &&
(!options.hasOwnProperty('gt') || str > options.gt);
}
40 changes: 40 additions & 0 deletions test/validators.js
Original file line number Diff line number Diff line change
Expand Up @@ -1252,6 +1252,46 @@ describe('Validators', function () {
'5',
],
});
test({
validator: 'isFloat',
args: [{
gt: -5.5,
lt: 10,
}],
valid: [
'9.9',
'1.0',
'0',
'-1',
'7',
'-5.4',
],
invalid: [
'10',
'-5.5',
'a',
'-20.3',
'20e3',
'10.00001',
],
});
test({
validator: 'isFloat',
args: [{
min: -5.5,
max: 10,
gt: -5.5,
lt: 10,
}],
valid: [
'9.99999',
'-5.499999',
],
invalid: [
'10',
'-5.5',
],
});
});

it('should validate hexadecimal strings', function () {
Expand Down
133 changes: 123 additions & 10 deletions validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,122 @@
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) {
return typeof obj;
} : function (obj) {
return obj && typeof Symbol === "function" && obj.constructor === Symbol ? "symbol" : typeof obj;
return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj;
};

var asyncGenerator = function () {
function AwaitValue(value) {
this.value = value;
}

function AsyncGenerator(gen) {
var front, back;

function send(key, arg) {
return new Promise(function (resolve, reject) {
var request = {
key: key,
arg: arg,
resolve: resolve,
reject: reject,
next: null
};

if (back) {
back = back.next = request;
} else {
front = back = request;
resume(key, arg);
}
});
}

function resume(key, arg) {
try {
var result = gen[key](arg);
var value = result.value;

if (value instanceof AwaitValue) {
Promise.resolve(value.value).then(function (arg) {
resume("next", arg);
}, function (arg) {
resume("throw", arg);
});
} else {
settle(result.done ? "return" : "normal", result.value);
}
} catch (err) {
settle("throw", err);
}
}

function settle(type, value) {
switch (type) {
case "return":
front.resolve({
value: value,
done: true
});
break;

case "throw":
front.reject(value);
break;

default:
front.resolve({
value: value,
done: false
});
break;
}

front = front.next;

if (front) {
resume(front.key, front.arg);
} else {
back = null;
}
}

this._invoke = send;

if (typeof gen.return !== "function") {
this.return = undefined;
}
}

if (typeof Symbol === "function" && Symbol.asyncIterator) {
AsyncGenerator.prototype[Symbol.asyncIterator] = function () {
return this;
};
}

AsyncGenerator.prototype.next = function (arg) {
return this._invoke("next", arg);
};

AsyncGenerator.prototype.throw = function (arg) {
return this._invoke("throw", arg);
};

AsyncGenerator.prototype.return = function (arg) {
return this._invoke("return", arg);
};

return {
wrap: function (fn) {
return function () {
return new AsyncGenerator(fn.apply(this, arguments));
};
},
await: function (value) {
return new AwaitValue(value);
}
};
}();

function toString(input) {
if ((typeof input === 'undefined' ? 'undefined' : _typeof(input)) === 'object' && input !== null) {
if (typeof input.toString === 'function') {
Expand Down Expand Up @@ -94,7 +207,7 @@
}

function merge() {
var obj = arguments.length <= 0 || arguments[0] === undefined ? {} : arguments[0];
var obj = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
var defaults = arguments[1];

for (var key in defaults) {
Expand Down Expand Up @@ -227,7 +340,7 @@
var ipv6Block = /^[0-9A-F]{1,4}$/i;

function isIP(str) {
var version = arguments.length <= 1 || arguments[1] === undefined ? '' : arguments[1];
var version = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : '';

assertString(str);
version = String(version);
Expand Down Expand Up @@ -477,7 +590,7 @@
}

function isAlpha(str) {
var locale = arguments.length <= 1 || arguments[1] === undefined ? 'en-US' : arguments[1];
var locale = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'en-US';

assertString(str);
if (locale in alpha) {
Expand All @@ -487,7 +600,7 @@
}

function isAlphanumeric(str) {
var locale = arguments.length <= 1 || arguments[1] === undefined ? 'en-US' : arguments[1];
var locale = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'en-US';

assertString(str);
if (locale in alphanumeric) {
Expand Down Expand Up @@ -583,7 +696,7 @@
if (str === '' || str === '.') {
return false;
}
return float.test(str) && (!options.hasOwnProperty('min') || str >= options.min) && (!options.hasOwnProperty('max') || str <= options.max);
return float.test(str) && (!options.hasOwnProperty('min') || str >= options.min) && (!options.hasOwnProperty('max') || str <= options.max) && (!options.hasOwnProperty('lt') || str < options.lt) && (!options.hasOwnProperty('gt') || str > options.gt);
}

var decimal = /^[-+]?([0-9]+|\.[0-9]+|[0-9]+\.[0-9]+)$/;
Expand Down Expand Up @@ -659,7 +772,7 @@
};

function isUUID(str) {
var version = arguments.length <= 1 || arguments[1] === undefined ? 'all' : arguments[1];
var version = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'all';

assertString(str);
var pattern = uuid[version];
Expand Down Expand Up @@ -767,7 +880,7 @@
}

function isAfter(str) {
var date = arguments.length <= 1 || arguments[1] === undefined ? String(new Date()) : arguments[1];
var date = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : String(new Date());

assertString(str);
var comparison = toDate(date);
Expand All @@ -776,7 +889,7 @@
}

function isBefore(str) {
var date = arguments.length <= 1 || arguments[1] === undefined ? String(new Date()) : arguments[1];
var date = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : String(new Date());

assertString(str);
var comparison = toDate(date);
Expand Down Expand Up @@ -875,7 +988,7 @@
var factor = [1, 3];

function isISBN(str) {
var version = arguments.length <= 1 || arguments[1] === undefined ? '' : arguments[1];
var version = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : '';

assertString(str);
version = String(version);
Expand Down
2 changes: 1 addition & 1 deletion validator.min.js

Large diffs are not rendered by default.

0 comments on commit cb09637

Please sign in to comment.