-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Clean up and type number formatting APIs #20334
Changes from 1 commit
7b450ce
0e06f2c
7756e36
1bade22
d254625
85a6912
3ca2dcb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -193,11 +193,6 @@ declare module 'cockpit' { | |
|
||
/* === String helpers ======================== */ | ||
|
||
type FormatOptions = { | ||
precision?: number; | ||
separate?: boolean; | ||
}; | ||
|
||
function message(problem: string | JsonObject): string; | ||
|
||
function gettext(message: string): string; | ||
|
@@ -206,11 +201,21 @@ declare module 'cockpit' { | |
function ngettext(context: string, message1: string, messageN: string, n: number): string; | ||
|
||
function format(format_string: string, ...args: unknown[]): string; | ||
function format_number(n: number, precision?: number): string | ||
function format_bytes(n: number, factor?: 1000 | 1024, options?: FormatOptions & { separate?: false }): string; | ||
function format_bytes(n: number, factor: 1000 | 1024, options: FormatOptions & { separate: true }): string[]; | ||
function format_bytes_per_sec(n: number, factor?: 1000 | 1024, options?: FormatOptions & { separate?: false }): string; | ||
function format_bytes_per_sec(n: number, factor: 1000 | 1024, options: FormatOptions & { separate: true }): string[]; | ||
function format_bits_per_sec(n: number, factor?: 1000 | 1024, options?: FormatOptions & { separate?: false }): string; | ||
function format_bits_per_sec(n: number, factor: 1000 | 1024, options: FormatOptions & { separate: true }): string[]; | ||
|
||
/* === Number formatting ===================== */ | ||
|
||
type FormatOptions = { | ||
precision?: number; | ||
base2?: boolean; | ||
}; | ||
type MaybeNumber = number | null | undefined; | ||
|
||
function format_number(n: MaybeNumber, precision?: number): string | ||
function format_bytes(n: MaybeNumber, options?: FormatOptions): string; | ||
function format_bytes_per_sec(n: MaybeNumber, options?: FormatOptions): string; | ||
function format_bits_per_sec(n: MaybeNumber, options?: FormatOptions & { base2?: false }): string; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just to make sure I understand this: The There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm pretty sure that enabling base2 will break this (ie: return just the number) in exactly the same way as passing 1024 as factor would. That particular function lacks base2 units (ie: no Mibps or so)... There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Right, and I don't think we ever supported that actually. |
||
|
||
/** @deprecated */ function format_bytes(n: MaybeNumber, factor: unknown, options?: object | boolean): string | string[]; | ||
/** @deprecated */ function format_bytes_per_sec(n: MaybeNumber, factor: unknown, options?: object | boolean): string | string[]; | ||
/** @deprecated */ function format_bits_per_sec(n: MaybeNumber, factor: unknown, options?: object | boolean): string | string[]; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1483,10 +1483,24 @@ function factory() { | |
}); | ||
}; | ||
|
||
function format_units(number, suffixes, factor, options) { | ||
// backwards compat: "options" argument position used to be a boolean flag "separate" | ||
if (!is_object(options)) | ||
options = { separate: options }; | ||
let deprecated_format_warned = false; | ||
function format_units(suffixes, number, second_arg, third_arg) { | ||
let options = second_arg; | ||
let factor = options?.base2 ? 1024 : 1000; | ||
|
||
// compat API: we used to accept 'factor' as a separate second arg | ||
if (third_arg || (second_arg && !is_object(second_arg))) { | ||
martinpitt marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if (!deprecated_format_warned) { | ||
console.warn(`cockpit.format_{bytes,bits}[_per_sec](..., ${second_arg}, ${third_arg}) is deprecated.`); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When do we remove it, should we include that? Like in 10 releases this API is gone? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm OK with next release already... |
||
deprecated_format_warned = true; | ||
} | ||
|
||
factor = second_arg || 1000; | ||
options = third_arg; | ||
// double backwards compat: "options" argument position used to be a boolean flag "separate" | ||
if (!is_object(options)) | ||
options = { separate: options }; | ||
} | ||
|
||
let suffix = null; | ||
|
||
|
@@ -1525,15 +1539,15 @@ function factory() { | |
} | ||
} | ||
|
||
const string_representation = cockpit.format_number(number, options.precision); | ||
const string_representation = cockpit.format_number(number, options?.precision); | ||
let ret; | ||
|
||
if (string_representation && suffix) | ||
ret = [string_representation, suffix]; | ||
else | ||
ret = [string_representation]; | ||
|
||
if (!options.separate) | ||
if (!options?.separate) | ||
ret = ret.join(" "); | ||
|
||
return ret; | ||
|
@@ -1544,31 +1558,25 @@ function factory() { | |
1024: [null, "KiB", "MiB", "GiB", "TiB", "PiB", "EiB", "ZiB"] | ||
}; | ||
|
||
cockpit.format_bytes = function format_bytes(number, factor, options) { | ||
if (factor === undefined) | ||
factor = 1000; | ||
return format_units(number, byte_suffixes, factor, options); | ||
cockpit.format_bytes = function format_bytes(number, ...args) { | ||
return format_units(byte_suffixes, number, ...args); | ||
}; | ||
|
||
const byte_sec_suffixes = { | ||
1000: ["B/s", "kB/s", "MB/s", "GB/s", "TB/s", "PB/s", "EB/s", "ZB/s"], | ||
1024: ["B/s", "KiB/s", "MiB/s", "GiB/s", "TiB/s", "PiB/s", "EiB/s", "ZiB/s"] | ||
}; | ||
|
||
cockpit.format_bytes_per_sec = function format_bytes_per_sec(number, factor, options) { | ||
if (factor === undefined) | ||
factor = 1000; | ||
return format_units(number, byte_sec_suffixes, factor, options); | ||
cockpit.format_bytes_per_sec = function format_bytes_per_sec(number, ...args) { | ||
return format_units(byte_sec_suffixes, number, ...args); | ||
}; | ||
|
||
const bit_suffixes = { | ||
1000: ["bps", "Kbps", "Mbps", "Gbps", "Tbps", "Pbps", "Ebps", "Zbps"] | ||
}; | ||
|
||
cockpit.format_bits_per_sec = function format_bits_per_sec(number, factor, options) { | ||
if (factor === undefined) | ||
factor = 1000; | ||
return format_units(number, bit_suffixes, factor, options); | ||
cockpit.format_bits_per_sec = function format_bits_per_sec(number, ...args) { | ||
return format_units(bit_suffixes, number, ...args); | ||
}; | ||
|
||
/* --------------------------------------------------------------------- | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmmm, so the argument here is. if I wanted to provide an
options
argument, I'd have to figure out the defaultfactor
which would be error prone?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The new API basically has two possibilities: SI units (default, old
factor=1000
) and IEC units (oldfactor=1024
).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jelly With the "old" API you could specify
undefined
for the factor, in which case it would be the default "1000". But indeed this is error prone -- in fact, my first version here gavenull
, which made it fall over completely. That's why I'd like to get rid of that.