better-bytes
Advanced tools
Comparing version 1.2.0 to 1.2.1
@@ -41,2 +41,3 @@ /** | ||
declare const PARSE_REG_EXP: RegExp; | ||
declare const NUMBER_REG: RegExp; | ||
type ParseOptions = { | ||
@@ -60,2 +61,2 @@ /** | ||
export { type FormatOptions, KILOBYTE_BASE, KILOBYTE_UNITS, KILO_BINARY_BYTE_BASE, KILO_BINARY_BYTE_UNITS, PARSE_REG_EXP, type ParseOptions, type StandardType, format, parse }; | ||
export { type FormatOptions, KILOBYTE_BASE, KILOBYTE_UNITS, KILO_BINARY_BYTE_BASE, KILO_BINARY_BYTE_UNITS, NUMBER_REG, PARSE_REG_EXP, type ParseOptions, type StandardType, format, parse }; |
@@ -26,2 +26,3 @@ var __defProp = Object.defineProperty; | ||
KILO_BINARY_BYTE_UNITS: () => KILO_BINARY_BYTE_UNITS, | ||
NUMBER_REG: () => NUMBER_REG, | ||
PARSE_REG_EXP: () => PARSE_REG_EXP, | ||
@@ -53,3 +54,3 @@ format: () => format, | ||
if (typeof value === "number" && value > Number.MAX_SAFE_INTEGER) { | ||
value = BigInt(value); | ||
value = BigInt(Math.floor(value)); | ||
} else if (typeof value === "bigint" && value <= BigInt(Number.MAX_SAFE_INTEGER)) { | ||
@@ -70,3 +71,4 @@ value = Number(value); | ||
} | ||
var PARSE_REG_EXP = /^((-|\+)?(\d+(?:\.\d+)?)) *((k|m|g|t|p|e|z|y)?i?b)$/i; | ||
var PARSE_REG_EXP = /^((\+)?(\d+(?:\.\d+)?)) *((k|m|g|t|p|e|z|y)?i?b)$/i; | ||
var NUMBER_REG = /^(\+)?\d+(\.\d+)?$/; | ||
function parse(data, options = {}) { | ||
@@ -76,5 +78,12 @@ if (typeof data !== "string") { | ||
} | ||
if (NUMBER_REG.test(data)) { | ||
const floatValue2 = Number.parseFloat(data); | ||
if (!Number.isFinite(floatValue2) || floatValue2 < 0) { | ||
return null; | ||
} | ||
return Math.floor(floatValue2); | ||
} | ||
const { forceKilobinary = false } = options; | ||
const results = PARSE_REG_EXP.exec(data); | ||
const floatValue = parseFloat(results?.[1] || data); | ||
const floatValue = Number.parseFloat(results?.[1] || data); | ||
const unit = results?.[4]?.toLowerCase(); | ||
@@ -84,5 +93,2 @@ if (!Number.isFinite(floatValue) || floatValue < 0) { | ||
} | ||
if (unit === "") { | ||
return Math.floor(floatValue); | ||
} | ||
let i = 0; | ||
@@ -109,4 +115,4 @@ let standard; | ||
const nextResult = typeof result === "bigint" ? result * BigInt(base) : result * base; | ||
if (nextResult > Number.MAX_SAFE_INTEGER) { | ||
result = BigInt(result) * BigInt(base); | ||
if (typeof result === "number" && nextResult > Number.MAX_SAFE_INTEGER) { | ||
result = BigInt(Math.floor(Number(result))) * BigInt(base); | ||
} else { | ||
@@ -127,2 +133,3 @@ result = nextResult; | ||
KILO_BINARY_BYTE_UNITS, | ||
NUMBER_REG, | ||
PARSE_REG_EXP, | ||
@@ -129,0 +136,0 @@ format, |
{ | ||
"name": "better-bytes", | ||
"version": "1.2.0", | ||
"version": "1.2.1", | ||
"description": "Better byte base conversion. Supports two base conversions: kilo binary byte (2^10) and kilobyte (10^3).\n更好的字节进制换算。支持 千位二进制字节(2^10) 和 千字节(10^3) 两种进制换算", | ||
@@ -5,0 +5,0 @@ "author": "CaoMeiYouRen", |
@@ -59,3 +59,5 @@ <h1 align="center">better-bytes </h1> | ||
format(-1) //Error: 'Data must be greater than or equal to 0' | ||
format(-1) // Error: 'Data must be greater than or equal to 0' | ||
format('invalid') // Error: 'Data must be a number or bigint' | ||
format(NaN) // Error: 'Data must be finite' | ||
@@ -74,2 +76,5 @@ parse('1KiB') // 1024 | ||
parse('1024') // 1024 | ||
parse('1000.5') // 1000 | ||
parse('1.123456789012345GiB') // 1206302541 | ||
@@ -81,4 +86,8 @@ parse('1.123456789012345GB') // 1123456789 | ||
parse('+1KiB') // 1024 | ||
parse('-1KiB') // null | ||
parse('1XB') // null | ||
parse('invalid') // null | ||
parse('Infinity') // null | ||
parse(123) // Error: Data must be a string | ||
@@ -85,0 +94,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
37445
289
204