util: optimize isUInt32 and isInt32.

This commit is contained in:
Christopher Jeffrey 2017-06-26 12:41:00 -07:00
parent edd095bfae
commit e02d645c07
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD
2 changed files with 17 additions and 17 deletions

View File

@ -85,17 +85,6 @@ util.concat = function concat(a, b) {
return data; return data;
}; };
/**
* Test whether a string is base58 (note that you
* may get a false positive on a hex string).
* @param {String?} obj
* @returns {Boolean}
*/
util.isBase58 = function isBase58(obj) {
return typeof obj === 'string' && /^[1-9a-zA-Z]+$/.test(obj);
};
/** /**
* Return hrtime (shim for browser). * Return hrtime (shim for browser).
* @param {Array} time * @param {Array} time
@ -125,6 +114,17 @@ util.hrtime = function hrtime(time) {
return process.hrtime(); return process.hrtime();
}; };
/**
* Test whether a string is base58 (note that you
* may get a false positive on a hex string).
* @param {String?} obj
* @returns {Boolean}
*/
util.isBase58 = function isBase58(obj) {
return typeof obj === 'string' && /^[1-9a-zA-Z]+$/.test(obj);
};
/** /**
* Test whether a string is hex (length must be even). * Test whether a string is hex (length must be even).
* Note that this _could_ await a false positive on * Note that this _could_ await a false positive on
@ -279,7 +279,7 @@ util.isInt = function isInt(value) {
*/ */
util.isInt8 = function isInt8(value) { util.isInt8 = function isInt8(value) {
return util.isInt(value) && value >= -0x80 && value <= 0x7f; return (value | 0) === value && value >= -0x80 && value <= 0x7f;
}; };
/** /**
@ -289,7 +289,7 @@ util.isInt8 = function isInt8(value) {
*/ */
util.isUInt8 = function isUInt8(value) { util.isUInt8 = function isUInt8(value) {
return util.isInt(value) && value >= 0 && value <= 0xff; return (value >>> 0) === value && value >= 0 && value <= 0xff;
}; };
/** /**
@ -299,7 +299,7 @@ util.isUInt8 = function isUInt8(value) {
*/ */
util.isInt32 = function isInt32(value) { util.isInt32 = function isInt32(value) {
return util.isInt(value) && value >= -0x80000000 && value <= 0x7fffffff; return (value | 0) === value;
}; };
/** /**
@ -309,7 +309,7 @@ util.isInt32 = function isInt32(value) {
*/ */
util.isUInt32 = function isUInt32(value) { util.isUInt32 = function isUInt32(value) {
return util.isInt(value) && value >= 0 && value <= 0xffffffff; return (value >>> 0) === value;
}; };
/** /**

View File

@ -208,7 +208,7 @@ Validator.prototype.u32 = function u32(key, fallback) {
if (value === null) if (value === null)
return fallback; return fallback;
if (value % 1 !== 0 || value < 0 || value > 0xffffffff) if ((value >>> 0) !== value)
throw new ValidationError(fmt(key) + ' must be a uint32.'); throw new ValidationError(fmt(key) + ' must be a uint32.');
return value; return value;
@ -252,7 +252,7 @@ Validator.prototype.i32 = function i32(key, fallback) {
if (value === null) if (value === null)
return fallback; return fallback;
if (value % 1 !== 0 || value < -0x80000000 || value > 0x7fffffff) if ((value | 0) !== value)
throw new ValidationError(fmt(key) + ' must be an int32.'); throw new ValidationError(fmt(key) + ' must be an int32.');
return value; return value;