Cleanup Date.fromNow() and friendlySize()

This commit is contained in:
djmaze 2020-10-03 14:02:33 +02:00
parent 2bd9528098
commit a0f8ac0dad
7 changed files with 28 additions and 50 deletions

View file

@ -7,6 +7,7 @@ const
msOffice = app+'vnd.openxmlformats-officedocument.',
openDoc = app+'vnd.oasis.opendocument.',
font = app+'x-font-',
sizes = ['B', 'KiB', 'MiB', 'GiB', 'TiB'],
exts = {
'eml': 'message/rfc822',
@ -309,5 +310,12 @@ export const File = {
}
return '';
},
friendlySize: bytes => {
bytes = parseInt(bytes, 10) || 0;
let i = Math.floor(Math.log(bytes) / Math.log(1024));
return (bytes / Math.pow(1024, i)).toFixed(2>i ? 0 : 1) + sizes[i];
}
};

View file

@ -99,16 +99,6 @@ export function inFocus() {
return false;
}
/**
* @param {(number|string)} sizeInBytes
* @returns {string}
*/
export function friendlySize(sizeInBytes) {
sizeInBytes = pInt(sizeInBytes);
const sizes = ['B', 'KiB', 'MiB', 'GiB', 'TiB'], i = pInt(Math.floor(Math.log(sizeInBytes) / Math.log(1024)));
return (sizeInBytes / Math.pow(1024, i)).toFixed(2>i ? 0 : 1) + sizes[i];
}
/**
* @param {string} theme
* @returns {string}

View file

@ -1,7 +1,6 @@
import ko from 'ko';
import { File, FileType } from 'Common/File';
import { pInt, friendlySize } from 'Common/Utils';
import {
attachmentDownload,
attachmentPreview,
@ -26,7 +25,6 @@ class AttachmentModel extends AbstractModel {
this.fileName = '';
this.fileNameExt = '';
this.fileType = FileType.Unknown;
this.estimatedSize = 0;
this.friendlySize = '';
this.isInline = false;
this.isLinked = false;
@ -60,7 +58,6 @@ class AttachmentModel extends AbstractModel {
if (json && 'Object/Attachment' === json['@Object']) {
this.mimeType = ((json.MimeType || '').toLowerCase()).trim();
this.fileName = json.FileName.trim();
this.estimatedSize = pInt(json.EstimatedSize);
// if it is inline
this.isInline = !!json.IsInline;
// if inline image is linked with CID in html
@ -76,7 +73,7 @@ class AttachmentModel extends AbstractModel {
this.mimeIndex = json.MimeIndex;
this.framed = !!json.Framed;
this.friendlySize = friendlySize(this.estimatedSize);
this.friendlySize = File.friendlySize(json.EstimatedSize);
this.cidWithoutTags = this.cid.replace(/^<+/, '').replace(/>+$/, '');
this.fileNameExt = File.getExtension(this.fileName);

View file

@ -1,5 +1,5 @@
import ko from 'ko';
import { pInt, friendlySize } from 'Common/Utils';
import { pInt } from 'Common/Utils';
import { File } from 'Common/File';
import { AbstractModel } from 'Knoin/AbstractModel';
@ -51,7 +51,7 @@ class ComposeAttachmentModel extends AbstractModel {
this.friendlySize = ko.computed(() => {
const localSize = this.size();
return null === localSize ? '' : friendlySize(localSize);
return null === localSize ? '' : File.friendlySize(localSize);
});
this.mimeType = ko.computed(() => File.getContentType(this.fileName()));

View file

@ -5,8 +5,7 @@ import { i18n } from 'Common/Translator';
import {
pInt,
encodeHtml,
friendlySize
encodeHtml
} from 'Common/Utils';
import { messageViewLink, messageDownloadLink } from 'Common/Links';
@ -188,7 +187,7 @@ class MessageModel extends AbstractModel {
* @returns {string}
*/
friendlySize() {
return friendlySize(this.size());
return File.friendlySize(this.size());
}
computeSenderEmail() {

View file

@ -15,7 +15,8 @@ import { UNUSED_OPTION_VALUE } from 'Common/Consts';
import { leftPanelDisabled, moveAction } from 'Common/Globals';
import { computedPagenatorHelper, friendlySize } from 'Common/Utils';
import { computedPagenatorHelper } from 'Common/Utils';
import { File } from 'Common/File';
import { mailBox, append } from 'Common/Links';
import { Selector } from 'Common/Selector';
@ -903,9 +904,9 @@ class MessageListMailBoxUserView extends AbstractViewNext {
quotaTooltip() {
return i18n('MESSAGE_LIST/QUOTA_SIZE', {
'SIZE': friendlySize(this.userUsageSize()),
'SIZE': File.friendlySize(this.userUsageSize()),
'PROC': this.userUsageProc(),
'LIMIT': friendlySize(this.userQuota())
'LIMIT': File.friendlySize(this.userQuota())
});
}

39
dev/prototype.js vendored
View file

@ -60,13 +60,6 @@
return (1 > w
? getWeek(new Date(x.getFullYear()-1,11,31)) /* previous year, last week */
: (52 < w && 4 > getISODay(x) ? 1 /* next year, first week */ : w) );
},
isDST = x => {
let y=x.getFullYear();
return x.getTimezoneOffset() != Math.max(
new Date(y, 0, 1).getTimezoneOffset(),
new Date(y, 6, 1).getTimezoneOffset()
);
};
// Defining locale
@ -153,7 +146,6 @@
case 's': return pad2(UTC?x.getUTCSeconds():x.getSeconds());
case 'u': return (UTC?x.getUTCMilliseconds():x.getMilliseconds()).toString().padStart(3,'0');
// Timezone
case 'I': return UTC ? 0 : isDST(x) ? 1 : 0;
case 'O': return UTC ? 'Z' : (d.Z > 0 ? '+' : '-') + pad2(Math.abs(d.Z / 60)) + '00';
case 'P': return UTC ? 'Z' : (d.Z > 0 ? '+' : '-') + pad2(Math.abs(d.Z / 60)) + ':' + pad2(Math.abs(d.Z % 60));
case 'T': return UTC ? 'UTC' : new Date(d.Y, 0, 1).toTimeString().replace(/^.+ \(?([^)]+)\)?$/, '$1');
@ -170,34 +162,25 @@
// Simulate momentjs fromNow function
Date.prototype.fromNow = function() {
let format,
let format = 's',
seconds = (Date.now() - this.getTime()) / 1000,
str = locale.relativeTime[0 < seconds ? 'past' : 'future'];
str = locale.relativeTime[0 < seconds ? 'past' : 'future'],
t = [[60,'m'],[3600,'h'],[86400,'d'],[2628000,'M'],[31536000,'y']],
i = 5;
seconds = Math.abs(seconds);
if (60 > seconds) {
format = 's';
} else if (3600 > seconds) {
seconds = seconds / 60;
format = 'm';
} else if (86400 > seconds) {
seconds = seconds / 3600;
format = 'h';
} else if (2628000 > seconds) {
seconds = seconds / 86400;
format = 'd';
} else if (31536000 > seconds) {
seconds = seconds / 2628000;
format = 'M';
} else {
seconds = seconds / 31536000;
format = 'y';
while (i--) {
if (t[i][0] <= seconds) {
seconds = seconds / t[i][0];
format = t[i][1];
break;
}
}
seconds = Math.round(seconds);
if (1 < seconds) {
format += format;
}
return str.replace('%s', locale.relativeTime[format].replace('%d', seconds));
}
};
Element.prototype.closestWithin = function(selector, parent) {
const el = this.closest(selector);