comparison js/feed-preview.js @ 32:4492db3b277e

Use human-readable file size units
author Guido Berhoerster <guido+feed-preview@berhoerster.name>
date Wed, 23 Jan 2019 14:53:28 +0100
parents 76e23b361e92
children
comparison
equal deleted inserted replaced
31:76e23b361e92 32:4492db3b277e
5 * License, v. 2.0. If a copy of the MPL was not distributed with this 5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/. 6 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
7 */ 7 */
8 8
9 'use strict'; 9 'use strict';
10
11 function formatFileSize(size) {
12 const LIMITS_UNITS = new Map([
13 [1024 * 1024 * 1024, 'GiB'],
14 [1024 * 1024, 'MiB'],
15 [1024, 'KiB'],
16 [0, 'B']
17 ])
18 for (let [limit, unit] of LIMITS_UNITS) {
19 if (size >= limit) {
20 return `${Number(size / limit).toFixed(1)} ${unit}`;
21 }
22 }
23 return '? B';
24 }
10 25
11 export function renderFeedPreview(feedPreviewDocument, feed, 26 export function renderFeedPreview(feedPreviewDocument, feed,
12 expandEntriesByDefault) { 27 expandEntriesByDefault) {
13 // inject XSL stylesheet which transforms XHTML to HTML allowing the use of 28 // inject XSL stylesheet which transforms XHTML to HTML allowing the use of
14 // the HTML DOM 29 // the HTML DOM
120 fileLinkElement.href = file.url; 135 fileLinkElement.href = file.url;
121 fileLinkElement.title = file.filename; 136 fileLinkElement.title = file.filename;
122 fileLinkElement.textContent = file.filename; 137 fileLinkElement.textContent = file.filename;
123 138
124 fileNode.querySelector('.entry-file-info').textContent = 139 fileNode.querySelector('.entry-file-info').textContent =
125 `(${file.type}, ${file.size} bytes)`; 140 `(${file.type}, ${ formatFileSize(file.size)})`;
126 141
127 fileListElement.appendChild(fileNode); 142 fileListElement.appendChild(fileNode);
128 } 143 }
129 144
130 entryNode.querySelector('.entry').append(fileListNode); 145 entryNode.querySelector('.entry').append(fileListNode);