1
0
mirror of https://github.com/badges/shields.git synced 2025-04-18 19:44:04 +03:00
shields/services/size.js
chris48s 151c70dd17
Add ability to format bytes as metric or IEC; affects [bundlejs bundlephobia ChromeWebStoreSize CratesSize DockerSize GithubRepoSize GithubCodeSize GithubSize NpmUnpackedSize SpigetDownloadSize steam VisualStudioAppCenterReleasesSize whatpulse] (#10547)
* add renderSizeBadge helper, use it everywhere

- switch from pretty-bytes to byte-size
- add renderSizeBadge() helper function
- match upstream conventions for metric/IEC units
- add new test helpers and use them in service tests

* unrelated: fix npm unpacked size query param schema

not strictly related to this PR
but I noticed it was broken

* chromewebstore: reformat size string, test against isIecFileSize
2024-12-01 19:53:26 +00:00

26 lines
747 B
JavaScript

/**
* @module
*/
import byteSize from 'byte-size'
/**
* Creates a badge object that displays information about a size in bytes number.
* It should usually be used to output a size badge.
*
* @param {number} bytes - Raw number of bytes to be formatted
* @param {'metric'|'iec'} units - Either 'metric' (multiples of 1000) or 'iec' (multiples of 1024).
* This should align with how the upstream displays sizes.
* @param {string} [label='size'] - Custom label
* @returns {object} A badge object that has three properties: label, message, and color
*/
function renderSizeBadge(bytes, units, label = 'size') {
return {
label,
message: byteSize(bytes, { units }).toString(),
color: 'blue',
}
}
export { renderSizeBadge }