mirror of
https://gitcode.com/gh_mirrors/hy/Hydro.git
synced 2026-10-20 01:38:19 +08:00
完善设置
This commit is contained in:
1 parent
fc0d9873a2
commit
7edb29d7b8
43 files changed
+219
-184
No files matched your search
+19
-13
@@ -1,10 +1,10 @@
|
||||
const md5 = require('./md5');
|
||||
|
||||
exports.gravatar = function gravatar(email, size = 32) {
|
||||
return `//gravatar.loli.net/avatar/${md5((email || '').toString().trim().toLowerCase())}?d=mm&s=${size}`;
|
||||
};
|
||||
function gravatar(email, s = 32) {
|
||||
return `//gravatar.loli.net/avatar/${md5((email || '').toString().trim().toLowerCase())}?d=mm&s=${s}`;
|
||||
}
|
||||
|
||||
exports.datetime_span = function datetimeSpan(dt, relative = true, format = '%Y-%m-%d %H:%M:%S') {
|
||||
function datetimeSpan(dt, relative = true, format = '%Y-%m-%d %H:%M:%S') {
|
||||
if (!dt) return 'DATETIME_SPAN_ERROR';
|
||||
if (dt.generationTime) dt = new Date(dt.generationTime * 1000);
|
||||
else if (typeof dt === 'number' || typeof dt === 'string') dt = new Date(dt);
|
||||
@@ -13,9 +13,9 @@ exports.datetime_span = function datetimeSpan(dt, relative = true, format = '%Y-
|
||||
dt.getTime() / 1000,
|
||||
dt.format(format),
|
||||
);
|
||||
};
|
||||
}
|
||||
|
||||
exports.paginate = function* paginate(page, numPages) {
|
||||
function* paginate(page, numPages) {
|
||||
const radius = 2; let first; let
|
||||
last;
|
||||
if (page > 1) {
|
||||
@@ -25,7 +25,9 @@ exports.paginate = function* paginate(page, numPages) {
|
||||
if (page <= radius) [first, last] = [1, Math.min(1 + radius * 2, numPages)];
|
||||
else if (page >= numPages - radius) {
|
||||
[first, last] = [Math.max(1, numPages - radius * 2), numPages];
|
||||
} else [first, last] = [page - radius, page + radius];
|
||||
} else {
|
||||
[first, last] = [page - radius, page + radius];
|
||||
}
|
||||
if (first > 1) yield ['ellipsis', 0];
|
||||
for (let page0 = first; page0 < last + 1; page0++) {
|
||||
if (page0 !== page) yield ['page', page0];
|
||||
@@ -34,15 +36,19 @@ exports.paginate = function* paginate(page, numPages) {
|
||||
if (last < numPages) yield ['ellipsis', 0];
|
||||
if (page < numPages) yield ['next', page + 1];
|
||||
yield ['last', numPages];
|
||||
};
|
||||
}
|
||||
|
||||
exports.size = function formatSize(size, base = 1) {
|
||||
size *= base;
|
||||
function size(s, base = 1) {
|
||||
s *= base;
|
||||
const unit = 1024;
|
||||
const unitNames = ['Bytes', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB'];
|
||||
for (const unitName of unitNames) {
|
||||
if (size < unit) return '{0} {1}'.format(Math.round(size), unitName);
|
||||
size /= unit;
|
||||
if (s < unit) return '{0} {1}'.format(Math.round(s), unitName);
|
||||
s /= unit;
|
||||
}
|
||||
return '{0} {1}'.format(Math.round(size * unit), unitNames[unitNames.length - 1]);
|
||||
return '{0} {1}'.format(Math.round(s * unit), unitNames[unitNames.length - 1]);
|
||||
}
|
||||
|
||||
global.Hydro.lib.misc = module.exports = {
|
||||
gravatar, datetimeSpan, paginate, size,
|
||||
};
|
||||
Reference in new issue
Block a user