Skip to content

Commit

Permalink
Even nicer file size outputs
Browse files Browse the repository at this point in the history
I removed the `precision: 2` in fe58da0 to make "1.0 MB" show up as "1 MB", but that made many other occurrences worse. This commit restores 2 as default precision, but lets us override the precision where we want to.
  • Loading branch information
arildm committed Oct 5, 2023
1 parent 2a4f966 commit 6f73ca6
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ As this project is a user-facing application, the places in the semantic version
### Fixed

- Downloading source file was broken for binary files
- Correct file size outputs using binary base instead of decimal

## [1.0.2] (2023-10-03)

Expand Down
7 changes: 4 additions & 3 deletions src/corpus/sources/UploadSizeLimits.vue
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import useMinkBackendInfo from "@/api/backendInfo.composable";
import useLocale from "@/i18n/locale.composable";
const { hasInfo, findInfo } = useMinkBackendInfo();
// Use filesize with precision 0 to have "1 MB" instead of "1.0 MB".
const { filesize } = useLocale();
</script>

Expand All @@ -11,19 +12,19 @@ const { filesize } = useLocale();
<tr>
<th>{{ $t("source.limit.file.recommended") }}</th>
<td class="text-right">
{{ filesize(findInfo("recommended_file_size", "max_file_length")) }}
{{ filesize(findInfo("recommended_file_size", "max_file_length"), 0) }}
</td>
</tr>
<tr>
<th>{{ $t("source.limit.file.max") }}</th>
<td class="text-right">
{{ filesize(findInfo("file_size_limits", "max_file_length")) }}
{{ filesize(findInfo("file_size_limits", "max_file_length"), 0) }}
</td>
</tr>
<tr>
<th>{{ $t("source.limit.upload.max") }}</th>
<td class="text-right">
{{ filesize(findInfo("file_size_limits", "max_content_length")) }}
{{ filesize(findInfo("file_size_limits", "max_content_length"), 0) }}
</td>
</tr>
</table>
Expand Down
6 changes: 4 additions & 2 deletions src/i18n/locale.composable.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,10 @@ export default function useLocale() {
);
}

function myFilesize(bytes) {
const str = filesize(bytes, { base: 2, locale: locale.value });
/** Wrap the filesize lib with some sane defaults and avoiding exponential notation. */
function myFilesize(bytes, precision = 2) {
// Default precision is 0 which means up until 2 decimals?
const str = filesize(bytes, { precision, base: 2, locale: locale.value });
// Convert exponential notation to ordinary.
return str.replace(/[\d.]+e[+\d]+/, parseFloat);
}
Expand Down

0 comments on commit 6f73ca6

Please sign in to comment.