释放双眼,带上耳机,听听看~!
type NumericValue = number | bigint | string;
export class NumberUtils {
static UINT8_MAX = BigInt('255');
static UINT16_MAX = BigInt('65535');
static UINT32_MAX = BigInt('4294967295');
static UINT64_MAX = BigInt('18446744073709551615');
static UINT_MIN = BigInt('0');
static INT32_MIN = BigInt('-2147483648');
static INT32_MAX = BigInt('2147483647');
static INT64_MAX = BigInt('9223372036854775807');
static INT64_MIN = BigInt('-9223372036854775808');
static MIN_FLOAT = -3.4e+38;
static MAX_FLOAT = 3.4e+38;
static isNumber(val: number): boolean {
return typeof (val) === 'number';
}
static isInteger(val: NumericValue): boolean {
if (typeof val === 'bigint') {
return true;
}
if (typeof val === 'number') {
return Number.isInteger(val);
}
try {
BigInt(val);
return true;
} catch {
return false;
}
}
private static isUintN<T extends NumericValue>(val: T, max: NumericValue): boolean {
try {
const bigIntVal = BigInt(val);
const maxBigInt = BigInt(max);
return this.isInteger(bigIntVal) && bigIntVal >= this.UINT_MIN && bigIntVal <= maxBigInt;
} catch {
return false;
}
}
static isUint8(val: NumericValue): boolean {
return this.isUintN(val, this.UINT8_MAX);
}
static isUint16(val: NumericValue): boolean {
return this.isUintN(val, this.UINT16_MAX);
}
static isUint32(val: NumericValue): boolean {
return this.isUintN(val, this.UINT32_MAX);
}
static isUint64(val: NumericValue): boolean {
return this.isUintN(val, this.UINT64_MAX);
}
static isInt32(val: NumericValue): boolean {
try {
const bigIntVal: bigint = BigInt(val);
return this.isInteger(bigIntVal)
&& this.INT32_MIN <= bigIntVal && bigIntVal <= this.INT32_MAX;
} catch (error) {
return false;
}
}
static isInt64(val: NumericValue): boolean {
try {
const bigIntVal: bigint = BigInt(val);
return this.isInteger(bigIntVal)
&& this.INT64_MIN <= bigIntVal && bigIntVal <= this.INT64_MAX;
} catch (error) {
return false;
}
}
static isDouble(val: number): boolean {
return this.isNumber(val) && !Number.isInteger(val) && (!isNaN(val));
}
static isFloat(val: number): boolean {
return this.isDouble(val) && (val >= this.MIN_FLOAT && val <= this.MAX_FLOAT);
}
static isFloatOrDouble(val: number): boolean {
return this.isFloat(val) || this.isDouble(val);
}
};
内容投诉