mirror of
https://github.com/Zakkaus/doona.git
synced 2026-12-06 08:02:04 +00:00
Share confirmation dialogs, formatters, cards and menus across features. Make failed reads retryable, correct cancellation behavior and standardize terminology and status text.
40 lines
1.3 KiB
TypeScript
40 lines
1.3 KiB
TypeScript
type U64 = string | bigint | null;
|
|
const MAX = (1n << 64n) - 1n;
|
|
|
|
export function parseU64(s: string | null): bigint | null {
|
|
if (s === null) return null;
|
|
if (s.length > 20 || !/^(0|[1-9][0-9]*)$/.test(s)) return null;
|
|
const value = BigInt(s);
|
|
if (value > MAX) return null;
|
|
return value;
|
|
}
|
|
|
|
function valueOf(value: U64): bigint | null {
|
|
if (typeof value !== 'bigint') return parseU64(value);
|
|
if (value < 0n) throw new RangeError('Unsigned value must not be negative');
|
|
return value;
|
|
}
|
|
|
|
/** Unknown counters remain unknown, including in aggregates. */
|
|
export function addU64(...values: U64[]): bigint | null {
|
|
let total = 0n;
|
|
for (const input of values) {
|
|
const value = valueOf(input);
|
|
if (value === null) return null;
|
|
total += value;
|
|
}
|
|
return total;
|
|
}
|
|
|
|
/** Only the bounded percentage is converted to Number. */
|
|
export function pctU64(part: U64, whole: U64): number | null {
|
|
const p = valueOf(part),
|
|
w = valueOf(whole);
|
|
if (p === null || w === null) return null;
|
|
if (w === 0n) return 0;
|
|
return Number(((p > w ? w : p) * 10000n + w / 2n) / w) / 100;
|
|
}
|
|
|
|
/** Round displayed milliseconds to 0.1 below 10 ms and whole milliseconds otherwise. */
|
|
export const millis = (value: number): number => (value < 10 ? Math.round(value * 10) / 10 : Math.round(value));
|