util: optimize isUInt32 and isInt32.
This commit is contained in:
parent
edd095bfae
commit
e02d645c07
@ -85,17 +85,6 @@ util.concat = function concat(a, b) {
|
||||
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).
|
||||
* @param {Array} time
|
||||
@ -125,6 +114,17 @@ util.hrtime = function hrtime(time) {
|
||||
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).
|
||||
* Note that this _could_ await a false positive on
|
||||
@ -279,7 +279,7 @@ util.isInt = function isInt(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) {
|
||||
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) {
|
||||
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) {
|
||||
return util.isInt(value) && value >= 0 && value <= 0xffffffff;
|
||||
return (value >>> 0) === value;
|
||||
};
|
||||
|
||||
/**
|
||||
|
||||
@ -208,7 +208,7 @@ Validator.prototype.u32 = function u32(key, fallback) {
|
||||
if (value === null)
|
||||
return fallback;
|
||||
|
||||
if (value % 1 !== 0 || value < 0 || value > 0xffffffff)
|
||||
if ((value >>> 0) !== value)
|
||||
throw new ValidationError(fmt(key) + ' must be a uint32.');
|
||||
|
||||
return value;
|
||||
@ -252,7 +252,7 @@ Validator.prototype.i32 = function i32(key, fallback) {
|
||||
if (value === null)
|
||||
return fallback;
|
||||
|
||||
if (value % 1 !== 0 || value < -0x80000000 || value > 0x7fffffff)
|
||||
if ((value | 0) !== value)
|
||||
throw new ValidationError(fmt(key) + ' must be an int32.');
|
||||
|
||||
return value;
|
||||
|
||||
Loading…
Reference in New Issue
Block a user