Skip to content

Add ability to format bytes as metric (e.g: kilobyte) or IEC (e.g: kibibyte) format #10437

@chris48s

Description

@chris48s

📋 Description

Shields has a number of size badges. In general, there's a couple of patterns for how these work:

  1. In some cases, the upstream data source reports an already formatted number. So for example with the spiget badge, the API returns an already formatted size and units.
curl "https://api.spiget.org/v2/resources/771" -s | jq '.file.size, .file.sizeUnit'

That's what goes on the badge. In these situations the number on the badge is always going to match what is shown on the upstream service.

  1. The other pattern is that the upstream API reports a raw number of bytes and we format it. So for example with the bundlephobia badge the API returns a raw number of bytes.
curl "https://bundlephobia.com/api/size?package=react" -s | jq .size

In the second case, we always format the raw number of bytes for display using pretty-bytes, which exclusively uses metric (e.g: kilobyte) units.

As a general principle, we want shields to be "consistent". This means more than one thing. It can involve maintaining consistency both "horizontally" across our suite of badges (e.g: the NPM license badge should work the same way as the PyPI license badge) and "vertically" with the upstream data sources (i.e: if the registry says the latest version of your package is v2.4.1, so should we).

Always using pretty-bytes achieves horizontal consistency, but can lead to vertical inconsistency. If the upstram data provider uses IEC (e.g: kibibyte) units, the size we report on the badge may be slightly different. For example, the bundlephobia badge "disagrees" with the bundlephobia website.

In my view, vertical consistency is more important in this case.

For badges where we format a raw number of bytes, I think we should switch from pretty-bytes to another formatting library like byte-size which allows formatting using both metric and IEC units.

We should review each of the size badges where we format a raw number of bytes instead of receiving an already formatted number. There are not a huge number of these. Where the upstream data provider uses IEC units, we should switch to using that.

We could also consider exposing a param allowing the user to specify the formatting, but I think matching the upstream by default is the most important thing to do here.

This is something that has been discussed several times before (albeit in less detail):

Metadata

Metadata

Assignees

No one assigned

    Labels

    coreServer, BaseService, GitHub auth, Shared helpers

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions