addons/firefox-addons/feed-preview

changeset 32:4492db3b277e

Use human-readable file size units
author Guido Berhoerster <guido+feed-preview@berhoerster.name>
date Wed Jan 23 14:53:28 2019 +0100 (16 months ago)
parents 76e23b361e92
children d2bf40011989
files js/feed-preview.js
line diff
     1.1 --- a/js/feed-preview.js	Tue Jan 22 14:40:06 2019 +0100
     1.2 +++ b/js/feed-preview.js	Wed Jan 23 14:53:28 2019 +0100
     1.3 @@ -8,6 +8,21 @@
     1.4  
     1.5  'use strict';
     1.6  
     1.7 +function formatFileSize(size) {
     1.8 +    const LIMITS_UNITS = new Map([
     1.9 +        [1024 * 1024 * 1024, 'GiB'],
    1.10 +        [1024 * 1024, 'MiB'],
    1.11 +        [1024, 'KiB'],
    1.12 +        [0, 'B']
    1.13 +    ])
    1.14 +    for (let [limit, unit] of LIMITS_UNITS) {
    1.15 +        if (size >= limit) {
    1.16 +            return `${Number(size / limit).toFixed(1)} ${unit}`;
    1.17 +        }
    1.18 +    }
    1.19 +    return '? B';
    1.20 +}
    1.21 +
    1.22  export function renderFeedPreview(feedPreviewDocument, feed,
    1.23          expandEntriesByDefault) {
    1.24      // inject XSL stylesheet which transforms XHTML to HTML allowing the use of
    1.25 @@ -122,7 +137,7 @@
    1.26                  fileLinkElement.textContent = file.filename;
    1.27  
    1.28                  fileNode.querySelector('.entry-file-info').textContent =
    1.29 -                        `(${file.type}, ${file.size} bytes)`;
    1.30 +                        `(${file.type}, ${ formatFileSize(file.size)})`;
    1.31  
    1.32                  fileListElement.appendChild(fileNode);
    1.33              }