-
Notifications
You must be signed in to change notification settings - Fork 11.6k
Added bitRate number helper #57285
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
Added bitRate number helper #57285
Conversation
$units = ['bps', 'Kbps', 'Mbps', 'Gbps', 'Tbps', 'Pbps', 'Ebps', 'Zbps', 'Ybps']; | ||
|
||
$unitCount = count($units); | ||
|
||
for ($i = 0; ($bits / 1000) > 0.9 && ($i < $unitCount - 1); $i++) { | ||
$bits /= 1000; | ||
} | ||
|
||
return sprintf('%s %s', static::format($bits, $precision, $maxPrecision), $units[$i]); |
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.
You could reduce duplicated code
$units = ['bps', 'Kbps', 'Mbps', 'Gbps', 'Tbps', 'Pbps', 'Ebps', 'Zbps', 'Ybps']; | |
$unitCount = count($units); | |
for ($i = 0; ($bits / 1000) > 0.9 && ($i < $unitCount - 1); $i++) { | |
$bits /= 1000; | |
} | |
return sprintf('%s %s', static::format($bits, $precision, $maxPrecision), $units[$i]); | |
$formatted = strtolower(static::fileSize($bits, $precision, $maxPrexision) . 'ps'; | |
return $bits < 1000 ? $formatted : ucwords($formatted); |
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.
@shaedrich I didn't reuse the fileSize()
method because bits and bytes are calculated differently. Bytes are calculated using binary base-2 (power of 1024) where bits are decimal base-10 (power of 1000).
Also the capitalization of the "b" matters as "Kb" = Kilobit and "KB" is Kilobyte etc.
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.
Capitalization is taken care of as you can see, but you are right about the base.
This could probably be moved to a private utility method:
/**
* @template TValue of string[]
*
* @param TValue $values
*
* @return array{int|float, value-of<TValue>}
*/
private static function convertForDisplay(int|float $value, array $units, int $base)
{
$unitCount = count($units);
for ($i = 0; ($value / $base) > 0.9 && ($i < $unitCount - 1); $i++) {
$value /= $base;
}
return [$value, $units[$i]];
}
but maybe, this is overkill
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.
My 2c I think the utility method might be useful if there is a 3rd method that relies on converting an integer to a string with a unit. Just my personal take 🤷♂️
Happy to do that in a separate PR and apply that to the fileSize()
method as well.
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.
Absolutely 👍🏻
Thanks for your pull request to Laravel! Unfortunately, I'm going to delay merging this code for now. To preserve our ability to adequately maintain the framework, we need to be very careful regarding the amount of code we include. If applicable, please consider releasing your code as a package so that the community can still take advantage of your contributions! |
📃 Description
This PR introduces a
bitRate()
number helper to convert a given bit value to a string representation. Example1000
to1 Kbps
. This is modeled off of the existingfileSize()
helper.📖 Docs
laravel/docs#10846