feat!: release v2 (#6545)

Co-authored-by: Artur <145776561+arturict@users.noreply.github.com>
Co-authored-by: arturict <arturict@users.noreply.github.com>
Co-authored-by: Thomas <thomas@ajnart.fr>
Co-authored-by: Afonso Jorge Ramos <afonsojorgeramos@gmail.com>
Co-authored-by: Danura30082 <danura30082+github@gmail.com>
Co-authored-by: Kushie <122986913+kushiemoon-dev@users.noreply.github.com>
Co-authored-by: ajnart <thomas@ajnart.fr>
Co-authored-by: ajnart <thomas@ajnart.dev>
Co-authored-by: chenjun <chenjun@foxmail.com>
Co-authored-by: Wikibear <wikibear@users.noreply.github.com>
Co-authored-by: Bergasha <bergasha@gmail.com>
Co-authored-by: leafyelin <43306196+leafyelin@users.noreply.github.com>
Co-authored-by: Lucas Araujo <lucas.lra@gmail.com>
Co-authored-by: GreenMonito <86085570+GreenMonito@users.noreply.github.com>
Co-authored-by: Nerdy-Technician <roffo@nerdy-technician.social>
Co-authored-by: Jun <34545896+jcstrong@users.noreply.github.com>
Co-authored-by: Edmond Abraham <54119952+gitedmond@users.noreply.github.com>
Co-authored-by: thrawnx <tight.bulb9384@fastmail.com>
Co-authored-by: Chris <58918656+nordada@users.noreply.github.com>
Co-authored-by: yi111 <153097222+Yi-111-a@users.noreply.github.com>
Co-authored-by: Yanis <naamaneyanis@gmail.com>
Co-authored-by: Wikibear <me@sej.de>
Co-authored-by: alessiottavo <93441886+alessiottavo@users.noreply.github.com>
This commit is contained in:
3286 files changed
+284386 -139176

No files matched your search

+55 -57
View File
@@ -16,53 +16,17 @@ export const formatNumber = (value: number, decimalPlaces: number) => {
return value.toFixed(decimalPlaces);
};
export const randomInt = (min: number, max: number) => {
return Math.floor(Math.random() * (max - min + 1) + min);
};
/**
* Number of bytes to a human-readable string using binary (KiB) suffixes.
* Does not accept floats; size in bytes should be an integer.
* Will return "NaI" and log a warning if a float is passed.
* `concat` is appended after the unit so it is omitted when the returned
* value is "NaI" or "∞". Returns "∞" if the size is too large to be
* represented in the current format.
*
* @deprecated Use `formatBytes` for single values, `formatBytesPair` for
* paired "used / total" displays, and `formatByteRate` for byte-per-second
* rates. This function is kept for backwards compatibility but should not
* be used in new code.
*/
export const humanFileSize = (size: number, concat = ""): string => {
//64bit limit for Number stops at EiB
const siRanges = ["B", "kiB", "MiB", "GiB", "TiB", "PiB", "EiB"];
if (!Number.isInteger(size)) {
console.warn(
"Invalid use of the humanFileSize function with a float, please report this and what integration this is impacting.",
);
//Not an Integer
return "NaI";
}
let count = 0;
while (count < siRanges.length) {
const tempSize = size / Math.pow(1024, count);
if (tempSize < 1024) {
return tempSize.toFixed(Math.min(count, 1)) + siRanges[count] + concat;
}
count++;
}
return "∞";
};
const BINARY_UNITS = ["B", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB"] as const;
const DECIMAL_UNITS = ["B", "KB", "MB", "GB", "TB", "PB", "EB"] as const;
export const defaultByteUnitSystem = "decimal" as const;
export type ByteUnitSystem = "binary" | "decimal";
export interface FormatBytesOptions {
/**
* Unit system to use. "binary" uses 1024 as the base and KiB/MiB/GiB/TiB suffixes,
* "decimal" uses 1000 as the base and KB/MB/GB/TB suffixes. Defaults to "binary".
* "decimal" uses 1000 as the base and KB/MB/GB/TB suffixes. Defaults to "decimal".
*/
unit?: ByteUnitSystem;
}
@@ -78,10 +42,17 @@ const pickUnitIndex = (bytes: number, base: number, lastIndex: number): number =
return index;
};
const pickRoundedUnitIndex = (value: number, base: number, lastIndex: number, fractionDigits: number): number => {
const index = pickUnitIndex(value, base, lastIndex);
if (index >= lastIndex) return index;
const scaled = value / base ** index;
return Number(scaled.toFixed(fractionDigits)) >= base ? index + 1 : index;
};
const sanitizeBytes = (bytes: number): number => (Number.isFinite(bytes) && bytes > 0 ? bytes : 0);
const resolveUnitConfig = (options: FormatBytesOptions) => {
const { unit = "binary" } = options;
const { unit = defaultByteUnitSystem } = options;
return {
units: unit === "binary" ? BINARY_UNITS : DECIMAL_UNITS,
base: unit === "binary" ? 1024 : 1000,
@@ -92,45 +63,45 @@ const resolveUnitConfig = (options: FormatBytesOptions) => {
* Format a byte value as a human-readable string with a unit suffix.
*
* Picks the largest unit that keeps the scaled value below the base. Use
* `formatBytesPair` when two related values (e.g. used and available) should
* `formatBytesPair` when two related values (e.g. used and total) should
* share a common unit.
*
* @example
* formatBytes(0); // "0.0 B"
* formatBytes(1024); // "1.0 KiB"
* formatBytes(985828802560); // "918.1 GiB"
* formatBytes(985828802560, { unit: "decimal" }); // "985.8 GB"
* formatBytes(1000); // "1.0 KB"
* formatBytes(985828802560); // "985.8 GB"
* formatBytes(985828802560, { unit: "binary" }); // "918.1 GiB"
*/
export const formatBytes = (bytes: number, options: FormatBytesOptions = {}): string => {
const { units, base } = resolveUnitConfig(options);
const safe = sanitizeBytes(bytes);
const index = pickUnitIndex(safe, base, units.length - 1);
const index = pickRoundedUnitIndex(safe, base, units.length - 1, 1);
const scaled = safe / base ** index;
return `${scaled.toFixed(1)} ${units[index]}`;
};
/**
* Format two related byte values (typically `used` and `available`) with a
* shared unit. The unit is picked from the sum of the two values so the
* combined display stays consistent.
* Format two related byte values (typically `used` and `total`) with a shared
* unit. The unit is picked from the larger displayed value so the pair stays
* consistent without promoting both values before either crosses a boundary.
*
* @example
* formatBytesPair(985828802560, 2858736793190);
* // { used: "0.9 TiB", available: "2.6 TiB" }
* // { used: "1.0 TB", total: "2.9 TB" }
*/
export const formatBytesPair = (
used: number,
available: number,
total: number,
options: FormatBytesOptions = {},
): { used: string; available: string } => {
): { used: string; total: string } => {
const { units, base } = resolveUnitConfig(options);
const safeUsed = sanitizeBytes(used);
const safeAvailable = sanitizeBytes(available);
const index = pickUnitIndex(safeUsed + safeAvailable, base, units.length - 1);
const safeTotal = sanitizeBytes(total);
const index = pickRoundedUnitIndex(Math.max(safeUsed, safeTotal), base, units.length - 1, 1);
const suffix = units[index];
return {
used: `${(safeUsed / base ** index).toFixed(1)} ${suffix}`,
available: `${(safeAvailable / base ** index).toFixed(1)} ${suffix}`,
total: `${(safeTotal / base ** index).toFixed(1)} ${suffix}`,
};
};
@@ -141,13 +112,40 @@ export const formatBytesPair = (
*
* @example
* formatByteRate(0); // "0.0 B/s"
* formatByteRate(1024); // "1.0 KiB/s"
* formatByteRate(985828802560, { unit: "decimal" }); // "985.8 GB/s"
* formatByteRate(1000); // "1.0 KB/s"
* formatByteRate(985828802560, { unit: "binary" }); // "918.1 GiB/s"
*/
export const formatByteRate = (bytes: number, options: FormatBytesOptions = {}): string =>
`${formatBytes(bytes, options)}/s`;
const BIT_RATE_UNITS = ["b/s", "Kb/s", "Mb/s", "Gb/s", "Tb/s", "Pb/s", "Eb/s"] as const;
export interface FormatBitRateOptions {
/** Maximum number of fractional digits. Trailing zeroes are omitted. Defaults to 1. */
maximumFractionDigits?: number;
}
/**
* Format a bit-per-second rate using decimal SI units.
*
* Inputs are always bits per second. Callers whose upstream contract uses
* kilobits must normalize explicitly before calling this function.
*
* @example
* formatBitRate(999); // "999 b/s"
* formatBitRate(1_000); // "1 Kb/s"
* formatBitRate(1_500_000); // "1.5 Mb/s"
*/
export const formatBitRate = (bitsPerSecond: number, options: FormatBitRateOptions = {}): string => {
const safeRate = Number.isFinite(bitsPerSecond) && bitsPerSecond > 0 ? bitsPerSecond : 0;
const requestedDigits = options.maximumFractionDigits ?? 1;
const maximumFractionDigits = Math.max(0, Math.min(20, Math.trunc(requestedDigits)));
const index = pickRoundedUnitIndex(safeRate, 1000, BIT_RATE_UNITS.length - 1, maximumFractionDigits);
const scaled = safeRate / 1000 ** index;
const formatted = Number(scaled.toFixed(maximumFractionDigits)).toString();
return `${formatted} ${BIT_RATE_UNITS[index]}`;
};
const IMPERIAL_MULTIPLIER = 1.609344;
export const metricToImperial = (metricValue: number) => metricValue / IMPERIAL_MULTIPLIER;
export const imperialToMetric = (imperialValue: number) => imperialValue * IMPERIAL_MULTIPLIER;